How to find area of holes in binary image?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Konstantin
le 12 Mai 2014
Commenté : Konstantin
le 12 Mai 2014
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 385x875 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!
0 commentaires
Réponse acceptée
Image Analyst
le 12 Mai 2014
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.
3 commentaires
Image Analyst
le 12 Mai 2014
How about
holeArea = numel(BW) - bwarea(BW);
If you want whole pixels as the area instead of how bwarea does it (which counts partial pixels), then
BW = imclearborder(~BW);
holeArea = sum(BW(:));
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
