2017-04-09 426 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