【问题标题】:How to warp an image into a trapezoidal shape in MATLAB如何在 MATLAB 中将图像变形为梯形
【发布时间】:2015-09-08 00:58:48
【问题描述】:

我正在处理狒狒图像,我想要的是获得这样的图像:

我试过fitgeotransprojective2daffine2d都没有成功,可能我没有正确理解这些函数的行为。

感谢您的帮助。

【问题讨论】:

  • 请注意mandrill 不是baboon,尽管它们属于同一个家族。

标签: image matlab image-processing


【解决方案1】:

只是警告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;

【讨论】:

  • 感谢您的回答正是我想要的。我使用了正确的功能,但我使用了错误的方式。你的解释也很清楚。真的谢谢你!!
  • 我的荣幸!它只需要练习:)。祝你好运!
  • 我是一个铁杆几何变换的人,我喜欢将那些矩阵相乘,而不是使用那些简单的 tforms!呵呵呵呵。很好的答案,这本书缺少几何变换
  • @rayryeng 也许这只是一个混乱,就像 Lenna / Lena :-)
  • @LuisMendo - 如果可以的话,我会给你 +10 的评论:D
猜你喜欢
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 2011-04-15
  • 2014-06-04
相关资源
最近更新 更多