How to create stereoParameters with known rotation and translation matrices, for rectification

7 views (last 30 days)
I have a translation and rotation matrix for two identical cameras. The translation matrix is in units of mm. The left and right camera images are rendered using these orientations.
How do I figure out the translation and rotation expected by stereoParameters? I have tried:
% Rotation and position of each camera
camera_left.sensor_rotation = eul2rotmat(deg2rad([0,-5,0]));
camera_right.sensor_rotation = eul2rotmat(deg2rad([0,5,0]));
camera_left.sensor_position = [-30,0,0];
camera_right.sensor_position = [30,0,0];
focalLength = focal_length / pixel_pitch; % focal length in pixels
principalPoint = [1024/2, 1280/2];
imageSize = [1280, 1024];
c = cameraIntrinsics(focalLength,principalPoint,imageSize);
cameraParams = cameraParameters('IntrinsicMatrix',c.IntrinsicMatrix);
% This is the part that seems to be incorrect
rot = camera_left.sensor_rotation * camera_right.sensor_rotation';
t = camera_left.sensor_position - camera_right.sensor_position * camera_left.sensor_rotation';
stereoParams = stereoParameters(cameraParams,cameraParams,rot, t);
[left, right] = rectifyStereoImages(image_left,image_right,stereoParams);
But the rectified images do not return valid disparities (and depending on the camera rotations, they look completely wrong).

Answers (1)

William Rose
William Rose on 26 May 2022
I am surprised your code runs, because lines 2 and 3 call a function by the wrong name - unless you wrote two functions of your own. You call eul2rotmat() and eurl2rotmat(). The Matlab function is eul2rotm().
  5 Comments
William Rose
William Rose on 27 May 2022
Aha! Now I see the prime after the second rotation matrix, to transpose it, which is the same as inverting it, since rotation matrices are orthonormal.
I see in the Help for stereoParameters, and I know you have seen the same, that rot and t are the rotation and translaiton of camera 2 "relative to camera 1". Therefore I assume the rot and t should be expressed in "camera 1 coordinates". I have not worked twith the computer vision toolbox, but when working with rotations and translations I always have to double check my work, especially with rotations, to correcly determine whether I want, and whether I am correctly expressing, a rotation of the coordinate frame, or a rotation of the objects in a frame - which are the inverse of each other. So the first thing I would check is to see if things work better if I invert the rotation, or the translation, or both.
William Rose
William Rose on 27 May 2022
I would also try not rotating and not translating camera 1 at all. Only translate and rotate camera 2. Then does stereoParameters() give expected results? That would be a step in the right direction...
I previously wrote "I assume the rot and t should be expressed in camera 1 coordinates." Maybe that is wrong, especially for the translation part. The Help says the translation argument to stereoParameters() shoud be the position of cam2 relative to cam1, but maybe that means cam2position-cam1position, in "absolute" coordinates. If true, your formula for t would need to be changed.

Sign in to comment.

Categories

Find more on MATLAB Support Package for USB Webcams in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!