Minggu, 18 Mei 2008

Portofolio 6

Portofolio 6:

stereo1.jpg:

im1 = imread('stereo1.jpg');
imshow(im1),uv = ginput(12);





uv = uv';
calibpts;
xyz = calibPts;
c = calibrate(im1,xyz,uv);

hasil:

C =

0.6789 -0.7263 -0.0627 362.5067
-0.1722 -0.1175 -0.9483 342.2501
0.0006 0.0003 -0.0003 1.0000

mean squared error is 2.986016e-001
error in satisfying the camera calibration matrix is 5.820335e-009


stereo2.jpg:

im2= imread('stereo1.jpg');
imshow(im2),uv = ginput(12);



uv = uv';
calibpts;
xyz = calibPts;
c = calibrate(1m2,xyz,uv);

hasil:

C =

0.9448 -0.1889 -0.0717 348.9422
-0.0682 -0.2070 -0.9236 338.6192
0.0002 0.0005 -0.0003 1.0000

mean squared error is 1.379448e-001
error in satisfying the camera calibration matrix is 6.008675e-010


listing of calibpts.m:

% 3D Coordinate data of the target calibration points
% All positions are expressed in millimetres
%
% If you run this script by simply typing
%
% >> calibpts
%
% you will end up with a local 12 x 3 matrix variable called
% calibPts containing these data points.

calibPts = [ 49 0 65 % x y z coords of point 1
129 0 65 % x y z coords of point 2
49 0 145 % etc
129 0 145
49 0 225
129 0 225
0 129 65
0 49 65
0 129 145
0 49 145
0 129 225
0 49 225 ];


listing of 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)];

% buat 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, :),'rx')

% plot XYZ coordinates
plot(suv(1,:), suv(2,:),'b+')

% 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;

Tidak ada komentar: