mask = grayImage > 50;
props = regionprops(mask, 'Centroid', 'EquivDiameter', 'Area');
allDiameters = [props.EquivDiameter]
allAreas = [props.Area]
xy = vertcat(props.Centroid)
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
clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 16;
folder = pwd;
baseFileName = 'Cropping_initial.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(rgbImage);
if numberOfColorBands > 1
grayImage = rgbImage(:, :, 1);
else
grayImage = rgbImage;
end
subplot(2, 2, 1);
imshow(grayImage);
axis on;
impixelinfo;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
impixelinfo;
g = gcf;
g.WindowState = 'maximized';
g.NumberTitle = 'off';
g.Name = 'Demo by Image Analyst'
drawnow;
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);
threshold = 50;
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');
mask = grayImage > threshold;
mask = imclearborder(mask);
mask = bwareaopen(mask, 1000);
subplot(2, 2, 3);
imshow(mask);
title('Final Mask', 'FontSize', fontSize);
drawnow;
props = regionprops(mask, 'Centroid', 'EquivDiameter', 'Area');
allDiameters = [props.EquivDiameter]
allAreas = [props.Area]
xy = vertcat(props.Centroid)
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!');
2 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/723613-finding-the-radii-of-circles-knowing-the-center-of-those-circles#comment_1277833
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/723613-finding-the-radii-of-circles-knowing-the-center-of-those-circles#comment_1277833
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/723613-finding-the-radii-of-circles-knowing-the-center-of-those-circles#comment_1277918
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/723613-finding-the-radii-of-circles-knowing-the-center-of-those-circles#comment_1277918
Sign in to comment.