Stereo camera calibration from matlab to opencv
11 次查看(过去 30 天)
显示 更早的评论
this is the script i am using to make stereo camera calibration , i would like to convert the results from matlab to opencv , searched online there is a function does that but i dont know how to use it
% Detect calibration pattern in images
detector = vision.calibration.stereo.CheckerboardDetector();
[imagePoints, imagesUsed] = detectPatternPoints(detector, imageFileNames1, imageFileNames2);
% Generate world coordinates for the planar patten keypoints
squareSize = 15; % in units of 'millimeters'
worldPoints = generateWorldPoints(detector, 'SquareSize', squareSize);
% Read one of the images from the first stereo pair
I1 = imread(imageFileNames1{1});
[mrows, ncols, ~] = size(I1);
% Calibrate the camera
[stereoParams, pairsUsed, estimationErrors] = estimateCameraParameters(imagePoints, worldPoints, ...
'EstimateSkew', false, 'EstimateTangentialDistortion', false, ...
'NumRadialDistortionCoefficients', 2, 'WorldUnits', 'millimeters', ...
'InitialIntrinsicMatrix', [], 'InitialRadialDistortion', [], ...
'ImageSize', [mrows, ncols]);
% View reprojection errors
h1=figure; showReprojectionErrors(stereoParams);
% Visualize pattern locations
h2=figure; showExtrinsics(stereoParams, 'CameraCentric');
% Display parameter estimation errors
displayErrors(estimationErrors, stereoParams);
% You can use the calibration data to rectify stereo images.
I2 = imread(imageFileNames2{1});
[J1, J2, reprojectionMatrix] = rectifyStereoImages(I1, I2, stereoParams);
0 个评论
回答(1 个)
Akanksha
2025-3-1
Below is the revised code that will help you with your query :
1. Create an Asymmetric Synthetic Checkerboard
rowSquares = 8; % Vertical dimension (one is even)
colSquares = 7; % Horizontal dimension (the other is odd)
squareSize_px = 50; % Pixels per checkerboard square
% Calculate image height and width in pixels
imgHeight = rowSquares * squareSize_px;
imgWidth = colSquares * squareSize_px;
% Initialize a white image
Ibase = 255 * ones(imgHeight, imgWidth, 'uint8');
% Paint black squares (chessboard-like)
for r = 0 : (rowSquares-1)
for c = 0 : (colSquares-1)
% If (r+c) is even, paint that square black
if mod(r+c, 2) == 0
rowStart = r*squareSize_px + 1;
rowEnd = (r+1)*squareSize_px;
colStart = c*squareSize_px + 1;
colEnd = (c+1)*squareSize_px;
Ibase(rowStart:rowEnd, colStart:colEnd) = 0; % black
end
end
end
% Synthetic "left" camera sees the checkerboard straight on
Ileft = Ibase;
% Synthetic "right" camera sees the checkerboard rotated by 5 degrees
Iright = imrotate(Ibase, 5, 'crop');
% Save to disk if you want real image files
imwrite(Ileft, 'left.png');
imwrite(Iright, 'right.png');
2. Detect Checkerboard Corners in Both Images
% NOTE: detectCheckerboardPoints also returns 'boardSize' = [m,n],
% the number of squares in the vertical (m) and horizontal (n) directions.
[imagePointsLeft, boardSizeLeft] = detectCheckerboardPoints(Ileft);
[imagePointsRight, boardSizeRight] = detectCheckerboardPoints(Iright);
disp('Number of corners detected in left image:');
disp(size(imagePointsLeft, 1));
disp('Number of corners detected in right image:');
disp(size(imagePointsRight, 1));
% Ensure both images detect the same boardSize
if any(boardSizeLeft ~= boardSizeRight)
error('Left and right images do not have the same board size.');
end
3. Create the 4D imagePoints Array
% For stereo, the shape must be: M x 2 x 2 x N
% M = number of corners
% 2 = x, y
% 2 = two cameras (left, right)
% N = number of pairs
M = size(imagePointsLeft, 1); % e.g., 42 corners
% Combine left and right corners into one pair
imagePointsOnePair = zeros(M, 2, 2);
imagePointsOnePair(:, :, 1) = imagePointsLeft; % left camera
imagePointsOnePair(:, :, 2) = imagePointsRight; % right camera
% Fake having 2 pairs by duplicating the single pair
N = 2;
imagePoints = repmat(imagePointsOnePair, [1, 1, 1, N]);
4. Generate World Points that Match the Detected Corners
% Use the boardSize returned by detectCheckerboardPoints
squareSize_mm = 15; % Arbitrary real-world size of each square
worldPoints = generateCheckerboardPoints(boardSizeLeft, squareSize_mm);
% The image size (rows, columns)
[mrows, ncols] = size(Ileft);
5. Estimate the Stereo Camera Parameters
% This calibrates both cameras and obtains intrinsics, distortion, rotation, translation.
[stereoParams, pairsUsed, estimationErrors] = estimateCameraParameters(...
imagePoints, worldPoints, ...
'EstimateSkew', false, ...
'EstimateTangentialDistortion', false, ...
'NumRadialDistortionCoefficients', 2, ...
'WorldUnits', 'millimeters', ...
'ImageSize', [mrows, ncols]);
% Show reprojection errors (how well corners fit the estimated model)
figure; showReprojectionErrors(stereoParams);
title('Reprojection Errors');
% Visualize the extrinsics (camera positions/orientations)
figure; showExtrinsics(stereoParams, 'CameraCentric');
title('Extrinsics');
% Display numeric parameter estimation errors
displayErrors(estimationErrors, stereoParams);
6. Rectify the Images
[J1, J2] = rectifyStereoImages(Ileft, Iright, stereoParams);
figure;
subplot(1,2,1); imshow(J1); title('Rectified Left');
subplot(1,2,2); imshow(J2); title('Rectified Right');
7. Extract Parameters for OpenCV
K1 = stereoParams.CameraParameters1.IntrinsicMatrix;
distCoeffs1 = [stereoParams.CameraParameters1.RadialDistortion(1:2), ...
stereoParams.CameraParameters1.TangentialDistortion, ...
stereoParams.CameraParameters1.RadialDistortion(3)];
K2 = stereoParams.CameraParameters2.IntrinsicMatrix;
distCoeffs2 = [stereoParams.CameraParameters2.RadialDistortion(1:2), ...
stereoParams.CameraParameters2.TangentialDistortion, ...
stereoParams.CameraParameters2.RadialDistortion(3)];
R = stereoParams.RotationOfCamera2;
T = stereoParams.TranslationOfCamera2;
disp('========== OpenCV-Compatible Parameters ==========');
disp('K1 (Left Camera Intrinsic Matrix):');
disp(K1);
disp('distCoeffs1 (Left Camera Distortion):');
disp(distCoeffs1);
disp('K2 (Right Camera Intrinsic Matrix):');
disp(K2);
disp('distCoeffs2 (Right Camera Distortion):');
disp(distCoeffs2);
disp('R (Rotation of Right Cam relative to Left):');
disp(R);
disp('T (Translation of Right Cam relative to Left):');
disp(T);
You can refer to the following documentation to get more insights on the functions used in the above code and how the query is resolved.
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Camera Calibration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!