Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display image between four corner points Matlab

Suppose I have 4 corner points : (x1, y1) ; (x2, y2) ;(x3, y3) ; (x4, y4) and a rectangular image size (m,n) How do I display the image such that the image when shown has its corners at the four mentioned points. In other words, four corners can control the angle at which the image is rotated (bear in mind the image edges might not be parallel) Thanks!

like image 355
user3317119 Avatar asked Jan 12 '23 04:01

user3317119


1 Answers

You need to warp the image for a generalized solution. You can do it as follows:

First, Read the image.

img=imread('cameraman.tif');
if size(img,3)==3
   img=rgb2gray(img);

Specify the set of transformed points (in your case, (x1,y1) ... (x4,y4)), they are fixedPoints.

movingPoints=[1 1;256 1; 256 256; 1 256] %(x,y) coordinate
fixedPoints=[25 25;250 12;255 200;30 180];

Then, estimate the transformation. I choose projective transformation. You can choose affine as well.

TFORM = fitgeotrans(movingPoints,fixedPoints,'projective');

Since, you want the image to go to the specified corners, you have to specify the output view. It can be done by constructing a reference 2-D image as follows.

R=imref2d(size(img),[1 size(img,2)],[1 size(img,1)]);

Finally, warp the image.

imgTransformed=imwarp(imread('cameraman.tif'),R,TFORM,'OutputView',R);

Show the image.

imshow(imgTransformed,[]);

You should have the corners of your image at the specified points and the box which contains the image will be of the size of the original image.

like image 60
Autonomous Avatar answered Jan 17 '23 18:01

Autonomous