-2

I want to crop a face section from an image but face image is not straight/vertically aligned. I am having four pixel points to crop it.. Problem is that, If i will transform image first the pixel points cannot be used thereafter to crop the facial section out of it. Or in other case I am not having an exact bounding box to crop the image directly using imcrop as facial sections are somewhat tilted left or right. The four pixel points are at forehead , chin and ears of the face to be cropped.

1 Answers1

2

You should look at poly2mask. This function produces a mask image from your given x and y coordinates:

BW = poly2mask(x,y,m,n);

where x and y are your coordinates, and the produced BW image is m by n. You can then use this BW image to mask your original image I by doing

I(~BW) = 0;

If you actually want to crop, then you could get the bounding box (either through the regionprops function or the code below):

x1 =  round(min(x));
y1 =  round(min(y));
x2 =  round(max(x));
y2 =  round(max(y));

and then crop the image after you have used the BW as a mask.

I2 = I(x1:x2,y1:y2);

Hope that helps.

cwissy
  • 513
  • 3
  • 9