Correct perspective image, specified focal length and camera position / rotation

I have an image taken with a standard camera, as shown in the setting below. I was fortunate to assume that I know the focal length (in millimeters), the crop coefficient and size of the sensor, and I can assume that the center of the projection is the center of the image - so I know its position in pixels and in terms of the sensor.

I also know the angle of the camera relative to the blue plane and its height above this plane. The camera rotates only along the x axis, which we consider parallel to the world x axis.

Camera setup above the plane

Now I need to get a metric image correction so that I can measure the object on the blue plane in millimeters (in 2D).

I tried different things in Matlab to get this to work, but my results so far have been disappointing. Clearly, this is a horizontal issue, and I think the scale should be solvable based on the fact that I know the size of the sensor and the number of pixels in it.

My searches led me to this similar question, but the answer methodology does not work for me (explained below): How to make perspective correction in Matlab from known internal and external parameters?

I tried to calculate the internal and external matrices, and then get the 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, gives me trash - given the full-size original of this image:

Reduced input image

I get this result from the above code:

Homography calculation result using above

x, , , , . - , ?

+4
1

. - KK , .

0

Source: https://habr.com/ru/post/1625194/


All Articles