仅CascadeClassifier产生未定义的引用错误

时间:2018-10-13 15:41:06

标签: c++ opencv qt-creator

这是Qt pro文件的libs部分

LIBS += -LD:/opencv-build/bin/ -llibopencv_core320
LIBS += -LD:/opencv-build/bin/ -llibopencv_highgui320
LIBS += -LD:/opencv-build/bin/ -llibopencv_imgcodecs320
LIBS += -LD:/opencv-build/bin/ -llibopencv_imgproc320
LIBS += -LD:/opencv-build/bin/ -llibopencv_features2d320
LIBS += -LD:/opencv-build/bin/ -llibopencv_calib3d320
LIBS += -LD:/opencv-build/bin/ -llibopencv_videoio320
LIBS += -LD:/opencv-build/bin/ -llibopencv_video320

这是代码(唯一使用CascadeClassifier的部分)。

cv::String faceCascade = ":/resources/haarcascade_frontalface_alt.xml";
cv::String eyeCascade = ":/resources/haarcascade_eye.xml";
CascadeClassifier faceClassifier;
CascadeClassifier eyeClassifier;

Mat Tracker::detectEyes(Mat& img){
    Mat gray;
    Mat result;
    faceClassifier.load(faceCascade);
    eyeClassifier.load(eyeCascade);
    cvtColor(img, gray, CV_BGR2GRAY);
    std::vector<Rect> face_pos;
    faceClassifier.detectMultiScale(gray, face_pos, 1.1, 3, 0 | CV_HAAR_SCALE_IMAGE, Size(10, 10));
    for (int i = 0; i < static_cast<int>(face_pos.size()); i++)    {
        rectangle(result, face_pos[i], Scalar(0, 255, 0), 2);
    }
    for (int i = 0; i < static_cast<int>(face_pos.size()); i++) {
        std::vector<Rect> eye_pos;
        Mat roi = gray(face_pos[i]);
        eyeClassifier.detectMultiScale(roi, eye_pos, 1.1, 3, 0 | CV_HAAR_SCALE_IMAGE, Size(10, 10));
        for (int j = 0; j < static_cast<int>(face_pos.size()); j++) {
            Point center(face_pos[i].x + eye_pos[j].x + (eye_pos[j].width / 2),
                       face_pos[i].y + eye_pos[j].y + (eye_pos[j].height / 2));
            int radius = cvRound((eye_pos[j].width + eye_pos[j].height) * 0.2);
            circle(result, center, radius, Scalar(0, 0, 255), 2);
        }
    }
    return result;
}

与CascadeClassifier相关的所有内容都会出错。

C:\Users\Coder\Desktop\qt\tracker.cpp:37: error: undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
C:\Users\Coder\Desktop\qt\tracker.cpp:38: error: undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
C:\Users\Coder\Desktop\qt\tracker.cpp:217: error: undefined reference to `cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'
C:\Users\Coder\Desktop\qt\tracker.cpp:224: error: undefined reference to `cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'
C:\Users\Coder\Desktop\qt\tracker.cpp:15: error: undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
C:\Users\Coder\Desktop\qt\tracker.cpp:16: error: undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
C:\Users\Coder\Desktop\qt\tracker.cpp:15: error: undefined reference to `cv::CascadeClassifier::CascadeClassifier()'
C:\Users\Coder\Desktop\qt\tracker.cpp:16: error: undefined reference to `cv::CascadeClassifier::CascadeClassifier()'

重要的是,所有其他OpenCV类都可以正常工作。我是否缺少与CascadeClassifier相关的库?

1 个答案:

答案 0 :(得分:0)

添加此内容:

    `LIBS += -LD:/opencv-build/bin/ -llibopencv_objdetect401` 

.pro文件并包含

 `opencv2/objdetect.hpp`

在头文件中