How to find the circle in image?

1 vue (au cours des 30 derniers jours)
Mohsina Zafar
Mohsina Zafar le 4 Déc 2017
Commenté : Mohsina Zafar le 9 Déc 2017
Hello,
I have an image and I am unable to extract the circle in it. I have used all the methods including regionprops, imfindcircles and much more.
Can you kindly tell how I can make the whole circular region white and the rest of the image black?
Best Regards.

Réponses (1)

KSSV
KSSV le 5 Déc 2017
YOu can try the below code. I have attached the result image and the function used. YOu can reduce the Radius the radius of circle, if you want.
I = imread('image.png') ;
I = rgb2gray(I) ;
figure(1)
imshow(I)
%%GEt the white regions
[y,x] = find(I) ;
%%Fit a circle
[xc,yc,R,a] = circfit(x,y) ;
%%If you want reduce the Radius by some fraction
% R = R-15 ;
th = linspace(0,2*pi) ;
xi = xc+R*cos(th) ; yi = yc+R*sin(th) ;
%%Get indices lying inside the circle
[ny,nx] = size(I) ;
[X,Y] = meshgrid(1:nx,1:ny) ;
idx = inpolygon(X(:),Y(:),xi,yi) ;
I(idx) = 255 ;
figure(2)
imshow(I)
Result:
  1 commentaire
Mohsina Zafar
Mohsina Zafar le 9 Déc 2017
Thank you for your answer. But I want the rest of the image to be black (all that is not a circle).

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by