如何检测可以在蒙版上绘制的最大尺寸矩形? [英] How can I detect the maximum-sized rectangle that I can draw onto the mask?

查看:180
本文介绍了如何检测可以在蒙版上绘制的最大尺寸矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个图像处理项目,而且我已经陷入了项目的一个步骤。情况就是这样;

I'm making an image processing project and I have stuck in one the project's steps. Here is the situation;

这是我的面具:

我想检测适合此尺寸的最大尺寸矩形像这样的面具。

and I want to detect the maximum-sized rectangle that can fit into this mask like this.

我正在为我的项目使用MATLAB。你知道实现这一目标的任何快速方法吗?任何代码示例,方法或技术都会很棒。

I'm using MATLAB for my project. Do you know any fast way to accomplish this aim. Any code sample, approach or technique would be great.

编辑1:以下两种算法适用于大量案例。但是在一些困难的情况下,他们都给出了错误的结果。我在我的项目中使用了它们。

EDIT 1 : The two algorithms below are works with lot's of the cases. But both of them give wrong results in some difficult cases. I'am using both of them in my project.

推荐答案

此方法从整个图像开始,逐个像素地收缩每个边框,直到找到可接受的矩形。

This approach starts with the entire image and shrinks each border in turn pixel-by-pixel until it finds an acceptable rectangle.

在示例图像上运行需要约0.02秒,因此速度相当快。

It takes ~0.02 seconds to run on the example image, so it's reasonably fast.

编辑:我应该澄清一点,这并不是一个通用的解决方案。该算法依赖于矩形居中并且具有与图像本身大致相同的宽高比。但是,在适当的情况下,它很快。 @DanielHsH提供了一个他们声称适用于所有情况的解决方案。

EDIT: I should clarify that this isn't meant to be a universal solution. This algorithm relies on the rectangle being centered and having roughly the same aspect ratio as the image itself. However, in the cases where it is appropriate, it is fast. @DanielHsH offered a solution which they claim works in all cases.

代码:

clear; clc;
tic;
%% // read image
imrgb= imread('box.png');
im = im2bw(rgb2gray(imrgb));    %// binarize image
im = 1-im;                      %// convert "empty" regions to 0 intensity
[rows,cols] = size(im);

%% // set up initial parameters
ULrow = 1;       %// upper-left row        (param #1)
ULcol = 1;       %// upper-left column     (param #2)
BRrow = rows;    %// bottom-right row      (param #3)
BRcol = cols;    %// bottom-right column   (param #4)

parameters = 1:4;   %// parameters left to be updated
pidx = 0;           %// index of parameter currently being updated

%% // shrink region until acceptable
while ~isempty(parameters); %// update until all parameters reach bounds

    %// 1. update parameter number
    pidx = pidx+1;
    pidx = mod( pidx-1, length(parameters) ) + 1;
    p = parameters(pidx);   %// current parameter number

    %// 2. update current parameter
    if p==1; ULrow = ULrow+1; end;
    if p==2; ULcol = ULcol+1; end;
    if p==3; BRrow = BRrow-1; end;
    if p==4; BRcol = BRcol-1; end;

    %// 3. grab newest part of region (row or column)
    if p==1; region = im(ULrow,ULcol:BRcol); end;
    if p==2; region = im(ULrow:BRrow,ULcol); end;
    if p==3; region = im(BRrow,ULcol:BRcol); end;
    if p==4; region = im(ULrow:BRrow,BRcol); end;

    %// 4. if the new region has only zeros, stop shrinking the current parameter
    if isempty(find(region,1))
        parameters(pidx) = [];
    end

end

toc;
params = [ULrow ULcol BRrow BRcol]
area = (BRrow-ULrow)*(BRcol-ULcol) 






此图片的结果:


The results for this image:

Elapsed time is 0.027032 seconds.

params =

    10    25   457   471


area =

      199362

可视化结果的代码:

imrgb(params(1):params(3),params(2):params(4),1) = 0;
imrgb(params(1):params(3),params(2):params(4),2) = 255;
imrgb(params(1):params(3),params(2):params(4),3) = 255;
imshow(imrgb);

另一个示例图片:

这篇关于如何检测可以在蒙版上绘制的最大尺寸矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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