display image between four corner points Matlab

2019-02-14 19:43发布

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!

3条回答
够拽才男人
2楼-- · 2019-02-14 20:16

Assuming the image will not be altered, only 2 points are needed to compute the image rotation. The general case is something like

angle = atan2(y2-y1, x2-x1)*180/pi; %angle between image and axis (in degrees)
B = imrotate(A,angle);              %rotation
查看更多
一纸荒年 Trace。
3楼-- · 2019-02-14 20:19

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.

查看更多
【Aperson】
4楼-- · 2019-02-14 20:21

Another approach very similar to the one proposed by @Parag, is to use the image transformation functions of MATLAB in a straightforward way.
Here is how: First you have to consider as if the image is within a 'unity' rectangle and define initial transformation conditions accordingly:

udata = [0 1]; 
vdata = [0 1];
fill_color = 128;
org_rect = [0 0;1 0;1 1;0 1];

Note that the fill_color variable represents just the color to be used for filling the parts of the canvas that will not be covered by the transformed image. Then you apply a projective transformation from the original to the new rectangle representing the image canvas as follows:

tform = maketform('projective', org_rect, new_rect);
[out_im,xdata,ydata] = imtransform( in_im, tform, 'bicubic', 'udata', udata, 'vdata', vdata, 'size', size(in_im), 'fill', fill_color);

As you may notice the transformation is bicubic and returns both the out image (out_im) and the new coordinate system represented by data and data. If you just keep the output image then it will be of the same size as the input image in the original coordinate system (so it will be a little stretched). In order to display the images properly you might use the following command:

imshow(out_im,'XData',xdata,'YData',ydata);

Here is an example. Let's considered the case of Lena shown below.
original Lena RGB
After applying the transformation we may display using the correct coordinates as shown below.
transformed Lena within the new coordinate system
If we decide to just display the output image without any reference to the new coordinate system we get the image shown below.
transformed Lena without coordinate system
The output rectangle used in this example was: [-1 -2;2 -1;3 3;-3 1]

查看更多
登录 后发表回答