c++ - OpenCV haarcascades加载根本不起作用
问题描述
我正在使用带有 OpenCV 4.4.0 的 Visual Studio 2019,一切都很棒,但是当我想开始人脸检测时,级联分类器不会加载 haarcascade
您还必须知道我在 c 分区中安装了 openCV,这是一个简单的代码
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <Windows.h>
#include <vector>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cam(0);
Mat img;
CascadeClassifier detector;
vector<Rect> faces;
Point p[2];
bool cap = false;
if (!detector.load("c:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"))
{
cout << "Image Detector Doesn't work\n";
return EXIT_FAILURE;
}
if (!cam.isOpened())
{
cout << "Can't Open Camera\n";
return EXIT_FAILURE;
}
while (!cap)
{
cam.read(img);
imshow("Cam", img);
waitKey(0);
if (GetAsyncKeyState(VK_ESCAPE))
cap = true;
}
destroyWindow("Cam");
cout << "Detecting Face...\n";
detector.detectMultiScale(img, faces);
for (int i = 0; i < faces.size(); i++)
{
p[0] = Point(faces[i].x,faces[i].y);
p[1] = Point(faces[i].x + faces[i].height,faces[i].y + faces[i].width);
rectangle(img,p[0],p[1],Scalar(0,0,255),3);
}
imwrite("Result.jpg",img);
return EXIT_SUCCESS;
}
此代码不加载 haarcascade 并在 cmd 中返回“无法加载”
所以我真的需要帮助并感谢所有人
解决方案
\
在 C++ 字符串文字中用作转义序列。
因此,您应该使用\\
将字符\
放入其中。
if (!dec.load("c:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"))
推荐阅读
- javascript - XMLHttpRequest 无法向 Flask 服务器发出请求
- entity-framework - 带有 EF Core 2.1 和 Sql Server 的时间戳?
- optimization - 整数二次规划与混合整数二次规划有什么区别?
- php - 如何在作曲家中列出孤儿包
- c# - 如何使用自定义表名在c#中插入
- python - 在谷歌搜索中选择第一个链接
- clang - 无法为 iOS (arm) 构建库,因为 SSE、AVX 等标志由 configure、libtool 设置
- generics - Dart - 具有泛型的类扩展具有第二泛型的第二类 - 这可能吗?
- neo4j - Neo4j:Cypher 查询返回错误的 json 结果
- python - 通过一个numpy数组循环和过滤以抵消后续相关的“事件”