Camera Calibration Code
This code calculates the size in pixels of a piece of A4 paper. Feel free to modify the basic paper size to suit whatever you might have on hand.
% This program uses a sheet of A4 paper to calibrate the px/m ratio.
function calibration=CameraCalibration
clear xP yP
global OperatingSystem
uiwait(warndlg('Lay sheet of A4 paper on table'));
if strcmp(OperatingSystem,'Windows')
global vid
paperIm=getsnapshot(vid);
elseif strcmp(OperatingSystem,'Linux')
im_cap='/tmp/frameGrab.jpg';
paperIm=imread(im_cap,'JPG');
end
imagesc(paperIm);
colormap(gray);
uiwait(warndlg('Click on each corner of sheet in image. Proceed in clockwise order starting from lower right.'));
for i=1:4
[xP(i),yP(i)]=ginput(1)
end
for i=1:3
SideLength(i)=sqrt((xP(i)-xP(i+1))^2+(yP(i)-yP(i+1))^2);
end
SideLength(4)=sqrt((xP(4)-xP(1))^2+(yP(4)-yP(1))^2);
Average1=(SideLength(1)+SideLength(3))/2;
Average2=(SideLength(2)+SideLength(4))/2;
ShortSide=min(Average1,Average2);
LongSide=max(Average1,Average2);
% A4 paper has dimensions of 297x210mm
ShortCal=ShortSide/.210;
LongCal=LongSide/.297;
calibration=(ShortCal+LongCal)/2;