簡體   English   中英

matlab - 找到一個可以覆蓋圖像中角色的邊界框

[英]matlab - Find a bounding box which can cover a character in image

圖像表示韓文字符。 圖像的所有值都由0或255組成。

然后我想得到一個可以完美覆蓋圖像中角色的邊界框。

例如:

輸入圖像

在此輸入圖像描述

輸出圖像 (我想要的是獲取紅色框的頂點。)

在此輸入圖像描述

我有一個想法,但我覺得這不好:

步驟1.找到圖像中最左邊和最上面的索引,說(l,up)

步驟2.找到圖像中最右邊和最下面的索引,比如說(r,low)

步驟3.然后,其頂點之一為(l,up)(r,low)的正方形( 邊界框 可以覆蓋圖像中的字符。

有沒有一個好主意或matlab庫?

即使沒有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(___)

您可以通過使用函數any來查找包含角色任何部分的行和列的邏輯索引 ,然后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);

這是裁剪區域的情節:

在此輸入圖像描述

如果要在裁剪區域周圍使用最小的一個像素邊框,可以修改boundBox的計算, boundBox所示:

boundBox = [find(colIndex, 1, 'first')-1 find(rowIndex, 1, 'first')-1; ...
            find(colIndex, 1, 'last')+1  find(rowIndex, 1, 'last')+1];
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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM