找到图像的边缘并在MATLAB中裁剪它 [英] Find the edges of image and crop it in MATLAB

查看:1085
本文介绍了找到图像的边缘并在MATLAB中裁剪它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a RGB image. I have scanned the image. So the image occupies a small portion of an A4 size sheet.

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

I want to find the border of the image and crop it. I could use edge detection operators like 'Sobel' etc, but they detect all the edges present in the image. All I want is the border of the image. Also many of the edge detection functions including 'bwboundaries' work only with binary or grayscale images. My image is RGB.

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

I tried using 'imcrop', but this is more of interactive cropping. I am keen on doing this automatically.

上传测试图像:

推荐答案

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

Since this is an rgb image, there will be apparent color in the gray areas, but there should be none in the white ones. You can make use of this to find the image, then you can get the bounding box.

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 修复你的形象。

There is a bit of a border left due to rotation. You can use the mask tto above to set all non-image pixels to NaN before cropping, or you can use imrotate to fix your image.

这篇关于找到图像的边缘并在MATLAB中裁剪它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆