How to correct the skewed perspective of an image

15 vues (au cours des 30 derniers jours)
Jacopo Marcolini
Jacopo Marcolini le 5 Jan 2017
Réponse apportée : DGM le 2 Mai 2023
I'm trying to transform this image:
into something like this:
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. (Here some intermediate steps I did to find them: http://imgur.com/a/pc7VB )
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)
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?
  1 commentaire
Nikhil Sreekumar
Nikhil Sreekumar le 10 Jan 2017
Hi Jacopo,
The movingPoints, is it forming a quadrilateral or a rectangle or a curved polygon? On trying to use the coordinates for the movingPoints as [25 17; 252 9; 278 275;-3 239] with affine transformation, I was able to warp the image into the required shape. Am I missing something here?
Thanks
Nikhil

Connectez-vous pour commenter.

Réponses (1)

DGM
DGM le 2 Mai 2023
It's always a challenge trying to keep track of which MATLAB/IPT tools use image axes coordinates [x y], and which ones use array coordinates [y x]. in this case, the coordinates were flipped and one corner wasn't very accurately placed.
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/159518/image.png');
sz = size(img);
movingPoints = [17 12;
7 254;
259 269;
256 16]; %coordinate of distorted corners [x y]
fixedPoints = [0 0;
0 sz(1);
sz(2) sz(1);
sz(2) 0]; %coordinate of image's corners [x y]
TFORM = fitgeotrans(movingPoints,fixedPoints,'projective');
R = imref2d(sz,[1 sz(2)],[1 sz(1)]);
imgTransformed = imwarp(img,TFORM,'OutputView',R);
imshow(imgTransformed);

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by