2017-04-09 358 views
0

我正在寫一篇論文,我需要從附加的圖像中找出黑色區域的區域。MATLAB中的面積計算

Original image

我已通過使用閾值和稱讚的圖像進行一些處理。 Processed image 現在我在查找黑色區域的區域時遇到問題。有人可以幫忙嗎?我是MATLAB的新手。

這裏是我的代碼:

img1=imread('C:/Users/Allan/Desktop/unnamed1.jpg'); 
imshow(img1) 

img1=rgb2gray(img1); 
imshow(img1) 

img2=im2bw(img1,graythresh(img1)); 
imshow(img2) 

img2=~img2; 
imshow(img2) 

B = bwboundaries(img2); 
imshow(img2) 
hold on 

for k = 1:length(B) 
boundary = B{k}; 
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2) 
end 
+0

你能給你的代碼是如何沒有做更多的信息,你期待它?它是否會導致錯誤或輸出錯誤? –

+0

不,它不給任何錯誤!它只將原始圖像轉換爲我附加的處理過的圖像。所以在此之後,我需要知道如何找到黑色區域的區域。 – Allan

+0

@AlSweigart我提供了這個問題中圖片的鏈接。 – Allan

回答

0

使用regionpropsbwarea

% take the NOT image 
bw = ~img2; 
% find individual regions 
cc = bwconncomp(bw); 
% find area for each black region 
props = regionprops(cc,{'Area','Centroid'}); 
regionArea = [props(:).Area]; 
% find area of total black region 
totalArea = bwarea(bw); 
% plotting 
for ii = find(regionArea > 100) 
    c = props(ii).Centroid; 
    text(c(1),c(2),num2str(regionArea(ii)),'Color','b',... 
     'HorizontalAlignment','center','VerticalAlignment','middle',... 
     'FontWeight','bold'); 
end 

enter image description here

+0

Thanks.will試試! – Allan

+0

非常感謝@ user2999345它的工作。 – Allan