Skip to content

Rayrai Example: Lens Distortion ​

Overview ​

Renders one scene, a checkerboard wall with a sphere and a cylinder in front of it, through three cameras at the same pose and shows the images side by side, 480 x 300 pixels each:

  • an ideal pinhole with a 1.5 rad horizontal field of view,
  • the same focal length and principal point with OpenCV plumb_bob barrel distortion (k1 = -0.3, k2 = 0.09, small p1 and p2),
  • an equidistant (OpenCV fisheye) lens with a 2.8 rad field of view and k1..k4.

The straight edges of the checkerboard bend in the distorted images exactly as a calibrated camera with these coefficients would bend them.

Target ​

CMake target: rayrai_lens_distortion.

Run ​

From the repository root, build the target and run it:

bash
source ./raisim_env.sh
cmake --build build-examples --target rayrai_lens_distortion --parallel 2
./build-examples/examples/rayrai_lens_distortion

See Build examples to configure a new build directory.

On Windows, run rayrai_lens_distortion.exe instead. This example renders in process with rayrai and does not need rayrai_tcp_viewer.

What you see ​

  • CPU projections (on by default) draws a magenta dot on every inner corner of the checkerboard, where raisim::cameraRayToPixel() images it with the camera's lens model. The dots sit on the corners of the rendered squares in all three images: the renderer's image and RaiSim's CPU lens model agree.
  • Below each image, the camera matrix K (fx, fy, cx, cy in the OpenCV pixel convention) and the distortion vector D in OpenCV order, as raisim::cameraMatrix() and raisim::cameraDistortionCoefficients() return them, ready for a ROS CameraInfo message or cv::undistort().
  • The fisheye image is black outside its image circle: rays more than half the horizontal field of view from the optical axis have no return.

Save the window ​

The capture that the rayrai examples share (see rayrai/example_common.hpp) saves the window as a PNG and exits:

bash
RAYRAI_EXAMPLE_CAPTURE=lens_distortion.png ./build-examples/examples/rayrai_lens_distortion

Details ​

  • Builds the three cameras from RGBCamera::RGBCameraProperties: the barrel lens with CameraLensModel::setOpenCvPinhole(fx, fy, cx, cy, {k1, k2, p1, p2, k3}) and the fisheye with CameraLensModel::setOpenCvFisheye(fx, fy, cx, cy, k1, k2, k3, k4), the focal lengths taken from raisim::resolveCameraIntrinsics().
  • raisin::Camera(properties) sets up each camera's projection. A distorted or fisheye lens renders a larger pinhole image and remaps it through a lookup table built from RaiSim's pixel-to-ray mapping, so getFinalTexture() is already the distorted image.
  • Projects a world point by expressing it in the OpenCV optical frame of the camera (x right, y down, z forward) and calling raisim::cameraRayToPixel(); pixel (u, v) is the centre of column u and row v.

See Camera lens models for the models, their coefficients and the URDF syntax, and Lens models for how rayrai renders them.

RaiSim — physics for robotics and AI.