Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do circular crop using matlab?

I usually use imcrop to crop rectanguler image but i wat to create circular crop. How to do it?

I know there is another quetion about this, here the link:

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

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

    and the result of imshow(mask) is rectangle with white background

  2. Cropping Circular region of interest around a point in MATLAB

    error in roimaskcc

  3. http://www.mathworks.com/matlabcentral/newsreader/view_thread/242489

    [xx,yy]=ndgrid(1:size(X,1), 1:size(X,2));
    CroppingMask= ( (xx-Xcenter).^2+(yy-Ycenter).^2<=Radius^2 );
    X=X.*CroppingMask;
    

    The result of imshow(CroppingMask) is a rectangle with black background with small white circular at the center of the rectangle. When I run line 3 it shows an error.

Please help me, step by step because I'm a beginner.

Here is my image: https://www.dropbox.com/s/5plqzqgyb1ej6gh/patricia.jpg. It resolution is 480x640.

like image 885
Aep Saepudin Avatar asked Nov 15 '13 01:11

Aep Saepudin


People also ask

How do I crop part of an image in Matlab?

Crop the image. Double-click the left mouse button or right-click inside the boundary of the crop rectangle and select Crop Image from the context menu. Retrieve the coordinates of the crop rectangle. Right-click inside the boundary of the crop rectangle and select Copy Position from the context menu.

Which function in Matlab is used to crop images?

To extract a rectangular portion of an image, use the imcrop function. Using imcrop , you can specify the crop region interactively using the mouse or programmatically by specifying the size and position of the crop region.


1 Answers

Solution (1) works perfectly well. Here is a complete working example using your image.

I = imread('patricia.jpg');
imageSize = size(I);
ci = [250, 300, 100];     % center and radius of circle ([c_row, c_col, r])
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = uint8((xx.^2 + yy.^2)<ci(3)^2);
croppedImage = uint8(zeros(size(I)));
croppedImage(:,:,1) = I(:,:,1).*mask;
croppedImage(:,:,2) = I(:,:,2).*mask;
croppedImage(:,:,3) = I(:,:,3).*mask;
imshow(croppedImage);

It produces the following image.

croppedImage

I hope this clarifies things. There is probably a better way of re-assembling the cropped image, but this was what I could come up with off the top of my head.

like image 197
mags Avatar answered Oct 13 '22 22:10

mags