点击下方卡片,关注“新机器视觉”公众号
重磅干货,第一时间送达
来源:CVHub
OpenCV-MinGW-Build,找到对应的Releases版本。这里以OpenCV-4.5.5-x64 | zip | tar.gz为例,我们点击下拉键Configuration:MinGW-W64 x86_64-posix-seh【下载最新版本即可】Windows-11-64bit-22000.434【Windwos 10也适配】CMake-3.21.3【一定要下载对应的版本,否则大概率会编译失败】下载时有条件的建议搭梯子,负责下行速度很慢。
OpenCV-4.5.5-x64 安装包:C:\opencv;此处我的安装路径为:
C:\cmake-3.21.3-windows-x86_64
C:\opencv\opencv
C:\mingw64
激活环境变量
检查是否成功
注意:编译过程有条件的尽量开
vpn,否则编译过程中涉及相关软件下载可能会很慢甚至失败。注意:编译过程有条件的尽量开vpn,否则编译过程中涉及相关软件下载可能会很慢甚至失败。
cmake-gui.exe,文件存放在 C:\cmake-3.21.3-windows-x86_64\cmake-3.21.3-windows-x86_64\bin 目录下;source code 地址或者点击Browse Source 选项选择对应的 source 路径;build 存放地址,可自己建立文件夹存放;Configure按钮,选择MinGW Makefiles本地编译器:gcc和g++路径:不出意外的话,程序会开始自动生成
Makefiles等文件配置,需要一段时间请耐心等待。
Configure后再点击Generate:简单总结下:
finish->configuring done->configure->generate
cmd,cd至刚刚的构建目录下C:/opencv/opencv/build/mingw64-build,输入编译指令minGW32-make -j8,完成后再输入minGW32-make install:C:\opencv\opencv\build\mingw64-build\bin路径添加到环境变量:VSCode,在插件管理搜索对应的插件:C/C++:.vscode文件夹下新建三个文件:c_cpp_properties.json、launch.json以及tasks.json:c_cpp_properties.json文件:{
"configurations": [
{
"name": "win",
"includePath": [
"${workspaceFolder}/**",
/*此处修改为你对应的路径*/
"C:/mingw64/include",
"C:/opencv/opencv/build/mingw64-build/install/include",
"C:/opencv/opencv/build/mingw64-build/install/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
/*此处修改为本机gcc编译器所在的对应路径*/
"compilerPath": "C:/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
launch.json文件:{
"version": "0.2.0",
"configurations": [
{
"name": "Opencv4.5.5 debug", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录,根据自身情况设定为${fileDirname}
"environment": [],
"externalConsole": false, // 调试时是否显示控制台窗口,设置为true时会弹出控制台出来,这个随意
"MIMode": "gdb",
/*此处修改*/
"miDebuggerPath": "C:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Opencv4.5.5 compile task" // 需要与tasks.json中的`label`字段保持一致
}
]
}
tasks.json文件:{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Opencv4.5.5 compile task",
/*修改*/
"command": "C:/mingw64/bin/g++.exe",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
/*注意:此处导入的路径为编译后的opencv路径,请勿导入原始的opencv路径*/
"-I", "C:/opencv/opencv/build/mingw64-build/install/include/",
"-I", "C:/opencv/opencv/build/mingw64-build/install/include/opencv2/",
"-L", "C:/opencv/opencv/build/mingw64-build/install/x64/mingw/bin/lib*"
],// 编译命令参数
"options": {
/*修改*/
"cwd": "C:/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"panel": "new", //这里shared表示共享,改成new之后每个进程创建新的端口
}
}
]
}
main.cpp文件,同时准备一张图片:#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(void)
{
cv::Mat img = cv::imread("D:/Projects/CODE_CPP/OpenCV/Projects/demo/lena.jpg");
cv::imshow("img", img);
cv::waitKey(0);
return 0;
}
完结。
本文仅做学术分享,如有侵权,请联系删文。