0

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 300x300 where the face in the webcam images would fit the resolution 300x300.
I have identified the face in the webcam image using opencv face cascade classifiers.
But how can I crop that face to fit in the size of 300x300?
Please help with some code lines as I am new to opencv.

Himanshu
  • 4,327
  • 16
  • 31
  • 39
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • http://docs.opencv.org/master/d7/d8b/tutorial_py_face_detection.html gives you a bounding box of the face. Simply crop the interior of this box, rescale it to 300x300 pixels and apply your gender classification. – LSA Jul 14 '15 at 11:37

1 Answers1

1

Here a small sample that will help you to crop and resize your faces:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
     Mat3b img = imread("path_to_image");

    // You find the rectFace through face detection
    // Here the values are hardcoded
    Rect rectFace(235, 30, 45, 55);

    Mat3b detection = img.clone();
    rectangle(detection, rectFace, Scalar(0,255,0));

    // Crop the image
    Mat3b face(img(rectFace)); 

    // Resize the face to 300x300
    Mat3b resized;
    resize(face, resized, Size(300,300), 0.0, 0.0, INTER_LANCZOS4);

    // Apply gender classification on resized

    imshow("Detection", detection);
    imshow("Face", face);
    imshow("Resized", resized);
    waitKey();

    return 0;
}

Detected face:

enter image description here

Cropped face:

enter image description here

Resized face:

enter image description here

Miki
  • 40,887
  • 13
  • 123
  • 202