C++ OpenCV facedetect 示例不会加载级联分类器 xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8152983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:05:32  来源:igfitidea点击:

OpenCV facedetect example won't load cascade classifier xml

c++cimage-processingopencvface-detection

提问by Eyal Perry

I'm working on a application that requires OpenCV object detection using Haar cascade classifier. I'm using OpenCV 2.3.1 with VS2010 on a 64bit Windows Machine. I compiled and built OpenCV myself and didn't use any pre-compiled binaries.

我正在开发一个需要使用 Haar 级联分类器进行 OpenCV 对象检测的应用程序。我在 64 位 Windows 机器上使用 OpenCV 2.3.1 和 VS2010。我自己编译和构建了 OpenCV,没有使用任何预编译的二进制文件。

First, I wanted to start meddling with the example facedetect.cpp that's included in OpenCV. I built it with no errors, but when I'm trying to run it won't open the cascade classifier xml file (the CascadeClassifier.load() function returns false). I didn't change anything from the sample source code.

首先,我想开始处理 OpenCV 中包含的示例 facedetect.cpp。我构建它时没有错误,但是当我尝试运行它时,它不会打开级联分类器 xml 文件(CascadeClassifier.load() 函数返回 false)。我没有更改示例源代码中的任何内容。

I'm using the xml file that is distributed with OpenCV so the problem isn't with the xml file. I also made sure that the application can access and read the file using a simple fopen.

我使用的是随 OpenCV 一起分发的 xml 文件,所以问题不在于 xml 文件。我还确保应用程序可以使用简单的 fopen 访问和读取文件。

I believe (but not sure) that the problem seems to be that the cascade classifier is of an "old" type. But in the OpenCV documentation it is specifically implied that the new CascadeClassifier object can open both "old" and "new" cascade classifiers.

我相信(但不确定)问题似乎是级联分类器属于“旧”类型。但是在 OpenCV 文档中特别暗示新的 CascadeClassifier 对象可以打开“旧”和“新”级联分类器。

Here's a link: http://opencv.itseez.com/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-load

这是一个链接:http: //opencv.itseez.com/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-load

I even tried using the pre-compiled OpenCV2.2 binary and it works excellent with that xml. And then I tried to compile the 2.2 sample source code, and again it couldn't load the xml.

我什至尝试使用预编译的 OpenCV2.2 二进制文件,它与那个 xml 一起工作得很好。然后我尝试编译 2.2 示例源代码,它再次无法加载 xml。

I'm aware that I can try using the old object CvHaarClassifierCascade, but I prefer to use the latest version of OpenCV and its objects.

我知道我可以尝试使用旧对象 CvHaarClassifierCascade,但我更喜欢使用最新版本的 OpenCV 及其对象。

Does anyone have a clue what am I doing wrong?

有谁知道我做错了什么?

回答by tk120404

Give the complete path of the xml file

给出xml文件的完整路径

String face = "c:/data/xml/haarcascade_frontalface_alt.xml";

It should work!

它应该工作!

回答by FMax

I had the same situation. I solved it when I realized that I'm linking release libs in Debug configuration. Changing opencv_231*.libto opencv_*231d.libhas solved the problem.

我有同样的情况。当我意识到我在调试配置中链接发布库时,我解决了它。更改opencv_231*.libopencv_*231d.lib已解决问题。

CascadeClassifier::loadis not the only function causing such troubles, see this thread for details: OpenCV imread(filename) fails in debug mode when using release libraries.

CascadeClassifier::load不是唯一导致此类问题的函数,请参阅此线程以获取详细信息:OpenCV imread(filename) failed in debug mode when using release libraries

回答by twerdster

I got this working by using notepad++. I converted all the relevant xml files to ANSI and also deleted the first line <?xml version="1.0"?>and then rewrote it by hand.

我通过使用记事本++来完成这项工作。我将所有相关的 xml 文件转换为 ANSI 并删除了第一行<?xml version="1.0"?>,然后手动重写了它。

回答by Saikumarch

If you are using windows then check the path. Concern is

如果您使用的是 Windows,请检查路径。关注的是

  1. escape sequence in the path.
  2. forward or backward slash depending on operating system.
  1. 路径中的转义序列。
  2. 正斜杠或反斜杠取决于操作系统。

It should be like C:\\Ceemple\\data\\haarcascades\\haarcascade_frontalface_alt.xml. (by the way I am using CeempleIDE)

它应该像C:\\Ceemple\\data\\haarcascades\\haarcascade_frontalface_alt.xml。(顺便说一下,我使用的是CeempleIDE)