如何查找重叠的连接组件 [英] How to find overlapping connected components
问题描述
我有两个单独的图像:第一个图像仅包含黑色的圆形物体,而第二个图像仅包含绿色的圆形物体.我正在尝试编写一个代码,以找出黑色对象中有多少绿色.重叠图像1(仅具有黑色对象的图像)和图像2(仅具有绿色对象的图像)时,可能会发生三种不同的情况.
I have two separate images: the first image only contains the black round objects, while the second image only contains the green round objects. I am trying to make a code that will figure out how much of the green is in the black objects. There are three different scenarios that could happen when overlaying image 1 (image with black objects only) and image 2 (image with green objects only) as shown in the image below.
我尝试使用regionprops
提取黑色和绿色对象的像素索引,并使用ismember
检查是否有重叠的像素.每个像元包含单个对象的像素索引.我觉得我在代码中做错了什么,但我似乎无法理解它的确切含义.我敢肯定,有一种更简单的方法可以做到这一点.
I have tried to extract the pixel indices of the black and green objects using regionprops
and check whether if there are any overlapping pixels using ismember
. Each cell contains the pixel indices of the a single object. I feel that there is something I'm doing wrong in the code but I can't seem to grasp what it is exactly. I'm sure that there is a simpler method to do this.
blackProperties = regionprops(logical(blackImage),'all');
greenProperties = regionprops(logical(greenImage),'all');
numBlackObjects = length(blackProperties);
numGreenObjects = length(greenProperties);
blackPixels = cell(1,numBlackObjects);
greenPixels = cell(1,numGreenObjects);
for j = 1:numBlackObjects
blackPixels{j} = blackProperties(j).PixelIdxList;
end
for j = 1:numGreenObjects
greenPixels{j} = greenProperties(j).PixelIdxList;
end
matchMem = zeros(100,2);
for j = 1:numel(blackPixels)
blackPix = blackPixels{j};
for i = 1:numel(greenPixels)
greenPix = greenPixels{i};
match = ismember(blackPix,greenPix);
matchMem(match,1) = find(match);
matchMem(match,2) = i;
end
end
推荐答案
您可以对绿色和黑色图像进行逻辑与(&
),以告诉您它们重叠的位置.然后,如果需要连接的组件,则可以在生成的重叠图像上调用regionprops
.
You can just do a logical AND (&
) with the green and black images which will tell you where they overlap. Then if you need connected components, you can call regionprops
on the resulting overlap image.
green_and_black = blackImage & greenImage;
overlap_props = regionprops(green_and_black, 'all')
这篇关于如何查找重叠的连接组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!