display image between four corner points Matlab

*爱你&永不变心* 提交于 2019-12-03 21:37:56

问题


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!


回答1:


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.




回答2:


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



回答3:


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.


After applying the transformation we may display using the correct coordinates as shown below.


If we decide to just display the output image without any reference to the new coordinate system we get the image shown below.


The output rectangle used in this example was: [-1 -2;2 -1;3 3;-3 1]

来源:https://stackoverflow.com/questions/21818151/display-image-between-four-corner-points-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!