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_bobbarrel distortion (k1 = -0.3,k2 = 0.09, smallp1andp2), - 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:
source ./raisim_env.sh
cmake --build build-examples --target rayrai_lens_distortion --parallel 2
./build-examples/examples/rayrai_lens_distortionSee 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,cyin the OpenCV pixel convention) and the distortion vectorDin OpenCV order, asraisim::cameraMatrix()andraisim::cameraDistortionCoefficients()return them, ready for a ROSCameraInfomessage orcv::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:
RAYRAI_EXAMPLE_CAPTURE=lens_distortion.png ./build-examples/examples/rayrai_lens_distortionDetails
- Builds the three cameras from
RGBCamera::RGBCameraProperties: the barrel lens withCameraLensModel::setOpenCvPinhole(fx, fy, cx, cy, {k1, k2, p1, p2, k3})and the fisheye withCameraLensModel::setOpenCvFisheye(fx, fy, cx, cy, k1, k2, k3, k4), the focal lengths taken fromraisim::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, sogetFinalTexture()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 columnuand rowv.
See Camera lens models for the models, their coefficients and the URDF syntax, and Lens models for how rayrai renders them.