2014-12-07 48 views

回答

3

默認情況下,當與logical型輸入掩碼引入regionprops自動應用bwlabel掩模,並計算爲輸入掩碼的每個連接組件屬性。
在你的情況,這不是一個理想的行爲,因爲你希望所有的白色像素被視爲同一個組件的一部分。爲了克服這種默認行爲,您只需將輸入掩碼從logical轉換爲其他數據類型即可。

st = regionprops(uint8(BW), 'BoundingBox'); %// cast to uint8 
rect = st.BoundingBox; %// the bounding box of all white pixels 

%// display the results 
figure; 
imshow(BW, 'border', 'tight'); 
hold on; 
rectangle('Position', rect, 'EdgeColor', 'r', 'LineWidth', 1.5); 


enter image description here

+0

所得謝謝你的迴應,但你的代碼仍然框僅該地區的一個片段。 – 2014-12-07 14:19:19

+0

@ASpac請參閱我的編輯 - 輸入圖像的結果。還請注意,在您提供的第一列和最後一行不是黑色的圖像中,我將它們作爲異常值刪除。 – Shai 2014-12-07 14:43:18

+0

我做了以下操作: '>> I5(:,1)= 0; >> I5(256,:)= 0; >> st = regionprops(uint8(I5),'BoundingBox'); %//投射到uint8 rect = st.BoundingBox; %//所有白色像素的邊界框 %//顯示結果 figure; imshow(I5,'border','tight'); 堅持; 矩形( '位置',矩形, 'EdgeColor', 'R', '線寬',1.5);' 和我得到這個錯誤:使用矩形 錯誤寬度和高度必須> 0 – 2014-12-08 08:15:49