Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Matlab中显示四个角点之间的图像_Matlab_Image Processing_Image Rotation - Fatal编程技术网

在Matlab中显示四个角点之间的图像

在Matlab中显示四个角点之间的图像,matlab,image-processing,image-rotation,Matlab,Image Processing,Image Rotation,假设我有4个角点:(x1,y1);(x2,y2);(x3,y3);(x4,y4)和矩形图像尺寸(m,n) 如何显示图像,使图像在显示时的四个角位于上述四点。 换句话说,四个角可以控制图像旋转的角度(请记住图像边缘可能不平行) 谢谢 假设图像不会改变,只需2个点即可计算图像旋转。一般情况是这样的 angle = atan2(y2-y1, x2-x1)*180/pi; %angle between image and axis (in degrees) B = imrotate(A,angle);

假设我有4个角点:(x1,y1);(x2,y2);(x3,y3);(x4,y4)和矩形图像尺寸(m,n) 如何显示图像,使图像在显示时的四个角位于上述四点。 换句话说,四个角可以控制图像旋转的角度(请记住图像边缘可能不平行)
谢谢

假设图像不会改变,只需2个点即可计算图像旋转。一般情况是这样的

angle = atan2(y2-y1, x2-x1)*180/pi; %angle between image and axis (in degrees)
B = imrotate(A,angle);              %rotation

您需要扭曲图像以获得通用解决方案。您可以按如下方式进行操作:

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);
首先,阅读图片

img=imread('cameraman.tif');
if size(img,3)==3
   img=rgb2gray(img);
imgTransformed=imwarp(imread('cameraman.tif'),R,TFORM,'OutputView',R);
imshow(imgTransformed,[]);
指定变换点集(在您的示例中为
(x1,y1)…(x4,y4)
),它们是
固定点

movingPoints=[1 1;256 1; 256 256; 1 256] %(x,y) coordinate
fixedPoints=[25 25;250 12;255 200;30 180];
然后,估计转换。我选择投影变换。您也可以选择仿射

TFORM = fitgeotrans(movingPoints,fixedPoints,'projective');
由于希望图像转到指定的角点,因此必须指定输出视图。可以通过如下构造参考二维图像来实现

R=imref2d(size(img),[1 size(img,2)],[1 size(img,1)]);
最后,扭曲图像

img=imread('cameraman.tif');
if size(img,3)==3
   img=rgb2gray(img);
imgTransformed=imwarp(imread('cameraman.tif'),R,TFORM,'OutputView',R);
imshow(imgTransformed,[]);
显示图像

img=imread('cameraman.tif');
if size(img,3)==3
   img=rgb2gray(img);
imgTransformed=imwarp(imread('cameraman.tif'),R,TFORM,'OutputView',R);
imshow(imgTransformed,[]);

您应该将图像的角点放在指定的点上,并且包含图像的框的大小与原始图像的大小相同。

另一种与@Parag提出的方法非常类似的方法是以简单的方式使用MATLAB的图像变换函数。
以下是如何: 首先,您必须考虑图像是否在“统一”矩形内并相应地定义初始转换条件:

udata = [0 1]; 
vdata = [0 1];
fill_color = 128;
org_rect = [0 0;1 0;1 1;0 1];
请注意,
fill\u color
变量仅表示用于填充画布中未被变换图像覆盖的部分的颜色。然后应用从原始矩形到表示图像画布的新矩形的投影变换,如下所示:

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);
正如您可能注意到的,转换是双三次的,并返回输出图像(
out\u im
)和由
data
data
表示的新坐标系。如果只保留输出图像,则其大小将与原始坐标系中的输入图像相同(因此会稍微拉伸)。 为了正确显示图像,可以使用以下命令:

imshow(out_im,'XData',xdata,'YData',ydata);
这里有一个例子。让我们考虑一下如下所示的Lena的情况。

应用转换后,我们可以使用如下所示的正确坐标进行显示。

如果我们决定只显示输出图像,而不参考新坐标系,则会得到如下所示的图像。


本例中使用的输出矩形是:
[-1-2;2-1;3-3;-3-1]

那里的
新记录是什么?