Laboratory 7: Camera Calibration

Camera calibration involves finding the relationship between 3D world coordinates and their 2D projected positions in the image.

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)]; % 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, :),'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;
        

stereo1.jpg

Mean squared error is 1.229390e-001
Error in satisfying the camera calibration matrix is 8.796030e-010

% uv matrix obtained for stereo1.jpg
uv1 = [388   422   391   426   395   429   259   324   260   326   260   328;
       270   244   199   177   125   105   259   275   188   204   115   130];
  
% camera calibration maxtrix for stereo1.jpg
c1 = [0.6596   -0.7391   -0.0615  363.4235;
     -0.1851   -0.1387   -0.9437  342.7417;
      0.0005    0.0003   -0.0003    1.0000];

  

stereo2.jpg

mean squared error is 1.695418e-001
error in satisfying the camera calibration matrix is 9.944410e-010

% uv matrix obtained for stereo2.jpg
uv2 = [392   460   395   463   398   468   303   332   306   334   307   336;
       278   267   206   198   133   126   240   266   172   196   102   125];
   
% camera calibration maxtrix for stereo2.jpg  
c2 = [0.9234   -0.2221   -0.0257  347.7796;
     -0.0741   -0.2278   -0.9168  339.8960;
      0.0002    0.0004   -0.0002    1.0000];