# how to find the radius of a circle from an image

104 views (last 30 days)

Show older comments

Devargya chakraborty
on 27 Feb 2023

Commented: Image Analyst
on 1 Mar 2023

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');

##### 0 Comments

### Accepted Answer

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
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.

### More Answers (2)

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:

- 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.
- 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.

You can check out implementations at File exchange: Exact minimum bounding spheres and circles - File Exchange - MATLAB Central (mathworks.com)

##### 0 Comments

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

Image Analyst
on 1 Mar 2023

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!