how to find the radius of a circle from an image
35 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Devargya chakraborty
el 27 de Feb. de 2023
Comentada: Image Analyst
el 1 de Mzo. de 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 comentarios
Respuesta aceptada
Image Analyst
el 1 de Mzo. de 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 comentarios
Image Analyst
el 1 de Mzo. de 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.
Más respuestas (2)
Aditya
el 28 de Feb. de 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 comentarios
Image Analyst
el 28 de Feb. de 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 comentarios
Image Analyst
el 1 de Mzo. de 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.
Ver también
Categorías
Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!