2013-04-17 34 views
0

在前景和背景的像素的數量採取字母「B」爲示例的圖像:計數圖像


如果我們畫出字母'B'的四邊直線,那麼它會創建一個矩形框。我必須在上面的圖片上的邊界框(重新調整框)內找到前景和背景像素的數量。我沒有得到result.pls幫助我。謝謝。

這是我的代碼:

E = imread('1.jpg'); 
label = graythresh(E); 
BW = im2bw(E, label); 
imshow(BW) 
L = bwlabel(E); 
z = regionprops(L,'BoundingBox'); 
nBlackpixel = sum(z(:)) 
nWhitepixel = numel(z) - nBlack 
+2

那麼,什麼是問題? –

+0

我沒有得到結果 – aryan

+0

在調試模式下運行並查看'z'和'z.BoundingBox' – Shai

回答

1

只使用'BoundingBox'財產

z = regionprops(L, 'BoundingBox'); 
nFG = zeros(1,numel(z)); % pre-allocate 
nBG = zeros(1,numel(z)); 
for ii=1:numel(z), % in case there are more than one object in the image 
    bb = imcrop(E, z(ii).BoundingBox); % crop the mask according to BoundingBox 
    nFG(ii) = nnz(bb); % count number of FG pixels 
    nBG(ii) = numel(bb) - nFG(ii); % nBG = tot number in BB minus num of FG 
end 
+0

謝謝。如何在上面的圖片中找到字母'B'的長寬比 – aryan

+0

@ aryan-這是一個新問題 - 張貼如此。 – Shai

1

嘗試使用不同的屬性

z = regionprops(L, 'Image'); 
nFG = zeros(1,numel(z)); % pre-allocate 
nBG = zeros(1,numel(z)); 
for ii=1:numel(z), % in case there are more than one object in the image 
    nFG(ii) = nnz(z(ii).Image); % count number of FG pixels 
    nBG(ii) = numel(z(ii).Image) - nFG(ii); % nBG = tot number in BB minus num of FG 
end 
+0

非常感謝你的代碼錯誤,但我一直在使用regionprops做(L,'BoundingBox') – aryan