【问题标题】:Find the edges of image and crop it in MATLAB查找图像的边缘并在 MATLAB 中对其进行裁剪
【发布时间】:2012-06-22 17:07:39
【问题描述】:

我有一个 RGB 图像。我已经扫描了图像。因此,图像只占 A4 尺寸纸张的一小部分。

我想找到图像的边框并裁剪它。我可以使用“Sobel”等边缘检测算子,但它们会检测图像中存在的所有边缘。我想要的只是图像的边框。此外,包括“bwboundaries”在内的许多边缘检测功能仅适用于二进制或灰度图像。我的图像是 RGB。

我尝试使用“imcrop”,但这更像是交互式裁剪。我热衷于自动执行此操作。

上传测试图片:

【问题讨论】:

  • 你能上传一个示例图片吗?

标签: image matlab crop edge-detection


【解决方案1】:

由于这是一张 rgb 图像,灰色区域会有明显的颜色,但白色区域应该没有。您可以利用它来查找图像,然后您可以获得边界框。

img = imread('http://i.stack.imgur.com/dEawA.jpg');
%# instead of "==" you can check for similarity within a tolerance
tt=img(:,:,1)==img(:,:,2) & img(:,:,2) == img(:,:,3);

%# invert tt so that it's 1 where there is signal
tt = ~tt;

%# clean up some of the smaller artifacts
tto = imopen(~tt,strel('square',100));

%# get the areas and bounding box of the areas above threshold
%# as an additional criterion, you could also use excentricity
%# or you could simply remove the bottom 100 rows of the scan
stats = regionprops(tto,'BoundingBox','Area');
area = cat(1,stats.Area);
[~,maxAreaIdx] = max(Area);
bb = round(stats(maxAreaIdx).BoundingBox);

%# note that regionprops switches x and y (it's a long story)
croppedImage = img(bb(2):bb(2)+bb(4),bb(1):bb(1)+bb(3),:);

由于旋转,会留下一点边框。您可以使用上面的掩码tto在裁剪前将所有非图像像素设置为NaN,也可以使用imrotate修复图像。

【讨论】:

  • 嗨乔纳斯,非常感谢。我不知道'regionprops'以这种方式工作。我想我可以试试这个。
【解决方案2】:

您可以尝试使用例如检测图像的角落哈里斯探测器(Matlab 中的corner)。将要检测的最大角点数设置为 4。然后使用 imcrop 中的角点位置。如果您发布图片,我可以给您更具体的提示。您的图像是 RGB 应该没有问题,只需将其转换为灰度即可。

【讨论】:

  • 谢谢。会试试这个。我上传了一张测试图片。由于背景和图像之间的像素值存在差异,因此图像的边界将是已知的。我将尝试 4 个角的角函数。
【解决方案3】:

您可以尝试使用bwlabelhttp://www.mathworks.com/help/toolbox/images/ref/bwlabel.html(连同find,如帮助页面中所述)来获取图像的索引并使用这些索引来裁剪原始图像。

您首先需要使用 im2bw http://www.mathworks.com/help/toolbox/images/ref/im2bw.html 将原始图像转换为二进制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2014-08-03
    • 2016-07-18
    相关资源
    最近更新 更多