文章目录
不配置系统的环境变量使用OpenCV
在clion里选择

后在environment variables里配置路径

使用还需有步骤:CMakeLists.txt里配置
set(OpenCV_DIR "D:/environment/clibs/opencv-4.8.0/build/install")
find_package(OpenCV REQUIRED)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
我还出现过传到qt的图像一闪一闪的问题,即使在系统环境变量里配置也出现过,暂时不知道原因
使用CascadeClassifier后报错finished with exit code -1073741511 (0xC0000139)
将mingw/bin下的libstdc+±6.dll复制到exe下

部分文章说的复制到Windows/system32下,但这样会造成qt无法运行项目(找不到动态链接库)
4.8版本训练模型时没有createsamples等文件
下载3.4.X版本,4.X也能用(opencv官方说的)
正负样本准备好后训练步骤:
- 为了方便处理文件,最好重命名,例如1.jpg,2.jpg。。。。。。正负数量比1:3,我的正样本1000张
- 修改图片像素
for (int n = 1; n <= 140; n++) { // 代表正数据集中开始和结束照片的数字
string path = "D:\\" + to_string(n) + ".jpg";
// 读取图片
Mat img = imread(path);
// resize(img, img, Size(20, 20)); // 修改正样本像素为20x20
resize(img, img, Size(80, 80)); // 修改负样本像素为80x80
imwrite(path, img);
}
- 正负样本各生成资源记录文件
dir /b/s/p/w *.jpg > name.txt - 生成vec文件
创建xml文件夹,并输入
opencv_createsamples.exe -vec vecname.vec -info 正样本name.txt -num 正样本数量 -w 20 -h 20
- 训练
-numPos 正样本数。此处正样本数小于生成时的样本数,我写的是500左右,否则会报错Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file
opencv_traincascade.exe -data xml -vec vecname.vec -bg 负样本name.txt -numPos 正样本数 -numNeg 负样本数 -numStages 20 -w 20 -h 20 -mode ALL
本部分参考:https://blog.youkuaiyun.com/qq_52852432/article/details/122618779
显示/播放mp4异常
将opencv-4.8.0\sources\build\bin下的opencv_videoio_ffmpeg480_64.dll复制到

代码如下,waitkey相关代码不能省略:
void CountFaces() {
VideoCapture cap("D:/a.mp4");
if (!cap.isOpened()) {
std::cerr << "Error: Unable to open the video file." << std::endl;
return;
} else {
std::cout << "Video file is successfully opened." << std::endl;
}
Mat frame;
namedWindow("Video", WINDOW_NORMAL); // 创建一个窗口
while (cap.read(frame)) {
imshow("Video", frame);
if (waitKey(30) >= 27) {
break;
}
}
waitKey(0);
cap.release();
destroyAllWindows();
}
2831





