I have obtained the camera calibration parameters such as camera matrix, distortion coeff, (rotation + translation) vector and image points using OpenCv code here.
The equation they used to compute the 2D screen coordinates is given by:
By hard coding the obtained values for the camera matrix, rotation + translation vector and the object point coordinates (X,Y,Z,1) in Matlab, i'm unable to get the same coordinate values as the image points. What am I missing here? Do i need to account for the distortion coefficients too for obtaining the exact or correct image points?
Matlab Code:
% Define all the parameters camera matrix , sample image point, object point, rotation and translation vectors%
cameraMatrix = [5.9354 0 3.1950; 0 5.9354 2.3950 ; 0 0 1]
rotationMatrix = [2.5233 1.6803 3.0728];
translationMatrix = [1.2682 1.9657 8.0141];
X = [0; 0; 0; 1];
rotationMatrix = transpose(rotationMatrix);
translationMatrix = transpose(translationMatrix);
%convert the rotation vector into rotation matrix using Rodrigues func.%
rotMat = rodrigues(rotationMatrix);
R_T = horzcat(rotMat, translationMatrix)
%Convert to 2D points%
imgPts = cameraMatrix * R_T * X
lastElement = imgPts(end);
ScreenImgPts = imgPts / lastElement
The object points are defined by the square size of the chessboard square size (30mm), i.e. [0,0,0,1] , [30,0,0,1] etc.
However after my calculations and comparing the image points stored in the xml file are not the same. My results are as follows
- 4.1343 3.8508 [0, 0, 0, 1]
- 3.8373 1.0331 [30, 0, 0, 1]
- 3.8002 0.6812 [60, 0, 0, 1]
The output (image points) for the first point, second point and 3rd point should be:
- 4.13546326e+002 3.85645935e+002
- 3.91346527e+002 3.85897003e+002
- 3.69121155e+002 3.86479431e+002
The output file for all the parameters is given here