matlab - 找到一个可以覆盖图像中角色的边界框

时间:2017-05-21 07:58:03

标签: algorithm matlab image-processing computer-vision image-recognition

图像代表韩文字符。图像的所有值都由0或255组成。

然后我想得到一个可以完美覆盖图像中角色的边界框。

例如:

输入图片

enter image description here

输出图像(我想要的是获取红色框的顶点。)

enter image description here

我有一个想法,但我觉得这不好:

第1步。查找图片中最左边和最上面的索引,例如(l,up)

第2步。查找图片中最右边和最下面的索引,例如(r,low)

第3步。然后是正方形(边界框),其顶点之一是(l,up)(r ,低)可以覆盖图像中的角色。

对此有没有好主意或matlab库?

3 个答案:

答案 0 :(得分:0)

即使没有Matlab图像处理工具箱,您也可以使用find提取输入图像的左,右,上,下边界索引。 假设图像是二进制矩阵(逻辑1或0),称为"输入":

leftBoundary = find(input,1,'first');
rightBoundary = find(input,1,'last');
topBoundary = find(input',1,'first');
BotBoundary = find(input',1,'last');

请记住这些是线性指数。如果需要,你可以使用find的其他召唤方法来获得正常的下标

  

[row,col] = find(___)

答案 1 :(得分:0)

您可以使用函数any查找包含角色任何部分的行和列的logical indices,然后find获取行和列索引四肢:

img = imread('Y2ZIW.png');  % Load RGB image you posted
bw = ~im2bw(img);           % Convert to binary and negate
rowIndex = any(bw, 2);      % N-by-1 logical index for rows
colIndex = any(bw, 1);      % 1-by-N logical index for columns

boundBox = [find(colIndex, 1, 'first') find(rowIndex, 1, 'first'); ...
            find(colIndex, 1, 'last')  find(rowIndex, 1, 'last')];

这为boundBox提供了以下2×2矩阵,我们可以将其用作图像的索引,以仅裁剪包含该字符的区域:

boundBox =

    71    57    % Left and upper corner index
   214   180    % Right and lower corner index

subRegion = bw(boundBox(1, 2):boundBox(2, 2), boundBox(1, 1):boundBox(2, 1));
imshow(subRegion);

以下是裁剪区域的情节:

enter image description here

如果您想要在裁剪区域周围使用最小的一个像素边框,则可以修改boundBox的计算,如下所示:

boundBox = [find(colIndex, 1, 'first')-1 find(rowIndex, 1, 'first')-1; ...
            find(colIndex, 1, 'last')+1  find(rowIndex, 1, 'last')+1];

答案 2 :(得分:0)

I1 = imread('Y2ZIW.png') ;
I = rgb2gray(I1) ;
[y,x] = find(I==0) ;
%% Bounding box
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
B = abs(x1-x0) ;
L = abs(y1-y0) ;

BB = [x0 y0 ; x0 y0+L ; x0+B y0+L ; x0+B y0 ; x0 y0] ;

imshow(I1) ;
hold on
plot(BB(:,1),BB(:,2),'r')