找到图像的边缘并在MATLAB中裁剪它

时间:2012-06-20 14:19:53

标签: image matlab crop edge-detection

我有一张RGB图像。我扫描了图像。因此图像占据了A4尺寸的一小部分。

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

我尝试使用'imcrop',但这更像是交互式裁剪。我很想自动这样做。

上传测试图片:

3 个答案:

答案 0 :(得分:5)

由于这是一个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);

enter image description here

%# 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));

enter image description here

%# 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),:);

enter image description here

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

答案 1 :(得分:0)

您可以尝试使用以下方法检测图像的角落。 Harris-Detector(Matlab中的corner)。将要检测的最大角数设置为4.然后使用imcrop中角的位置。如果您要发布图像,我可以为您提供更具体的提示。您的图像是RGB应该不是问题,只需将其转换为灰度。

答案 2 :(得分:0)

您可以尝试使用bwlabel http://www.mathworks.com/help/toolbox/images/ref/bwlabel.html(以及帮助页面中提到的find)来获取图像的索引并使用它们裁剪原始图像。

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