c++ - Opencv 3.0加载人脸级联时出错

标签 c++ debugging opencv visual-c++ visual-studio-2013

嗨,我已经实现了 opencv 库。它适用于某些代码,例如视频捕获或从文件运行视频。但是当我实现面部检测或物体检测或运动检测程序的代码时。目前我已经实现了这个程序。

#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/opencv.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

/** @function main */
int main(void)
{
    VideoCapture capture;
    Mat frame;

    //-- 1. Load the cascades
    if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading eyes cascade\n"); return -1; };

    //-- 2. Read the video stream
    capture.open(-1);
    if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }

    while (capture.read(frame))
    {
        if (frame.empty())
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        //-- 3. Apply the classifier to the frame
        detectAndDisplay(frame);

        int c = waitKey(10);
        if ((char)c == 27) { break; } // escape
    }
    return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    //-- Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);

        Mat faceROI = frame_gray(faces[i]);
        std::vector<Rect> eyes;

        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

        for (size_t j = 0; j < eyes.size(); j++)
        {
            Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
            int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
            circle(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
        }
    }
    //-- Show what you got
    imshow(window_name, frame);
}

当我尝试调试时,出现错误程序“[7912] ConsoleApplication1.exe”已退出,代码为 -1 (0xffffffff)。

当我尝试在不调试的情况下启动时,它会给出错误加载面部级联错误。

我还发现了一件事,调试时的一条警告消息是 C:\Users\rushikesh\Documents\Visual Studio 2013\Projects\ConsoleApplication1\x64\Debug\opencv_world300d.dll'。找不到或打开 PDB 文件。

但我检查有 world300d.dll。 opencv 3.0.0的一些程序正在运行,所以我想我已经正确配置了它,但是很少有程序特别跟踪对象或运动或检测到面部没有运行并给我同样的错误。

编辑 按照@srslynow的建议尝试后,我收到以下错误。

enter image description here

最佳答案

您的程序找不到 .xml 文件。请注意,从 Visual Studio IDE 运行程序时的默认工作目录不是 .exe 所在的位置。它是您项目的根目录。

可能的解决方案:

  • 将 xml 文件移至项目根目录
  • 将工作目录(在项目 > 属性 > 调试下)更改为 $(SolutionDir)$(Platform)\$(Configuration)\

编辑:

捕获默认视频设备是通过使用 capture.open(0); 这可能是以 -1 状态退出程序的原因,我假设您的计算机上确实有网络摄像头?

关于c++ - Opencv 3.0加载人脸级联时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220230/

相关文章:

c++ - 编译器忽略我的包含 -I 新库版本

python - 如何从python中的多个文件夹中读取多个图像

c++ - 从 C-Style 字符串 C++ 中删除字符

c++ - 如何知道 QLineEdit 是否获得焦点?

java - 修改步骤过滤器以仅显示项目类

javascript - 单击提交后,如何重定向到 js 组件并通过 php 将信息发送到 db?

c++ - opencv 中的视觉里程计(可能使用 RGBD)

c++ - 将 const wchar_t* 和 cont char* 从 VB 传递到 C++ DLL

iOS 框架和崩溃报告

opencv - 用相机计算运动物体的速度