MATLAB: I have to find diameters of the circles in given image and count circles, given any random point which is inside the circles

circle

I have an image which contains circles. i have to count the number of circles in the image. also if given co-ordinates of any random point inside these circles, i have to find the diameter of each of these circles using the given point only. i have written the code to convert the image to binary which is as follows:
clc;clear all;close all;
img=imread('H:\BIOMEDICAL\img.jpg');
figure,imshow(img);
gimg = min( img, [], 3 );
figure, imshow(gimg);
BW = im2bw( gimg, .5 );
figure,imshow(BW);
I think after taking the co-ordinates of the point inside the circle, i have to draw straight lines from this point up to the boundary of the circle, by checking whether the pixel is black or white, as the pixels inside the circle are shown as black in the binary image. i have to detect the small circles which are shown in the binary image.
Then by joining these points on the boundary i have to detect the edge of the circle and then count the number of circles. and lastly i have to give output as the number of circles detected along with their respective diameters. i don't know how to implement this code. can anyone help me please?

Best Answer

You would be better off doing color segmentation to get the circles, but if you get the threshold right, you can do it like you tried:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
% Read in image
img=imread('img.jpg');
subplot(3, 3, 1);
imshow(img);
title('Original Color Image', 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
grayImage = min( img, [], 3 );
subplot(3, 3, 2);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(3, 3, 3);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold and invert
% BW = ~im2bw( grayImage, .5 );
BW = grayImage < 170;
subplot(3, 3, 4);
imshow(BW);
title('Initial Binary Image', 'FontSize', fontSize);
% Get rid of border.
BW = imclearborder(BW);
% Get rid of small blobs
BW = bwareaopen(BW, 400);
subplot(3, 3, 5);
imshow(BW);
% Identify individual blobs by seeing which pixels are connected to each other.
% Each group of connected pixels will be given a label, a number, to identify it and distinguish it from the other blobs.
% Do connected components labeling with either bwlabel() or bwconncomp().
labeledImage = bwlabel(BW, 8); % Label each blob so we can make measurements of it
% labeledImage is an integer-valued image where all pixels in the blobs have values of 1, or 2, or 3, or ... etc.
subplot(3, 3, 6);
imshow(labeledImage, []); % Show the gray scale image.
title('Labeled Image, from bwlabel()', 'FontSize', fontSize);
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(3, 3, 7);
imshow(coloredLabels);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.

caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom, then from left to right.');
title(caption, 'FontSize', fontSize);
% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, 'Area', 'Perimeter', 'EulerNumber');
numberOfBlobs = size(blobMeasurements, 1);
allPerimeters = [blobMeasurements.Perimeter];
allAreas = [blobMeasurements.Area]
allEuler = [blobMeasurements.EulerNumber]; % Num regions - num holes in region.
circularities = allPerimeters .^ 2 ./ (4*pi*allAreas)
% Find blobs with circularities less than 2 and with no holes insdie them.
circularBlobIndexes = find(circularities < 2)
% Extract circular blobs
circlesImage = ismember(labeledImage, circularBlobIndexes);
subplot(3, 3, 8);
imshow(circlesImage);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
caption = sprintf('Only the circular blobs.');
title(caption, 'FontSize', fontSize);
% Remeasure with the new, final binary image composed of only circles
labeledImage = bwlabel(circlesImage);
blobMeasurements = regionprops(labeledImage, 'EquivDiameter');
allDiameters = [blobMeasurements.EquivDiameter]