MATLAB: How to crop a circle from an image

imaimage processing

hey, asking for the second time.
I need to find a circle in an image and crop that region of the circle to a new image, I already found a way to find the circle using "imfindcircle" fuction, but when I use that function it only marks the circle and i need to crop it somehow but I dont know what info i can use… thanks in advance

Best Answer

Try the following tutorial:
% Setup
clear
close all
clc
inputImg = imread('cameraman.tif');
% Show demo Image
hf = figure();
set(hf,'Units', 'Normalized', 'OuterPosition', [0 0 1 1])
subplot(1,2,1)
imshow(inputImg)
% Example of data output from findcircles with two circle outputs
centers = [50 50; 150 200];
radii = [20; 30];
% Draw circles on image (just for demo)
drawcircle('Center',centers(1,:),'Radius',radii(1))
drawcircle('Center',centers(2,:),'Radius',radii(2))
% Generate binary image (mask) for representing cropped regions
mask = false(size(inputImg));
for c = 1:numel(radii)
[colsInImg,rowsInImg] = meshgrid(1:size(inputImg,1),1:size(inputImg,2));
circlePixels = (rowsInImg - centers(c,2)).^2 ...
+ (colsInImg - centers(c,1)).^2 <= radii(c).^2;
mask(circlePixels) = true;
end
% Use mask to get cropped image
cropped = inputImg;
cropped(~mask) = false;
% Show results
subplot(1,2,2)
imshow(cropped)