不想自己编译的,可以尝试直接使用别人编译好的,不过要注意将包含dll文件的目录添加进系统变量.这样在没有包含dll文件的路径下运行包含opencv的程序时,才不会报dll缺失.
单独编译链接,没有lib文件:opencv4.5.4编译完成库,编译条件,看下面的前提
Qt5152、VTK910、opencv454、eigen340联合编译包(包含lib文件):
VTK910、opencv454、eigen340
联合编译中的opencv454需要Qt5.15.2中的动态链接库,路径类似xxx\Qtcreator\application\5.15.2\mingw81_64\bin
前提:
1.windows10系统
2.软件vs code
3.C++插件:C/C++ v1.6.0
4.opencv version: 4.5.4
5.gcc8.1.0 异常模型seh(minGW中的)
6.cmake version: 3.22.0.0-rc1
如果不想自己编译(可以下载编译后的库文件,在文章最下方),但是不一定成功
步骤:
1.下载minGW,并将minGW的bin目录加入系统环境变量Path
下载minGW网址:
https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/
2.下载opencv官网源文件,并解压
vs code中C++使用opencv,需要自己编译源文件
3.下载最新版cmake,并添加环境变量
4.在cmake-3.17.0-rc3-win64-x64\bin中打开cmake-gui.exe,并配置
图中第一行将第2步骤中解压的源文件路径填入
(应和D:/studying/language/C++/libraries/opencv-4.5.2相似,且opencv-4.5.2文件夹中没有除源文件的其他无关文件,只要解压后,别往里面加东西就行)
图中第二行填入你想要将编译后的文件放入的文件夹路径
5.点击configure,选择MinGW Makefiles生成器,指定本地编译器(specify native compilers),最后点击next,选择C和C++编译器,编译器路径类似xxx\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\mingw64\bin.完成一次configuration后,还需要再进行一次configuration,直到,完成configuration,显示configuration is done(第一次configuration结束后,value使用默认的就可以,(如果有其他需求,在进行勾选)
6.点击Generate,等待.若出错,则点击configure,接着等待.configure完成后(没问题后,进行后面的操作.若有问题找出原因),点击Generate.
(确保Generate后任何警告和错误)
7.找到第4步骤中,填入的binary文件所在路径,使用shift+右键在Power Shell中打开文件夹.接着在power shell中运行minGW32-make (这里的可要 -j 4,试试make install) .等待(若在power shell中进度条太长时间没有动,点击鼠标右键试试).
完成标志: 进度条显示为100%,且等待输入下一个命令(不要关闭窗口)
8.运行minGW32-make install,生成install文件夹
9.将:\studying\language\opencv\code\opencv454\install\x64\mingw\bin(里面存放的是dll文件)类似的地址添加进环境变量.不然运行编译的程序会显示缺少dll.
(没有将这个添加进环境变量,且配置文件中没有设置,那么可能有以下情况)
1.debug中显示unexpected output from “-exec -run”;
2.在vscode中运行无输出结果.
3.在资源管理中直接运行exe文件,提示缺少dll文件.
10.配置vscode
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}",
"D:\\studying\\language\\C++\\libraries\\opencv452\\install\\include",//在.cpp和.h文件中#include 头文件的时候,才能找到
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/studying/language/mingw64/bin/g++.exe",//修改为你的g++编译器所在路径
"intelliSenseMode": "windows-gcc-x64",
"cppStandard": "c++11"
}
],
"version": 4
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "D:/studying/language/mingw64/bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\studying\\language\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:/studying/language/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++11",
//添加c_cpp_properties.json中配置的对应的头文件搜索路径
"-I","D:\\studying\\language\\C++\\libraries\\opencv452\\install\\include",
//-L后添加dll文件所在路径
//注意"-L"和"-l"一定要单独放,不要这样写成"-L xxxxx".如果写成这样,编译的时候会在命令中添加"",会报错
"-L","D:\\studying\\language\\C++\\libraries\\opencv452\\install\\x64\\mingw\\bin",
//仅仅告诉cpp文件头文件,并不能找到对应的dll文件,因此需要使用 "-l"明确指出
//注意"-l" 后面跟的库名不能加后缀,不然会提示ld.exe无法打开文件-l(name).(posterfix)
"-l", "libopencv_calib3d454",
"-l", "libopencv_core454",
"-l", "libopencv_dnn454",
"-l", "libopencv_features2d454",
"-l", "libopencv_flann454",
"-l", "libopencv_gapi454",
"-l", "libopencv_highgui454",
"-l", "libopencv_imgcodecs454",
"-l", "libopencv_imgproc454",
"-l", "libopencv_ml454",
"-l", "libopencv_objdetect454",
"-l", "libopencv_photo454",
"-l", "libopencv_stitching454",
"-l", "libopencv_video454",
"-l", "libopencv_videoio454"
],
"options": {
"cwd": "D:\\studying\\language\\C++\\pro"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
测试源码:
#include
#include
#include
using namespace std;
//opencv头文件
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
int blurAmount = 15; //保存滑动条的位置值
static void onChange(int pos,void* userInput);
static void onMouse(int event,int x,int y ,int,void *userInput);
int main()
{
Mat airplane1 = imread("D:\\studying\\language\\C++\\pro\\opencv_test\\wallhaven-pkk69p.jpg");
namedWindow("origin_img");
namedWindow("airplane1");
imshow("origin_img",airplane1);
createTrackbar("airplane1","airplane1",&blurAmount,30,onChange,&airplane1);
setMouseCallback("airplane1",onMouse,&airplane1);
onChange(blurAmount,&airplane1);
waitKey(0);
destroyWindow("airplane1");
return 0;
}
static void onChange(int pos,void*userData)
{
if (pos <=0)
return;
Mat imgBlur;
Mat *img = (Mat*)userData;
cv::blur(*img,imgBlur,Size(pos,pos));
//Display
imshow("airplane1",imgBlur);
}
static void onMouse(int event, int x, int y,int, void *userInput)
{
if(event != EVENT_LBUTTONDOWN)
return;
Mat *img = (Mat*)userInput;
//绘图
//cvCircle(img,Point(x,y),10,Scalar(0,255,0),3);
//调用模糊图像方法
onChange(blurAmount, img);
}
Cmake在configuration时,遇到配置进程失败,选择有效文件啊.可以选择cmake.msi文件,修复cmake.再进行configuration.
在Qt5中使用opencv
在完成编译器的选择和第一次配置之后,需要在cmake配置界面,勾选WITH_QT和WITH_OPENGL,并且需要配置Qt5_DIR为xxx/Qtcreator/application/5.15.2/mingw81_64/lib/cmake/Qt5.然后再点击config。其余操作和上面一样
若要和VTK联合编译需要选择WITH_VTK和配置VTK_DIR为类似xxx/VTK910_build/lib/cmake/vtk-9.1
路径(建议在完成第一次config后,立即配置VTK并config,然后再配置其他,不然后面更改配置可能会导致,VTK不支持)
可以在cmake下方信息栏,查看VTK是否支持
参考:
1.win10下VSCode配置opencv4.4.0(超详细教程,亲测有效)
2.VScode中配置C++的Opencv库
3.Qt配置OpenCV教程,亲测已试过(详细版)(包含Qt测试)