how to find the radius of a circle from an image
조회 수: 35 (최근 30일)
이전 댓글 표시
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
채택된 답변
Image Analyst
2023년 3월 1일
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
Image Analyst
2023년 3월 1일
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.
추가 답변 (2개)
Aditya
2023년 2월 28일
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
Image Analyst
2023년 2월 28일
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
Image Analyst
2023년 3월 1일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!