I have an image captured using a standard camera, as shown in the setup below. I am fortunate enough to assume I know the focal length (in millimetres), the crop factor and the sensor size, and can assume the centre of projection is the centre of the image - so I know its position in pixels and in terms of the sensor.
I also know the angle of the camera with reference to the blue plane and its height above that plane. The camera only rotates in its x-axis, which we assume to be parallel to the world x-axis.
Now I need to get a metric rectification of the image such that I can measure an object on the blue plane in millimetres (in 2D).
I've been trying various things in Matlab to get this to work, but my results thus far have been disappointing. Clearly this is a plane-plane homography problem, and I think scale should be solvable based on the fact I know the sensor size and the number of pixels in it.
My searching led me to this similar question, but the methodology in the answers isn't working for me (explained further below): How to do perspective correction in Matlab from known Intrinsic and Extrinsic parameters?
I've tried calculating the intrinsic and extrinsic matrices, then getting a homography:
f = 4.9; % Focal length is given as 4.9mm
sensor_size = [6.17 4.55]; % Again in mm
cc = sensor_size ./ 2;
% Calculate intrinsic matrix
KK = [f 0 cc(1);0 f cc(2); 0 0 1];
theta = 21.8; % Angle of camera above plane, degrees
Rc = xrotate(theta) % xrotate fcn given below
Tc = [0 0 60]'; % Camera is 60mm above plane along camera z-axis
H = KK * [R(:,1) R(:,2) Tc];
figure;imshow(imtransform(I,maketform('projective',H), 'Size', image_size))
function R = xrotate( theta )
R = [ 1 0 0;
0 cosd(theta) sind(theta);
0 -sind(theta) cosd(theta) ];
end
This unfortunately provides me with junk - given the full-sized original of this image:
I get this result from the above code:
Given its only an x-axis rotation I would think we should get a trapezium shape as a result of the homography, but I don't seem to. Can anyone point to any obvious mistakes I've made here?