MATLAB: Counting holes in an image digital image processingimage processingImage Processing Toolbox writing an algorithm that will count the number of holes in the image that do not touch the border Best Answer Threshold then call imclearborder() and bwlabel(). Assuming you have dark holes on a bright background:% Binarize:binaryImage = grayImage < someValue;% Remove those touching borderbinaryImage = imclearborder(binaryImage);% Count holes:[~, numHoles] = bwlabel(binaryImage); Related SolutionsMATLAB: How can i separate all the connected regions in a binary image and display them individually. Use bwlabel() and ismember[labeledImage, numRegions] = bwlabel(binaryImage);props = regionprops(labeledImage, 'Area')% Compute all the areas.allAreas = [props.Area]% Display each blob in its own figure.for k = 1 : numRegions thisRegion = ismember(labeledImage, k); figure; imshow(thisRegion); drawnow;end MATLAB: Removing black holes from within a white circles You have white stuff around the border of the image, which means that everything will get filled. So call imclearborder firstbinaryImage = imclearborder(binaryImage);binaryImage = imfill(binaryImage, 'holes');If you need to put the white border back, let me know. Related QuestionNumber of Object in binary imageRemoving regions with holesFill holes in binary Image
Best Answer