2015-04-19 35 views
1

I標記使用CC = bwconncomp(BW);和使用代碼如何把標籤圖像的矩形?這是使用CC = bwconncomp(BW)標記的

numPixels = cellfun(@numel,CC.PixelIdxList); 
[biggest,idx] = max(numPixels); 
BW(CC.PixelIdxList{idx}) = 0; 

掩蔽具有最大面積的標籤現在我希望把薄的矩形框圍繞最大面積,而不是掩蔽它的圖像。如何做到這一點?

全碼:

f = imread('test.PNG'); 
subplot(2,2,1); imshow(f,[]); title('Original Image'); 
for i = 1:3 
    Image = medfilt2(Image,[3 3]); 
end 
[Image_Num, num] = bwlabel(Image,8); 
subplot(2,2,3); imshow(Image); title('after median filtering'); %labeling algorithm 
BW=im2bw(Image); 
subplot(2,2,4); imshow(BW); title('binary image'); 
CC = bwconncomp(BW); %area based segmentation 
numPixels = cellfun(@numel,CC.PixelIdxList); 
[biggest,idx] = max(numPixels); 
BW(CC.PixelIdxList{idx}) = 0; 
figure; 
imshow(BW); title('AFTER AREA BASED MASKING'); 
+1

你有沒有考慮過使用'rectangle'註解函數? – Shai

回答

2

您可以使用bwlabelregionpropsrectangle註釋:

lb = bwlabel(bw); %// label each CC 
st = regionprops(lb, 'Area', 'BoundingBox'); %// get area and bounding box for each region 
[mxa mxi] = max([st.Area]); %// find max area region 

現在你可以註釋

figure; 
imshow(bw); hold on; 
rectangle('Position', st(mxi).BoundingBox, 'EdgeColor', 'r'); 

'coins.png'將此結果圖像爲: enter image description here

+0

如果有兩個面積相同的物體,那麼它是如何工作的?因爲在我的圖像中,我有兩個對象具有相同的面積...當我掩蓋他們都蒙面,但是當我使用此代碼時,它只能放置一個對象的矩形... –

+0

@santoshpatil在這種情況下,您需要使用'find':'mxi = find([st.Area] == max([st.Area]));'獲得面積等於最大值的區域的所有索引。你爲每個這樣的區域循環調用'rectangle'。 – Shai

+1

謝謝你的工作... –

1

CC.PixelIdxList{idx}會給你在哪裏你的對象位於圖像中的位置的線性指標。

您可以使用ind2sub將線性索引轉換爲行和列位置,然後我們可以確定這些行和列索引的左上角和右下角。一旦我們這樣做,我們就可以相應地標記你的圖像。

因此:

%// Determine row and column locations 
[row,col] = ind2sub(size(BW), CC.PixelIdxList{idx}); 

%// Get top left and bottom right coordinates 
topleft_row = min(row); 
topleft_col = min(col); 
bottomright_row = max(row); 
bottomright_col = max(col); 

%// Draw a white box around the object 
%// Left vertical line 
BW(topleft_row:bottomright_row, topleft_col) = true; 
%// Right vertical line 
BW(topleft_row:bottomright_row, bottomright_col) = true; 
%// Top horizontal line 
BW(topleft_row, topleft_col:bottomright_col) = true; 
%// Bottom horizontal line 
BW(bottomright_row, topleft_col:bottomright_col) = true; 

這裏使用的內置到MATLAB coins.png的例子。我在圖像中閱讀,將其限定並填入洞中。

im = imread('coins.png'); 
BW = im2bw(im, graythresh(im)); 
BW = imfill(O, 'holes'); 

當我做到這一點,運行上面的代碼周圍繪製的最大目標的矩形,這就是我得到:

enter image description here


如果這個代碼關係到你的程序,不要做任何掩蔽。上述替換掩蔽代碼.​​..所以:

f = imread('test.PNG'); 
subplot(2,2,1); imshow(f,[]); title('Original Image'); 
for i = 1:3 
    Image = medfilt2(Image,[3 3]); 
end 
[Image_Num, num] = bwlabel(Image,8); 
subplot(2,2,3); imshow(Image); title('after median filtering'); %labeling algorithm 
BW=im2bw(Image); 
subplot(2,2,4); imshow(BW); title('binary image'); 
CC = bwconncomp(BW); %area based segmentation 
numPixels = cellfun(@numel,CC.PixelIdxList); 
[biggest,idx] = max(numPixels); 

%-------- CHANGE HERE 

%// Determine row and column locations 
[row,col] = ind2sub(size(BW), CC.PixelIdxList{idx}); 

%// Get top left and bottom right coordinates 
topleft_row = min(row); 
topleft_col = min(col); 
bottomright_row = max(row); 
bottomright_col = max(col); 

%// Draw a white box around the object 
%// Left vertical line 
BW(topleft_row:bottomright_row, topleft_col) = true; 
%// Right vertical line 
BW(topleft_row:bottomright_row, bottomright_col) = true; 
%// Top horizontal line 
BW(topleft_row, topleft_col:bottomright_col) = true; 
%// Bottom horizontal line 
BW(bottomright_row, topleft_col:bottomright_col) = true; 
+0

嘿夥計它不工作...輸出只是大直線... –

+0

那麼它適用於我的例子。如果沒有你所有的代碼,我都不知道你做錯了什麼。抱歉。祝你好運! – rayryeng

+0

這裏是我的cmplte代碼'f = imread('test。PNG「); subplot(2,2,1) imshow(f,[]),title('Original Image') for i = 1:3 Image = medfilt2(Image,[3 3]); end [Image_Num,num] = bwlabel(Image,8); 副劇場(2,2,3); imshow(Image); title('中值過濾後'); %標記算法 BW = im2bw(Image); subplot(2,2,4); imgow(BW); imshow(BW); title('binary image'); CC = bwconncomp(BW);基於區域的分割 numPixels = cellfun [最大,idx] =最大(numPixels); BW(CC.PixelIdxList {idx})= 0; figure,imshow(BW); 標題('以區域爲基礎的掩蓋');' –