How to correct the skewed perspective of an image in Matlab

614 Views Asked by At

I'm trying to transform this image:

img1

into something like this:

imgTransformed

in MATLAB, basically by matching the corners of the blue square with the corners of the image and making every other pixel transform accordingly.

I can find the corners of the distorted blue square and its centroid, its border and diagonals, if any of these can be useful for the solution.

I thought that I could create a projective tform based on matching corners. My attempt was:

img = imread('img1.png');

if size(img,3)==3
   img = rgb2gray(img);
end

movingPoints = [12 17; 253 16; 269 259;16 256]; %coordinate of distorted corners 
fixedPoints=[0 0;size(img,1) 0;size(img,1) size(img,2);0 size(img,2)]; %coordinate of image's corners 
TFORM = fitgeotrans(movingPoints,fixedPoints,'projective');
R=imref2d(size(img),[1 size(img,2)],[1 size(img,1)]);

imgTransformed=imwarp(imread('img1.png'),TFORM,'OutputView',R);
figure, imshow(imgTransformed);

But it seems to accomplish almost nothing. I also tried with:

  • TFORM = fitgeotrans(movingPoints,fixedPoints,'affine')
  • TFORM = fitgeotrans(movingPoints,fixedPoints,'polynomial',2) finding other 2 control points.

But still no result.

I know the OpenCV functions getperspectivetransform and warpPerspective can do something like this. Is there a way to do it in MATLAB?

0

There are 0 best solutions below