How to find the radii of circles, centers of circles,euclidean distance between each circle and angle among (small circle-->large circle -->small circle)?

2 views (last 30 days)
I have 3 circled masks and i want to find the radii , centers of the circles,euclidean distance between each circle and angle between small circle-->large circle -->small circle?
(PS:i have attached the original image)
Here is the code so far (Thanks to Mathworks and Image Analyst, where i followed their guidelines to get upto this point).
% Read the original image (1st convert from RGB to grey )
clear all;clc;
originalimage=rgb2gray(imread('new_whole_mask_P3290258.jpg'));
subplot(2,4,1);imshow(originalimage);
set(gcf,'units','normalized','outerposition',[0 0 1 1]);
drawnow;
caption=sprintf('original image showing\n1 large circle and 2 small circles.');
title(caption);
axis image;
% Obtain and display the histogram
[pixelCount,greyLevels]=imhist(originalimage);
subplot(2,4,2);
bar(pixelCount);
title('Histogram of original image');
xlim([0 greyLevels(end)]);
grid on;
% In order to get the binary, set the threshold value as 25
thresholdValue=25;
binaryImage=originalimage>thresholdValue;
% Show the threshold as a vertical red bar on the histogram
hold on;
maxYValue = ylim;
line([thresholdValue, thresholdValue], maxYValue, 'Color', 'r');
% Display the binary image
subplot(2, 4, 3);
imshow(binaryImage);
title('Binary Image, obtained by thresholding');
% Label each circle to make it feasibe for calculations
labeledImage = bwlabel(binaryImage, 8); % Label each circle
subplot(2,4,4);
imshow(labeledImage,[]);
title('Labeled Image,from bwlabel()');
% calculate the standard deviation of each region
s = regionprops(binaryImage,originalimage, {'Centroid','PixelValues','BoundingBox'});
subplot(2,4,5); imshow(originalimage);
title('Standard Deviation of Regions');
hold on
% compare the weighted with unweighted centroid locations superimpose the centroids
numObj = numel(s);
for k = 1 : numObj
s(k).StandardDeviation = std(double(s(k).PixelValues));
text(s(k).Centroid(1),s(k).Centroid(2), ...
sprintf('%2.1f', s(k).StandardDeviation), ...
'EdgeColor','b','Color','r');
end
hold off
% Assign pseudo random colors to each circle
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% pseudo random color labels
subplot(2, 4, 6);
imshow(coloredLabels);
axis image; % Make sure image is not artificially stretched
caption = sprintf('Pseudo colored labels, from label2rgb().\nCircles are numbered from top to bottom,\n then from left to right.');
title(caption);
% Get all the circle properties
circleMeasurements = regionprops(labeledImage, originalimage, 'all');
numberOfCircles = size(circleMeasurements, 1);
% Plot the borders of all the circles on the original grayscale image
subplot(2, 4, 7);
imshow(originalimage);
title('Outlines, from bwboundaries()');
axis image; % Make sure image is not artificially stretched
hold on;
% bwboundaries() returns a cell array, where each cell contains the row/column coor
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;
%Calculate properties of regions in the image and return the data in a table.
cal= regionprops('table',binaryImage,'Centroid','Area','FilledArea','Perimeter')
Below is the answer which i got:
cal =
Area Centroid FilledArea Perimeter
2866 679.763782274948 956.926378227495 2866 187.709
5122 837.71710269426 1122.0909800859 5122 258.353
1 814 1157 1 0
1 863 1157 1 0
2735 1060.16599634369 964.146983546618 2739 190.81
But this is not what i expect (as i have only 3 circles).
Kindly help me to figure this out.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Apr 2016
I do not have the toolbox to test with, so I cannot tell exactly what is happening. I would point out, though, that you are using a JPG file, so you are creating compression artifacts. Depending on the exact level you threshold at, you can end up with isolated around the circles, where due to the compression artifacts a value might happen to be just above the threshold while adjacent values are below the threshold.
With that particular image with a clean black background, you can use
binaryImage = any(originalimage,3);
which is to say that you can threshold against 0.
If you must use JPEG images then your algorithm needs to take into account that the values stored in the array might not be what you think you are seeing.
  8 Comments
Walter Roberson
Walter Roberson on 9 Apr 2016
You should look at either centdists or sq_centdists but not both.
The output of squareform would be expected in this situation to be a 3 x 3 matrix with 0 along the diagonal.
Unfortunately I do not have the toolboxes to test with so I cannot run the code.
Chathu
Chathu on 9 Apr 2016
Edited: Chathu on 9 Apr 2016
Thank you so much for the hint,Walter. Really appreciate it. (glad that i had the opportunity to vote, as your responses are quite helpful :) Thanks again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!