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 border
binaryImage = imclearborder(binaryImage);
% Count holes:
[~, numHoles] = bwlabel(binaryImage);
Related Question