如何在MATLAB中识别和裁剪图像内的矩形 [英] How to identify and crop a rectangle inside an image in MATLAB

查看:2609
本文介绍了如何在MATLAB中识别和裁剪图像内的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图像中有一个矩形。矩形可以是任何设计,但背景不是单一颜色。这张照片是从像这样的手机相机拍摄的。

我想在图像中裁剪到内部图片(风景)。

I have an image which has a rectangle drawn in it. The rectangle can be of any design but the background isn't a single color. It's a photo taken from a phone camera like this one. I want to crop the to inner picture (scenary) in the image.


如何在MATLAB中执行此操作?

How can I do this in MATLAB?

我试过这段代码

img = im2double(imread('https://i.stack.imgur.com/iS2Ht.jpg'));
BW = im2bw(img);
dim = size(BW)
col = round(dim(2)/2)-90;
row = min(find(BW(:,col)))
boundary = bwtraceboundary(BW,[row, col],'N');
r = [min(boundary) , max(boundary)];
img_cropped = img(r(1) : r(3) , r(2) : r(4) , :);
imshow(img_cropped);

但它只适用于一张图片,这一张

but it works only for one image, this one

而不是上面的那个或这个

and not the one above or this one


我需要找到一个适用于任何具有特定矩形设计的图像的代码。任何帮助都将得到重视。谢谢

I need to find a code which works for any image with a specific rectangle design.Any help will be aprreciated.Thank you


推荐答案

下面的处理考虑以下内容:

The processing below considers the following:


  • 背景技术是单色的,可以使用渐变

  • 边框是单色(渐变),很容易与背景区分开来,而不是barocco / rococco风格

  • 一张图片是一种带有lo的真实世界图片细节,而非 Malevich的黑角广场

  • backgroung is monochrome, possible with gradient
  • a border is monochrome(gradient), is easily distinguishable from background, not barocco/rococco style
  • a picture is kind of real world picture with lots of details, not Malevich's Black Square

所以我们首先搜索图片并通过熵过滤展平背景

So we first search the picture and flatten background by entropy filtering

img=imread('http://i.stack.imgur.com/KMBRg.jpg');
%dimensions for neighbourhood are just a guess
cross_nhood = false(11,11); cross_nhood(:,6)=1;cross_nhood(6,:)=1;
img_ent = entropyfilt(img./255,repmat(cross_nhood,[1 1 3]));
img_ent_gray = rgb2gray(img_ent);

然后我们找到使用Harris探测器的角落并选择4点:最左边两个,最右边两个,然后裁剪图像因此去除背景(精确到倾斜)。我使用r2011a,你可能有点不同的功能,参考MATLAB帮助

Then we find corners using Harris detector and choose 4 point: two leftmost and two rightmost, and crop the image thus removing background (with precision up to inclination). I use r2011a, you may have a bit different functions, refer to MATLAB help

harris_pts = corner(img_ent_gray);
corn_pts = sortrows(harris_pts,1); 
corn_pts = [corn_pts(1:2,:);...
            corn_pts(size(corn_pts,1)-1:size(corn_pts,1),:)];
crop_img=img(min(corn_pts(:,2)):max(corn_pts(:,2)),...
             min(corn_pts(:,1)):max(corn_pts(:,1)),:);
corn_pts(:,1)=corn_pts(:,1) - min(corn_pts(:,1));
corn_pts(:,2)=corn_pts(:,2) - min(corn_pts(:,2));
corn_pts = corn_pts + 1;

这是一个问题:角点之间的线条倾斜一点不同的角度。它既可以是角点检测和图像捕获的问题(客观失真和/或有点错误的采集角度)。没有直截了当,永远正确的解决方案。我最好选择最大的倾角(它会稍微裁剪图片)并开始逐行处理图像(分割图像使用Bresenham算法)直到任何或大多数,你选择,像素属于图片,而不是内边界。可区分的特征可以是局部熵,颜色值的标准,特定的颜色阈值,不同的统计方法等等。

An here is a problem: the lines between the corner points are inclined at a bit different angle. It can be both problem of corner detection and image capture (objective distortion and/or a bit wrong acquisition angle). There is no straightforward, always right solution. I'd better choose the biggest inclination (it will crop the picture a little) and start processing the image line by line (to split image use Bresenham algorithm) till any or most, you to choose, pixels belongs to the picture, not the inner border. The distinguishable feature can be local entropy, std of colour values, specific colour threshold, different methods of statistics, whatever.

另一种方法是进行颜色分割,我喜欢大多数Gram-Shmidt正交化或 a * -b *颜色分割。但是,如果图像歪斜并且图片的一部分与边框的颜色匹配(参见最后一张图片,左下角),则会遇到所有相同的问题。

Another approach is to do colour segmentation, I like most Gram-Shmidt orthogonalization or a*-b* color segmentation. However, you'll get all the same problems if image is skewed and part of a picture matches the colour of a border (see last picture, bottom left corner).

这篇关于如何在MATLAB中识别和裁剪图像内的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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