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

Sis*_*sta 1 matlab image crop edge-detection

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

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

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

上传测试图像:

Jon*_*nas 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);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

%# 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));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

%# 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),:);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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