How can I resize an image where the face should be

2019-09-04 14:55发布

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.

1条回答
Explosion°爆炸
2楼-- · 2019-09-04 15:42

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

查看更多
登录 后发表回答