Search code examples
matlabimage-processingtoolbox

Rectify a photo in Matlab


I'm working with some road photos and I have to transform the captured perspective. My goal is to transform the road into a square. My photos look similar to this one: Road image

However, I need the road to take the shape of a square, not a triangle or trapeze, so I can work on it.

I have the angles Pitch, Yaw and Roll from my photo.

I'm trying this:

roll_x = 178.517332325424462;
pitch_y = -0.829084891331837937;
pan_z = -0.668910403057581759;

%% Transform perspective
img = imread('imageLoaded.png');
R_rot = R_z(pan_z)*R_y(pitch_y)*R_x(roll_x);

R_2d  = [   R_rot(1,1)  R_rot(1,2) 0; 
        R_rot(2,1)  R_rot(2,2) 0;
        0           0          1    ];
tform = affine2d(R_2d);
outputImage = imwarp(img,tform);
figure(1);
imshow(outputImage);


%% Matrix for Yaw-rotation about the Z-axis
function [R] = R_z(psi)
    R = [cosd(psi) -sind(psi) 0;
         sind(psi)  cosd(psi) 0;
         0          0         1];
end

%% Matrix for Pitch-rotation about the Y-axis
function [R] = R_y(theta)
    R = [cosd(theta)    0   sind(theta);
         0              1   0          ;
         -sind(theta)   0   cosd(theta)     ];
end

%% Matrix for Roll-rotation about the X-axis
function [R] = R_x(phi)
    R = [1  0           0;
         0  cosd(phi)   -sind(phi);
         0  sind(phi)   cosd(phi)];
end

but I'm rotating the image, not transforming it. How can I achieve my goal?


Solution

  • MATLAB has fitgeotrans, a function that can find the affine transform from points you can define manually, you could use it directly or to try and determine what's wrong with your transform.

    That said, an affine transform is not what you need. From the last page of this:

    Difference between transforms

    An affine transform maintains parallelism, which you can't have when transforming a trapeze-shaped road into a square. You need a projective transform (homography).

    See this StackOverflow answer on how to do it.