How to find overlapping connected components

£可爱£侵袭症+ 提交于 2020-01-06 15:13:41

问题


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.

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

回答1:


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')


来源:https://stackoverflow.com/questions/39039684/how-to-find-overlapping-connected-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!