win10下VSCode配置opencv4.5.2(64位应用程序)

一、前言 

        用vscode来写opencv代码需要自己编译OpenCV,主要用到MinGW-w64和CMake工具。由于可能存在的版本兼容问题,下载这些工具前最好先访问网站:

    https://github.com/huihut/OpenCV-MinGW-Build

  该网站包含了已经编译过的Opencv库,不想自己编译的话可以直接下载(编译过程并不会一帆风顺,需要有折腾的精神)。

  该网站还提供了现成的版本兼容信息,点开Configuration可以看到所对应的MinGW和CMake版本,下载时就可以据此选择合适的MinGW和CMake版本,能减少许多麻烦事。

        本文章直接使用的是编译后的opencv4.5.2(64位版本)

二、老规矩,先上各种工具及安装包的下载链接

opencv4.5.2下载链接:

链接:https://pan.baidu.com/s/1zSoEcG2Y0d9Vdk0zdCdSgw 
提取码:ke43 
 

GCC_MinGW64位编译器下载链接:

链接:https://pan.baidu.com/s/1aCYDc8Xd7zya4R-Ufiq60Q 
提取码:tny2 
 

三、工具和opencv4.5.2的安装

    1、使用GCC_MinGW64位编译器

 下载后解压到自选路径。找到下图的文件夹位置:

  

win10下VSCode配置opencv4.5.2(64位应用程序)_第1张图片

   添加该路径到环境变量,如:

    F:\MinGW\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\mingw64\bin

  检查安装:

    

  MinGW-w64用处是编译C和C++等程序,vscode不像VS一样本身就具备编译功能,因此需要额外借助MinGW-w64。MinGW-w64+vscode的优点就是轻量级,vscode无论在界面、内存占用和速度等方面都完胜VS。另外自己用gcc来编译程序,可以避免像VS那样生成一大堆文件的情况。单就OpenCV学习来说,我更喜欢使用vscode。

2、将下载后的opencv解压后放到如下位置

C:\OpenCV-MinGW-Build-OpenCV-4.5.2-x64

win10下VSCode配置opencv4.5.2(64位应用程序)_第2张图片

四、VsCode配置以及调试运行

1、tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "opencv3.4.2 compile task",
            "command": "C:/Program Files (x86)/mingw-w64/x86_64-8.1.0-release-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
                "-I",  "C:/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/include",
                "-I",  "C:/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/include/opencv2",

                "-L", "C:/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/x64/mingw/lib",
                "-l", "libopencv_calib3d452",
                "-l", "libopencv_core452",
                "-l", "libopencv_dnn452",
                "-l", "libopencv_features2d452",
                "-l", "libopencv_flann452",
                "-l", "libopencv_gapi452",
                "-l", "libopencv_highgui452",
                "-l", "libopencv_imgcodecs452",
                "-l", "libopencv_imgproc452",
                "-l", "libopencv_ml452",
                "-l", "libopencv_objdetect452",
                "-l", "libopencv_photo452",
                "-l", "libopencv_stitching452",
                "-l", "libopencv_video452",
                "-l", "libopencv_videoio452"


            ],
            "options": {
                "cwd": "C:/Program Files (x86)/mingw-w64/x86_64-8.1.0-release-posix-seh-rt_v6-rev0/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

     

2、launch.json

{

    "version": "0.2.0",
    "configurations": [
        {
            "name": "opencv3.4.2 debuge",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files (x86)/mingw-w64/x86_64-8.1.0-release-posix-seh-rt_v6-rev0/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "opencv3.4.2 compile task"
        }
    ]
}

3、c_cpp_properties.json

{
    "configurations": [
        {
            "name": "win",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/include",
                "C:/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/include/opencv2"           
            ],
            "defines": [],
            "compilerPath": "C:/Program Files (x86)/mingw-w64/x86_64-8.1.0-release-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

4、main.cpp


#include 
//std::fstream m_file;
#include 
#include 
#include 
using namespace cv;
using namespace std;

int main()
{
	
	//抓取摄影栈
	try
	{
		system("PAUSE");

		VideoCapture cap(0);
		cap.set(3,1280);
		cap.set(4,720);
		//尝试开放摄影栈
		if (!cap.isOpened()) return -1;
	
		//用矩阵记录抓取的每张frame
		Mat frame;

		//建立一个视窗,名称为camera
		namedWindow("camera", 1);
	
		for (;;)
		{	
			//把取得的影像放置到矩阵中	
			cap >> frame;
	
			//演示frame到camera名称的视窗	
			imshow("frame", frame);
	
			if (waitKey(30) >= 0) break;
		}
		system("PAUSE");
	}
	catch(const std::exception& e)
	{
		std::cerr << e.what() << '\n';
	}

	return 0;
}

5、将opencv所有的DLL动态库,复制到应用程序根目录下

win10下VSCode配置opencv4.5.2(64位应用程序)_第3张图片

6、验证程序,Ctrl+Shift+B编译即可生成对应exe文件或者F5键调试运行。

参考网址:https://my.oschina.net/u/4313733/blog/4585043

你可能感兴趣的:(c++,Visual,Studio,Code,opencv,vscode,opencv,c++)