[英]matlab - Find a bounding box which can cover a character in image
即使沒有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.