MATLAB: How to find area of holes in binary image

hole detectionimage analysis

Hi, I know this is pretty similar to a few questions before, but I am encountering a different problem not discussed so far.
The story: I have an image that i need to analyze for voids. All good an well and I successfully create the binary image (as seen from the screenshot). However, when I try to apply the imfill function on that picture to detect the holes, my script keeps on going. Keep in mind that original image is 385×875 px at 16 bit depth and the binary is at 2 bit…(doh..)
However, the script just keeps running and running with no sign of ever finishing. My task is to find the area (in pixels or mm) of the black region inside the white one. And having this problem is not much of a help to me.. Here's the script as well:
% start the timer
tic
% begin actual script
% loads image to matrix I
I = imread('scan0408.tif');
% convert the grayscale image to index image with 16-bit depth
[X, map] = gray2ind(I, 16);
% shows the new indexed image
imshow(X, map);
% convert the indexed image to a binary image with treshold of 0.38 (found
% to be the best treshold for these images)
BW = im2bw(X,map,0.38);
% show the binary image to compare to the original grayscale
imshow(X,map), figure, imshow(BW)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% takes the image and finds the locations of all the "holes". consequently,
% it fills the holes and prepares the image for eventual area calculation
BW2 = imfill(BW)
[BW2,locations] = imfill(BW)
BW2 = imfill(BW,locations)
BW2 = imfill(BW,'holes')
I2 = imfill(I)
BW2 = imfill(BW,locations,conn)
[gpuarrayI2]= imfill(gpuarrayI)
% end of actual script
% end the timer
toc
Thanks in advance!

Best Answer

You forgot to attach the image. I can look at it after that. Is it closed off on the right edge, because it's not obvious? If so, I'd probably try to invert the image, then call imclearborder and then sum the pixels.