MATLAB: Finding the radii of circles knowing the center of those circles

MATLABradii detection

I have a 2D hologram which has 25 circles of diffrent radii. I have removed the noise and binarized the image and then I found the centre points of the circles.
Now, i need to finally find the radii of those circles without using advanced functions

Best Answer

You can do this:
mask = grayImage > 50; % Or whatever
props = regionprops(mask, 'Centroid', 'EquivDiameter', 'Area');
allDiameters = [props.EquivDiameter]
allAreas = [props.Area]
xy = vertcat(props.Centroid)
% Plot centroid over the image.

for k = 1 : length(props)
txt = sprintf('#%d at (x, y)\n= (%.1f, %.1f)', k, xy(k, 1), xy(k, 2));
text(xy(k, 1), xy(k, 2), txt, 'Color', 'r', 'FontSize', 8, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end
Below, and attached, is the full demo:
% Code to find the centroids of spots.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================



% Read in gray scale image.
folder = pwd;
baseFileName = 'Cropping_initial.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 = 1.
[rows, columns, numberOfColorBands] = size(rgbImage);
% If it's RGB instead of grayscale, convert it to gray scale.
if numberOfColorBands > 1
grayImage = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage;
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
axis on;
impixelinfo; % Let user mouse around and see gray level.
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
impixelinfo;
% Enlarge figure to full screen.
g = gcf;
g.WindowState = 'maximized';
g.NumberTitle = 'off';
g.Name = 'Demo by Image Analyst'
drawnow;
%===============================================================================
% Display the histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Original Gray Scale Image Histogram', 'FontSize', fontSize);
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
%===============================================================================
% IMAGE SEGMENTATION
% Specify a threshold. The image is 16 bits in the range 0-65535 so the threshold will be high.
threshold = 50;
% Place line on histogram at the mean.
xline(threshold, 'Color', 'r', 'LineWidth', 2);
yl = ylim;
txt = sprintf(' Threshold = %d', threshold);
text(threshold, yl(1) + 0.76 * (yl(2)-yl(1)), txt, 'Color', 'r', 'FontSize', 18, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
% Create a binary image
mask = grayImage > threshold;
% Get rid of white surround or any blob touching the border (because it's not a complete blob).
mask = imclearborder(mask);
% Get rid of any blobs less than 1000 in area (there is one blob now that has an area of 1 and the rest are around 7000-9000).
mask = bwareaopen(mask, 1000);
subplot(2, 2, 3);
imshow(mask);
title('Final Mask', 'FontSize', fontSize);
drawnow;
%===============================================================================
% IMAGE ANALYSIS
props = regionprops(mask, 'Centroid', 'EquivDiameter', 'Area');
allDiameters = [props.EquivDiameter]
allAreas = [props.Area]
xy = vertcat(props.Centroid)
% Plot centroid over the image.
for k = 1 : length(props)
txt = sprintf('#%d at (x, y)\n= (%.1f, %.1f)', k, xy(k, 1), xy(k, 2));
text(xy(k, 1), xy(k, 2), txt, 'Color', 'r', 'FontSize', 8, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end
msgbox('Done! Thank you Image Analyst!');