【发布时间】:2015-09-08 00:58:48
【问题描述】:
【问题讨论】:
标签: image matlab image-processing
【问题讨论】:
标签: image matlab image-processing
只是警告fitgeotrans 是 MATLAB R2013b 的一部分,因此这不适用于低于此的任何版本。
将图像变形为“梯形”的过程非常简单。您需要做的是创建一个转换对象,该对象获取图像的四个角点并将它们放置到您想要的梯形的相应角上。一旦你找到了这个变换,你就可以用这个变换对象扭曲图像,但要确保你指定了坐标系,以便原点相对于原始图像的左上角。如果你不这样做,那么原点是相对于将被裁剪的新图像。一旦您找到这个扭曲的图像,我们就会将其显示在相对于原始图像坐标系的图像框架上。
您正在使用来自 University of Southern California's SIPI (Signal and Image Processing Institute) Image Database 的臭名昭著的 Mandrill 图像,而 MATLAB 将其作为图像处理工具箱的一部分。首先加载数据集,因为这是一个带有指定颜色映射的索引图像,所以需要ind2rgb 才能将其转换为彩色图像。
%// Load in the image
load mandrill;
img = ind2rgb(X,map);
Mandrill 图像存储在img,我们得到:
现在下一部分是创建一个变换对象,将图像的四个角点扭曲到梯形坐标。 fitgeotrans 完美地做到了这一点。您需要指定一组“移动点”,即您要转换的点。在这种情况下,这些是您要转换的原始图像的四个角点。您将其放入4 x 2 矩阵中,其中第一列是列/x 坐标,第二列是行/y 坐标。我将分别指定左上角、右上角、左下角和右下角坐标。接下来,您需要指定一组“固定点”,它们是您希望移动点最终移动到的点。同样,这将在4 x 2 矩阵中,这些是梯形的坐标。我不得不胡乱摆弄坐标才能让它正确,但非常欢迎您根据自己的喜好修改它。您还想指定一个射影变换,因为梯形的经验非常丰富。
%// Create perspective transformation that warps the original
%// image coordinates to the trapezoid
movingPoints = [1 1; size(img,2) 1; 1 size(img,1); size(img,2) size(img,1)];
fixedPoints = [180 100; 340 100; 50 300; 450 300];
tform = fitgeotrans(movingPoints, fixedPoints, 'Projective');
tform 存储转换对象。接下来,我们需要创建一个参考坐标系来扭曲我们的图像。我们希望针对原始图像坐标系执行此操作。你可以使用imref2d 来做到这一点。第一个输入是图像的大小,分别是向量中的行和列,然后指定x/column 限制和y/row 限制。我们希望这是相对于原始图像的。
%// Create a reference coordinate system where the extent is the size of
%// the image
RA = imref2d([size(img,1) size(img,2)], [1 size(img,2)], [1 size(img,1)]);
RA 存储此参考帧。您需要做的最后一件事是使用这个新的转换对象扭曲图像。使用imwarp 来实现这种变形。要使用该功能,您需要指定要变形的图像、变换对象,并且要确保变形是相对于原始图像帧坐标系的,因此您必须将 'OutputView' 标志指定为上面创建的标志.
%// Warp the image
[out,r] = imwarp(img, tform, 'OutputView', RA);
out 包含扭曲的图像,r 包含图像相对的坐标系。对于这个问题,r 应该等于 RA。
现在,如果您想查看图像,请使用 imshow 和这个新的坐标系来最终显示图像。执行此操作时,您会看到显示的坐标区,因此请关闭它:
%// Show the image and turn off the axes
imshow(out, r);
axis off;
....我们得到:
为了您的复制和粘贴乐趣,这里是完整的代码:
%// Load in the image
load mandrill;
img = ind2rgb(X,map);
%// Create perspective transformation that warps the original
%// image coordinates to the trapezoid
movingPoints = [1 1; size(img,2) 1; 1 size(img,1); size(img,2) size(img,1)];
fixedPoints = [180 100; 340 100; 50 300; 450 300];
tform = fitgeotrans(movingPoints, fixedPoints, 'Projective');
%// Create a reference coordinate system where the extent is the size of
%// the image
RA = imref2d([size(img,1) size(img,2)], [1 size(img,2)], [1 size(img,1)]);
%// Warp the image
[out,r] = imwarp(img, tform, 'OutputView', RA);
%// Show the image and turn off the axes
imshow(out, r);
axis off;
【讨论】: