how to get size of holes in an object (image processing)

12 views (last 30 days)
I have several images like this with a lot more noise though.
I can get outside boundaries and parameters following this example http://www.mathworks.in/help/images/examples/identifying-round-objects.html but how do I get the size (dia) of internal holes? Also when I reduce the noise using a structural element it considerably reduces the size of holes. How do I deal with this?

Answers (1)

Image Analyst
Image Analyst on 21 Apr 2014
I hope this wasn't your homework because it was so simple I did it all in about 3 minutes:
% Demo to find equivalent circular diameters.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 22;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\SG\Documents\Temporary';
baseFileName = 'nuts.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get a binary image
binaryImage = rgbImage(:,:,2) < 100;
% Get rid of background.
binaryImage = imclearborder(binaryImage);
% Get rid of small noise regions.
binaryImage = bwareaopen(binaryImage, 500);
subplot(2, 2, 2);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize);
% Label the parts
labeledImage = bwlabel(binaryImage);
% Measure the equivalent circular diameter
measurements = regionprops(labeledImage, 'EquivDiameter');
allDiameters = [measurements.EquivDiameter]
The answer is in the command window and gives the diameters in pixels:
allDiameters =
66.8416 67.5050 54.6070 67.4012
Adapt as needed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!