How to detect circle in this pic?

1 view (last 30 days)
SUNGDEOK KIM
SUNGDEOK KIM on 5 Dec 2019
Answered: Shashank Gupta on 9 Dec 2019
I want detect every circle in this pic using this code. But, I detect only four.
so, How to detect every circle in this pic? what's wrong my code?
clear
clc
image = imread('example5.PNG');
image = edge(imgaussfilt(rgb2gray(image), 2), 'Canny');
[x y] = find(image == 1);
pindex = cat(2, y, x);
hough_matrix = zeros(1000, 1000);
rad = [55 56 64 65 51 52];
pindex = pindex + 245;
for k = 1 : 7
for p=1:size(pindex, 1)
center = pindex(p, :);
% 모든 허프 matrix에 대한 이중 for문
for i = center(1)-rad(k):center(1)+rad(k)
x = i - center(1);
for j = center(2)-rad(k):center(2)+rad(k)
y = j - center(2);
if round(sqrt(x*x + y*y)) == rad(k)
hough_matrix(j, i) = hough_matrix(j, i) + 1;
end
end
end
end
[x y] = find(hough_matrix == max(hough_matrix, [], 'all'));
center = cat(2, y, x);
center = center - 245;
if k==1
figure;imshow(image);
else
viscircles(center, rad(k));
end
end

Answers (1)

Shashank Gupta
Shashank Gupta on 9 Dec 2019
Hi Kim,
Looks like you are trying to implement a Hough transform algorithm from the scratch. MATLAB already has the function “imfindcircles” which detect the circle in the image given a range of radius. Also this function uses the same Hough transform algorithm. But before using the function you need to do some pre-processing, I am following up your code to find the edge, now use this edged image to fill up the holes and now you can use “imfindcircle” function. This might help you.
I am also attaching a snippet of the code for the reference
image = imread('image.png');
image = edge(imgaussfilt(rgb2gray(image), 2), 'Canny');
img = imfill(image,'holes');
imshow(img);

Tags

Community Treasure Hunt

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

Start Hunting!