Kalibrasi Kamera
Listing fungsi calibrate.m
% CALIBRATE
%
% Function to perform camera calibration
%
% Usage: C = calibrate(im, XYZ, uv)
%
% Where: im – is the image of the calibration target.
% XYZ – is a n x 3 array of XYZ coordinates
% of the calibration target points.
% uv – is a 2 x n array of the image coordinates
% of the calibration target points.
% C – is the 3 x 4 camera calibration matrix.
%
% This function plots the uv coordinates onto the image of
% the calibration target. It also projects the XYZ coordinates
% back into image coordinates using the calibration matrix
% and plots these points too as a visual check on the accuracy of
% the calibration process.
% Lines from the origin to the vanishing points in the X, Y and
% Z directions are overlaid on the image.
% The mean squared error between the positions of the uv coodinates
% and the projected XYZ coordinates is also reported.
%
% The function should also report the error in satisfying the
% camera calibration matrix constraint – the magnitude of
% (q1 x q3).(q2 x q3)
%
function C = calibrate(im, XYZ, uv)
% obtain rows so arbitrary number of points can be used
[rows, cols] = size(XYZ);
XYZ1 = [XYZ, ones(rows, 1)]; % makes it easier to work with
% build B matrix
for n = 1:rows
B(2*n-1, : )= [XYZ1(n, : ) 0 0 0 0 -uv(1, n)*XYZ(n, : )];
B(2*n, : ) = [0 0 0 0 XYZ1(n, : ) -uv(2, n)*XYZ(n, : )];
end
c = B \ uv(:);
c(12) = 1;
C = reshape(c,4,3)’
XYZ1 = XYZ1′;
for i = 1:rows
suv(:,i) = C*XYZ1(:,i);
suv(:,i) = suv(:,i)/suv(3,i);
end
% calculate the mean squared error between the positions of the uv coodinates and suv.
mse = mean(mean((uv – suv(1:2,:)).^2));
fprintf(1, ‘mean squared error is %d\n’, mse);
% calculate the error in satisfying the camera calibration matrix constraint
q1 = C(1,1:3)’;
q2 = C(2,1:3)’;
q3 = C(3,1:3)’;
error = abs(dot(cross(q1, q3), cross(q2, q3)));
fprintf(1, ‘error in satisfying the camera calibration matrix is %d\n’, error);
% annotate image
imshow(im);
hold;
% plot uv coordinates
plot(uv(1,
, uv(2,
,’r+’)
% plot XYZ coordinates
plot(suv(1,:), suv(2,:),’bx’)
% draw vanishing lines
for i = 1:3
line([C(1,4), C(1,i)/C(3,i)],[C(2,4), C(2,i)/C(3,i)])
end
hold;
Sebelum menjalankan fungsi calibrate.m, fungsi stereo.m ( ada di portofolio 8 ) harus dijalankan terlebih dahulu untuk mendapatkan XYZ dan uv.
Dari fungsi stereo.m didapatkan:
XYZ =
-7.8964 124.8445 228.2591
-7.8892 48.7874 227.7442
-2.3221 130.4471 147.1380
-0.7141 50.5747 146.7381
-5.0965 127.0813 68.1338
-5.3585 45.8466 71.0790
51.4819 7.7006 220.6782
134.5497 12.8033 222.6472
50.3066 4.4372 146.1820
132.0424 8.0994 143.9290
51.7956 7.9634 57.3379
137.3195 9.2072 60.4677
uv1 =
Columns 1 through 10
261 327 258 326 258 325 393 427 391 425
115 130 187 202 257 274 127 103 196 178
Columns 11 through 12
384 421
277 245
uv2 =
Columns 1 through 10
306 333 306 335 302 330 400 469 396 464
103 124 172 197 241 263 138 127 207 195
Columns 11 through 12
392 462
279 268
Kemudian dijalankan fungsi calibrate.m, dari ’stereo1.jpg’ dihasilkan:
Kemudian dijalankan fungsi calibrate.m, dari ’stereo2.jpg’ dihasilkan:
Nilai matriks kalibrasi untuk ’stereo1.jpg’:
C =
0.6590 -0.7035 -0.0556 363.9159
-0.1924 -0.1134 -0.9588 345.3708
0.0005 0.0005 -0.0003 1.0000
Nilai matriks kalibrasi untuk ’stereo2.jpg’:
C =
0.9040 -0.2829 -0.0331 348.0122
-0.0735 -0.2575 -0.8987 336.8385
0.0002 0.0002 -0.0002 1.0000
Nilai error reproyeksi untuk ’stereo1.jpg’:
mean squared error is 5.546909e-001
error in satisfying the camera calibration matrix is 4.894755e-010
Nilai error reproyeksi untuk ’stereo2.jpg’:
mean squared error is 5.742843e-001
error in satisfying the camera calibration matrix is 8.831331e-009