MATLAB: I’m trying to crop a circle from an image using imfindcircle… but It deosn’t work. what is the issue

cropimfindcircle

Here is my code
imfindcircle can find the circle(traffic sign) but the mask to crop it , is not functioning
clc;
close all;
image = imread('111.jpg');
hsv = rgb2hsv(image);
h= hsv(:,:,1);
s= hsv(:,:,2);
maskh = (h <= 0.09) | (h >= 0.9);
masks = s >= 0.2 ;
redmask = uint8(maskh & masks) ;
redmasko = bwareaopen(redmask,50);
[centers,radii,metric] = imfindcircles(redmasko,[20 50]);
h = viscircles(centers,radii);
imagesize= size(image);
[xx,yy] = ndgrid((1:imagesize(1))-centers(1),(1:imagesize(2))-centers(2));
mask = uint8((xx.^2 + yy.^2)<(radii^2));
croppedImage = uint8(zeros(size(image)));
croppedImage(:,:,1) = image(:,:,1).*mask;
croppedImage(:,:,2) = image(:,:,2).*mask;
croppedImage(:,:,3) = image(:,:,3).*mask;
figure ; imshow(croppedImage);

Best Answer

Swap the coordinates in the output of imfindcircles.
[xx,yy] = ndgrid((1:imagesize(1))-centers(2),(1:imagesize(2))-centers(1));