C++ OPENCV 4.3.0 x64 为什么 face_cascade 会抛出错误?

C++ OPENCV 4.3.0 x64 Why face_cascade throws error?

我正在使用 OpenCV 将人脸识别功能添加到我的 C++ 程序中。我以前从未使用过它,而且我似乎无法使用面部识别的级联功能。我想知道他们是否对新版本中的 FLAGS 进行了一些更改。我可以显示图像,但是当涉及到级联时,它总是会引发错误。谁能告诉我我错过了什么?

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc.hpp>
    #include <stdio.h>
    #include <iostream>
    #include <opencv2\objdetect.hpp>

    using namespace std;
    using namespace cv;

    String face_cascade_name  = "sources/data/haarcascades/haarcascade_frontalface_default.xml";
    String eyes_cascade_name  = "sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
    String smile_cascade_name = "sources/data/haarcascades/haarcascade_smile.xml";

    CascadeClassifier face_cascade;
    CascadeClassifier eyes_cascade;
    CascadeClassifier smile_cascade;

    string window_name = "Capture - Face detection";
    RNG rng(12345);

    int main()
    {
        //-- 1. Load the cascades
        if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading file 1\n"); return -1; };
        if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading file 2\n"); return -1; };
        if (!smile_cascade.load(smile_cascade_name)) { printf("--(!)Error loading file 3\n"); return -1; };

        std::string image_path = samples::findFile("test.jpg");
        Mat img = imread(image_path, IMREAD_COLOR);
        Mat img_gry;

        if (img.empty())
        {
            std::cout << "Could not read the image: " << image_path << std::endl;
            return 1;
        }

        imshow("Display window", img);

        // Detect faces
        std::vector<Rect> faces;
        cvtColor(img, img_gry, COLOR_BGR2GRAY);
        equalizeHist(img, img_gry);

        //I GET ERROR HERE
        face_cascade.detectMultiScale(img_gry, faces, 1.1, 2, CASCADE_SCALE_IMAGE, Size(30, 30), Size(130, 130)); //I GET ERROR HERE

/*...REST WILL BE PARSING faces...*/

        int g_key = waitKey(0); // Wait for a keystroke in the window

        if (g_key == 's')
        {
            imwrite("starry_night.png", img); //save image in same path
        }

        return 0;
    }

您是否尝试过使用类似的东西:

face_cascade.detectMultiScale(img_gry, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));

我将代码修改为:

/*...
OTHER CODE
...*/
std::vector<Rect> faces;
cvtColor(img, img_gry, COLOR_BGR2GRAY);
equalizeHist(img_gry, img_gry);
face_cascade.detectMultiScale(img_gry, faces);

for (size_t i = 0; i < faces.size(); i++)
{
       /*...PARSE...*/
}

imshow("Display window", img);

如@SourceCode 所述:

equalizeHist( smallImg, smallImg); //my variable is img_gry instead.

而且,我修改了:

face_cascade.detectMultiScale(img_gry, faces, 1.1, 2, CASCADE_SCALE_IMAGE, Size(30, 30), Size(130, 130));

face_cascade.detectMultiScale(img_gry, faces);

否则在我的情况下它不起作用。

来源如果有帮助:https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html