Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 235
Leo.peis Avatar asked Jan 10 '11 21:01

Leo.peis


People also ask

How do I crop part of an image in Matlab?

Using the mouse, draw a rectangle over the portion of the image that you want to crop. Perform the crop operation by double-clicking in the crop rectangle or selecting Crop Image on the context menu. The cropped image appears in the figure window. The Crop Image tool returns the cropped area in the return variable, J .


1 Answers

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;
like image 170
Jonas Avatar answered Oct 21 '22 02:10

Jonas