MATLAB: I have 12 objects and only 10 objects have a yellow dot inside it .How to extract the ids of the objects with only the yellow dot

Image Processing Toolboximage segmentation

The yellow dot is not at the centeroid of the object. I have created a yellow mask for each of the objects, but i am struggling to create a for loop to extract the ids of the objects with only the yellow dot inside of it

Best Answer

Segment the objects. Then segment the yellow dots. Then label the objects and use that labeled image to get a list of blobs that have the dot, and call unique. Then call ismember() to get the mask with only the yellow dots
% Fill yellow holes in the objects.
objectMask(yellowHoleMask) = true;
% Fill the objects in case the yellow was in a "lake" in the blob and didn't actually touch the blob itself.
objectMask = imfill(objectMask, 'holes');
% Label each blob.
[labeledObjects, numBlobs] = bwlabel(objectMask);
% Find labels with yellow dots in them
indexesWithYellow = unique(labeledObjects(yellowHoleMask))
% Extract only those blobs that have a yellow dot in them
objectsWithYellowInside = ismember(labeledObjects, indexesWithYellow); % Creates a binary image
If you want the original blobs without the yellow spot being filled in them (so they'll have a hole in them) then you'll need a slight modification, as this code will give filled blobs where the yellow and containing blob are all one blob. If that's the case, then supply your two binary images.