Leo.peis
Leo.peis

Reputation: 14323

MATLAB: how do I crop out a circle from an image

I need to crop a circle in MATLAB.

I need to perform iris segmentation, and I´ve identified the center point and the radius of the iris, and I need to cut it off from the image.

I have a vector ci that ci(1) is X-coordinate ci(2) is Y-coordinate and ci(3) is the radius of the circle.

Upvotes: 9

Views: 12316

Answers (1)

Jonas
Jonas

Reputation: 74940

One way to do this is to create a binary mask with ones inside the circle and zeros outside. You can then use this array to either mask everything outside the circle with NaNs, or to read the pixel values of the image inside the mask.

To create a circle mask, an easy way is to create coordinate arrays centered on the iris, and threshold the distance, like this:

[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = (xx.^2 + yy.^2)<ci(3)^2;

Upvotes: 10

Related Questions