Ask Your Question
0

How can I resize an image where the face should be cropped and scaled to fit the size?

asked 2015-07-13 23:50:31 -0600

mayooran gravatar image

My webcam takes images. But opencv gender classification needs the images to be of the same size of that of the images used to train. So I need my webcam images to be 300300 where the face in the webcam images would fit the resolution 300300. I have identified the face in the webcam image using opencv face cascade classifiers. But how can I crop that facce to fit in the size of 300*300? Please help with some code lines.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-07-14 01:15:44 -0600

berak gravatar image
//
// use a cascade classifier to find the face region:
//
cv::String cascade_path("E:\\code\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
cv::CascadeClassifier cascade;
bool ok = cascade.load(cascade_path);

Mat gray = ....; // grayscale input image
vector<Rect> faces;
cascade.detectMultiScale(gray, faces, 1.2, 3);

// 
// if it found something, resize the found region:
//
if (faces.size() > 0)
{
     Rect roi = faces[0];
     Mat cropped;
     resize(gray(roi), cropped, Size(300,300));
     imwrite("cropped.png", cropped);
}
edit flag offensive delete link more

Comments

Is it possible to do this without making the image gray as well?

mayooran gravatar imagemayooran ( 2015-07-14 08:27:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-13 23:50:31 -0600

Seen: 1,056 times

Last updated: Jul 14 '15