Windows10+VsCode+OpenCV开发环境搭建
一、概述
主要是想尝试一下VsCode+OpenCV开发。尝试了Visual Studio,感觉太重量级。所以最终选择了vscode,废话不多说开搞
二、开搞
1.下载安装一个VsCode
2.安装VsCode C++开发插件,安装如图下面4个插件就行
3.安装mingw64。
这里的安装版本需要注意下,需要安装posix线程版本,不然opencv编译的时候会报错
x86_64-12.2.0-release-posix-seh-rt_v10-rev1
解压后配置环境变量就行
下载地址如下:https://github.com/niXman/mingw-builds-binaries/releases
4.下载opencv源代码
下载地址:https://opencv.org/releases/
5.下载cmake并安装
下载地址:https://cmake.org/download/
打开GUI界面
以上4部都完成之后,进入build_install目录(也就是你上面配置的编译输出目录)
并打开命令行执行如下图所示:
等待执行完成,如果执行完成后没有出错。则接着执行:mingw32-make install
完成后opencv就彻底配置好了。
然后把:D:\opencv\build_install\install\x64\mingw\bin配置到环境变量中。
6.配置VsCode
需要新建三个文件,分别是:launch.json、tasks.json、c_cpp_properites.json
a.settings.json文件的配置(这个是vscode自动生成的,打开文件夹后会生成)
{ "C_Cpp.default.includePath": [ // 下面的两个路径需要改为自己的路径。C:/work/opencv/opencv453_build是我编译opencv的路径 "D:/opencv/build_install/install/x64/mingw/lib", "D:/opencv/build_install/install/include" ], "code-runner.executorMap": { "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=UTF-8 && &'$dir$fileNameWithoutExt'", // 下面三个路径,也需要改为自己的路径 "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -I D:/opencv/build_install/install/include -I D:/opencv/build_install/install/include/opencv2 -L D:/opencv/build_install/install/x64/mingw/bin/libopencv_*" }, "files.associations": { "array": "cpp", "atomic": "cpp", "*.tcc": "cpp", "cctype": "cpp", "clocale": "cpp", "cmath": "cpp", "cstdarg": "cpp", "cstddef": "cpp", "cstdint": "cpp", "cstdio": "cpp", "cstdlib": "cpp", "cwchar": "cpp", "cwctype": "cpp", "deque": "cpp", "unordered_map": "cpp", "vector": "cpp", "exception": "cpp", "algorithm": "cpp", "memory": "cpp", "memory_resource": "cpp", "optional": "cpp", "string": "cpp", "string_view": "cpp", "system_error": "cpp", "tuple": "cpp", "type_traits": "cpp", "utility": "cpp", "fstream": "cpp", "initializer_list": "cpp", "iosfwd": "cpp", "iostream": "cpp", "istream": "cpp", "limits": "cpp", "new": "cpp", "ostream": "cpp", "sstream": "cpp", "stdexcept": "cpp", "streambuf": "cpp", "typeinfo": "cpp", "chrono": "cpp", "complex": "cpp", "condition_variable": "cpp", "cstring": "cpp", "ctime": "cpp", "list": "cpp", "functional": "cpp", "iterator": "cpp", "map": "cpp", "ratio": "cpp", "set": "cpp", "iomanip": "cpp", "mutex": "cpp", "thread": "cpp" } }
b.launch.json文件内容配置
{ "version": "0.2.0", "configurations": [ { "name": "opencv4.5.2 debuge", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", // 这里,需要改为自己的mingw路径 "miDebuggerPath": "D:/mingw64/bin/gdb.exe", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "opencv4.5.2 compile task" } ] }
3.tasks.json文件内容配置
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "opencv4.5.2 compile task", // 这里需要改为自己的mingw路径 "command": "D:/mingw64/bin/g++.exe", "args": [ "-g", "${file}", "-o", "${workspaceFolder}\\${fileBasenameNoExtension}.exe", // 下面三个路径改为自己对应的opencv编译路径 "D:/opencv/build_install/install/x64/mingw/bin/libopencv_*", "-I", "D:/opencv/build_install/install/include", "-I", "D:/opencv/build_install/install/include/opencv2" ], "options": { // 这里改为自己的mingw路径 "cwd": "D:/mingw64/bin" }, "problemMatcher": [ "$gcc" ], "group": "build", "presentation": { "panel": "new" //默认为“shared“表示共享,改成new之后每个进程创建新的端口 } } ] }
4.c_cpp_properites.json文件内容配置
{ "configurations": [ { "name": "win", "includePath": [ "${workspaceFolder}/**", // 下面6个路径改为自己对应的路径 "D:/opencv/build_install/install/include", "D:/opencv/build_install/install/include/opencv2", "D:/mingw64/lib/gcc/x86_64-w64-mingw32/12.2.0/include", "D:/mingw64/lib/gcc/x86_64-w64-mingw32/12.2.0/include/c++", "D:/mingw64/lib/gcc/x86_64-w64-mingw32/12.2.0/include/c++/x86_64-w64-mingw32" ], "defines": [], // 改为自己的mingw路径 "compilerPath": "D:/mingw64/bin/gcc.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
配置目录如下:
7.到此时VsCode+OpenCV已经配置完成了,下面用一个输出图片的例子做测试。
#include <iostream> // #include <opencv.hpp> #include <opencv2/opencv.hpp> #include <opencv2/highgui.hpp> using namespace std; using namespace cv; int main() { cv::Mat src = cv::imread("images/image1.jpeg"); cv::imshow("src",src); cv::waitKey(0); cout << "hello opencv " << endl; return 0; }
点击F5运行项目,或者右键执行Run Code
结束!