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

对着背影说爱祢 提交于 2019-12-25 03:04:07

问题


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:


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:

Cropped face:

Resized face:



来源:https://stackoverflow.com/questions/31397902/how-can-i-resize-an-image-where-the-face-should-be-cropped-and-scaled-to-fit-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!