how to find the radius of a circle from an image

36 views (last 30 days)
I want to fit a circle just like the black line drawn in the figure and trying to find its radius. below is the code i am writing but not getting anything significant. kindly help me how to approch this kind of problems.
clear all
clc
% read the image
img = imread('gra.png');
% convert the image to grayscale
img_gray = rgb2gray(img);
% perform edge detection using the Canny algorithm
edge_img = edge(img_gray, 'sobel');
% perform hough transform to detect circles
[centers, radii] = imfindcircles(edge_img, [10 50000],'ObjectPolarity','bright','Sensitivity', 0.95);
% find the largest circle
[max_r, max_i] = max(radii);
max_center = centers(max_i, :);
% plot the results
figure;
imshow(img);
hold on;
viscircles(max_center, max_r, 'EdgeColor', 'b');

Accepted Answer

Image Analyst
Image Analyst on 1 Mar 2023
OK, not sure why you don't want to use John's program but I will. Here is my code:
% Initialization steps.
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 = 20;
% Read the image
rgbImage = imread('gra.png');
% Display the image.
subplot(2, 1, 1);
imshow(rgbImage);
title('Original Image', 'FontSize', fontSize);
drawnow;
% Create a mask.
grayImage = rgb2gray(rgbImage);
mask = grayImage < 255;
% Display the image.
subplot(2, 1, 2);
imshow(mask);
title('Mask Image', 'FontSize', fontSize);
drawnow;
% Get coordinates of all white pixels in the mask.
[y, x] = find(mask);
% Find minimum bounding circle's center and radius.
[center, radius] = minboundcircle(x, y)
% Plot that circle over the image.
viscircles(center, radius, 'EdgeColor', 'b', 'LineWidth', 2);
and here is the result:
  4 Comments
Image Analyst
Image Analyst on 1 Mar 2023
Evidently if you did download the minboundcircle code from
you didn't extract it to a folder and add it to the path. Please do so.

Sign in to comment.

More Answers (2)

Aditya
Aditya on 28 Feb 2023
Hi,
I think you can reduce/simplify the problem statement to finding the enclosing circle around a collection of points.
To solve this problem, you can try out multiple algorithms depending on your requirements:
  1. The simplest could be to find the 2D Bounding box of the points. The radius of the enclosing circle would be the diagonal of the bounding box.
  2. The above solution, however, would not guarantee you the tightest/minimum enclosing circle. You can find the minimum enclosing circle using Welzl's Algorithm. You can read more about the Smallest Circle problem.

Image Analyst
Image Analyst on 28 Feb 2023
Please attach 'gra.png' so we can run your code. So I see small red circles and a large black circle. Do you have the coordinates of the centers of the red circles? If you want the min bounding circle you can use
Otherwise if you want to fit just some of the more "outer" red circles then you need to somehow define the radius that defines ones you want to include in the fit and those inner ones that you want to exclude from the fit.
  6 Comments
Devargya chakraborty
Devargya chakraborty on 28 Feb 2023
it is very difficult to quantify tbh. you can say it is just like fitting a curve in a large set of data, some points will never follow the trend. in this case you can say i will start drawing a circle with bigger radius until its cover most of the atoms (here atoms being the red and white circle).
again i am saying most because some atoms will obviously go outside the circle. my job is to minimize the number of atoms going outside the circle and to find its final radius.
Image Analyst
Image Analyst on 1 Mar 2023
If you want to minimize the number of atoms outside the circle then you want the min bounding circle so that the number outside will be zero. For this you can use John's function in the link I gave you. Will you try it? Just pass your max_center array into his function. Or if you want no part outside, then pass in the coordinates of your binary image you get with find() into his function.

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!