Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Cropping an ellipse from an image

Date: 2023-01-05 14:42:48

I want to extract an elliptical region from an image (a portion of a face portion from an image) preferably in MATLAB: 

For example, in this image, I want to extract the region within red boundary.
Can anyone help me with this ? 

 Answers:

Cropping is easy, all you have to do is apply a proper mask. The trick is to create such a mask.

Assuming A is your image, try this: 

 

%# Create an ellipse shaped mask
c = fix(size(A) / 2);   %# Ellipse center point (y, x)
r_sq = [76, 100] .^ 2;  %# Ellipse radii squared (y-axis, x-axis)
[X, Y] = meshgrid(1:size(A, 2), 1:size(A, 1));
ellipse_mask = (r_sq(2) * (X - c(2)) .^ 2 + ...
    r_sq(1) * (Y - c(1)) .^ 2 <= prod(r_sq));

 

%# Apply the mask to the image
A_cropped = bsxfun(@times, A, uint8(ellipse_mask));

The cropped image will be stored in A_cropped. Play with the coordinates of the center and the values of the radii until you get the desired result.

EDIT: I extended the solution for RGB images (if matrix A is 3-D).


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com