How to crop a detected face and face parts and save them as separate images?

22 views (last 30 days)
I am using the detectfacepart code of the following link ( http://www.mathworks.in/matlabcentral/fileexchange/36855-face-parts-detection) for detecting the face and face parts. How can I crop the detected face and face parts and save them as separate images?
  1 Comment
Dibya
Dibya on 22 Apr 2013
Sorry but I could do it myself.
For example;
leye = imcrop(img, bbox(:, 5: 8));
figure;imshow(leye);
imwrite(leye,'leye.jpg');

Sign in to comment.

Answers (3)

Adnan Rasool
Adnan Rasool on 9 Aug 2016
Edited: Image Analyst on 22 Sep 2017
Asslam o Alikum
Here is the code for
  1. Select image from a Location
  2. Detect faces in the selected image
  3. Find the Location of Detected Faces
  4. Crop all detected faces on one Figure
%Code start
clc;
[filename, folder] = uigetfile ({'*.jpg';'*.bmp';'*.png'}, 'File Selector');
fullFileName = fullfile(folder, filename);
img = imread(fullFileName);
figure(1);
imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7 ;
BB = step(FaceDetect, img);
figure(2);
imshow(img);
for i = 1 : size(BB,1)
rectangle('Position', BB(i,:), 'LineWidth', 3, 'LineStyle', '-', 'EdgeColor', 'r');
end
for i = 1 : size(BB, 1)
J = imcrop(img, BB(i, :));
figure(3);
subplot(6, 6, i);
imshow(J);
end
%Code End
If this Helped: Remember me in your Prayers
Thanks
  6 Comments
Sidra  Ashraf
Sidra Ashraf on 8 Jan 2020
may this code helpful formore than 1 images???
means ihave todetect face for almost thousands images. is thiswork in loop??

Sign in to comment.


Muluken  Birara
Muluken Birara on 12 May 2017
i want to proposed to work lip detection from image sequence , but know i cant preprocessed several image file
  • how to detect multi face image using loop
  • how to crop then and stror new dir for farther processing

Image Analyst
Image Analyst on 22 Apr 2013
Make sure you're passing [leftColumn, topRow, numberOfColumns, numberOfRows] as the second argument to imcrop. I'm not sure what you're passing. If you pass the right thing, it will work.
  2 Comments
Dibya
Dibya on 22 Apr 2013
Thanks for your response. Yes,the bbox(:,5:8) contains the bounding box i.e (xmin,ymin,width,height)for left eye in that example.
Image Analyst
Image Analyst on 22 Apr 2013
I don't think it does, because you have : which means "all rows" so there is likely more than 4 numbers there. Try this before you call imcrop():
croppingRectangle = bbox(:, 5:8) % No semicolon
whos croppingRectangle
and tell us what it says.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!