Home  >  Article  >  Development Tools  >  A brief discussion on how to build and configure the opencv environment in VSCode

A brief discussion on how to build and configure the opencv environment in VSCode

青灯夜游
青灯夜游forward
2021-11-24 19:11:235063browse

How to build and configure the opencv environment in

VSCode? The following article will introduce to you how to build and configure the opencv environment in VSCode. I hope it will be helpful to friends in need!

A brief discussion on how to build and configure the opencv environment in VSCode

Some time ago I wanted to learn opencv. Since I have always been accustomed to using vscode to write code, I decided to use vscode to build an opencv environment. I read many articles to set up the environment, and then chose the method of one article. The article was indeed detailed, but I felt that there were still some details that were not explained clearly. I later relied on a step from another article to successfully set up the environment. Finish. Therefore, I decided to talk about the steps to build the opencv environment based on this article and my own experience.

[Recommended learning: "vscode introductory tutorial"]

Resource download

MinGW-w64 offline package download address: https://sourceforge. net/projects/mingw-w64/files/.
The online package download will be very slow, so you can download the offline package directly.

A brief discussion on how to build and configure the opencv environment in VSCode

CMake download address: https://cmake.org/download/

CMake needs to select the binary file, that is, the Binary distributions column. Select the file corresponding to your computer to download

A brief discussion on how to build and configure the opencv environment in VSCode
opencv library: https://sourceforge.net/projects/opencvlibrary/.

Software installation and configuration environment variables

Install?

MinGW-w64 and CMake do not need to be installed. You only need to unzip it to the storage path of your choice (similar to the installation path)

Configure environment variables

Find the following picture MinGW-w64 folder location:

A brief discussion on how to build and configure the opencv environment in VSCode

Add this path to the environment variable, as I configured: F:\MINGW64\mingw64\bin.

How to add environment variables: Right-click My Computer->Advanced System Settings->Environment Variables->Select both user variables and system variables. Double-click path to add the path->OK

A brief discussion on how to build and configure the opencv environment in VSCode

Find the CMake folder location as shown below:

A brief discussion on how to build and configure the opencv environment in VSCode
Add the path to the environment variable, such as my configuration: F:\ CMAKE\bin.

Find the location of the opencv folder in the picture below:

A brief discussion on how to build and configure the opencv environment in VSCode

Add the path to the environment variable, such as: F:\opencv4.5.1\opencv\build\ x64\mingw\bin.

The current environment variable path that should be available

A brief discussion on how to build and configure the opencv environment in VSCode

#Check whether the environment variable is added successfully:

win R key and enter cmd to the terminal:

A brief discussion on how to build and configure the opencv environment in VSCode
A brief discussion on how to build and configure the opencv environment in VSCode

#The commands in the terminal are a bit different. Mine is gcc -v and cmake --version. I don’t know whether it is -v or –version

Generate MakeFiles

Before operating cmake-gui, if you have not climbed over the wall, you must first append the following to the end of the C:\Windows\System32\drivers\etc\hosts file:

151.101.72.133 raw.githubusercontent.com

Because cmake-gui will download some files, these files are on raw.githubusercontent.com. If you do not modify the hosts, many files are likely to fail to download.

Modification method: Open the hosts file in Notepad for editing, or drag it directly to vsocde to open and modify it. If you have modified but cannot save this problem, you can make a copy first, delete the original file, and copy the modified file back to its original location.

Open cmake-gui and select the source file path and MakeFiles saving path (optional path), as shown in the figure:

A brief discussion on how to build and configure the opencv environment in VSCode

Click Configure, the pop-up window configuration is as follows:

A brief discussion on how to build and configure the opencv environment in VSCode

Please note that MinGW is selected in the drop-down box, don’t get sidetracked. Then click Next, select the compilation tool in the MinGW file as follows, and finally click Finish.

A brief discussion on how to build and configure the opencv environment in VSCode

执行过程中消息框会出现一堆红色信息,最后显示Configure done,是正常的。如果执行时中断,则存在其他问题。在执行完后,勾选BUILD_opencv_world,WITH_OPENGL和BUILD_EXAMPLES,不勾选WITH_IPP、WITH_MSMF和ENABLE_PRECOMPILED_HEADERS(如果有的话),CPU_DISPATCH选空。如果要编译opencv_contrib,则需要在OPENCV_EXTRA_MODULES_PATH 把路径选择为解压的opencv_contrib文件中的“modules”文件夹。

再次点击Configure,这次执行完后仍有错误如下:

A brief discussion on how to build and configure the opencv environment in VSCode

也就是说前面虽然修改了hosts,但是有些文件仍然没有成功下载,解决的方法就是手动下载它们。可以用浏览器访问下载链接,或者使用迅雷等下载器进行下载。CMakeDownloadLog.txt文件中列出了所有丢失文件的下载链接,比如:

https://raw.githubusercontent.com/opencv/opencv_3rdparty/759a23e24ab787a0979f8a93103dcc3105ec10c1/ffmpeg/opencv_ffmpeg.dll

一个个访问这些链接,下载后放到OpenCV源文件里.cache的相应子文件夹中替代原缓存文件(下载的文件重命名为相应地缓存文件名并删除原缓存文件)。这样从头到尾下载CMakeDownloadLog.txt中列出的所有丢失文件,之后,再次Configure,理论上不会出现红色的错误消息了。然后点击Generate,正常的话会显示非红色的消息Generate Done。一般翻墙成功的话都可以直接下载成功

编译opencv

CMD到MakeFiles所在文件夹,执行minGW32-make命令,或者使用多线程minGW32-make -j 4命令:

A brief discussion on how to build and configure the opencv environment in VSCode

如果报错可查阅下面的网址帮助:

https://blog.huihut.com/2018/07/31/CompiledOpenCVWithMinGW64/

一般问题都是可以通过勾选和去勾选解决的,因为之前就Configure过,因此回头再Configure一下花不了太多时间。

如果同时编译opencv_contrib,可能会报如下错误:

A brief discussion on how to build and configure the opencv environment in VSCode

编译完成之后,输入minGW32-make install 来完成装载。

minGW32-make install

vscode配置

在VScode中安装C/C++扩展,添加以下三个json文件:

launch.json 需要配置miDebuggerPath项。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "opencv4.5.1 debuge",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "F:/MINGW64/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "opencv4.5.1 compile task"
        }
    ]
}

c_cpp_properties.json 需要配置compilerPath项和includePath项。

{
    "configurations": [
        {
            "name": "win",
            "includePath": [
                "${workspaceFolder}/**",
                "F:/opencv4.5.1/opencv/build/x64/mingw/install/include",
                "F:/opencv4.5.1/opencv/build/x64/mingw/install/include/opencv2",
                "F:/opencv4.5.1/opencv/build/x64/mingw/install/include/opencv2/myself_all"
            ],
            "defines": [],
            "compilerPath": "F:/MINGW64/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

tasks.json 需要配置command项、args项和options项。

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "opencv4.5.1 compile task",
            "command": "F:/MINGW64/mingw64/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
                "F:/opencv4.5.1/opencv/build/x64/mingw/install/x64/mingw/bin/libopencv_world451.dll",
                "-I",
                "F:/opencv4.5.1/opencv/build/x64/mingw/install/include",
                "-I",
                "F:/opencv4.5.1/opencv/build/x64/mingw/install/include/opencv",
                "-I",
                "F:/opencv4.5.1/opencv/build/x64/mingw/install/include/opencv2",
            ],
            "options": {
                "cwd": "F:/MINGW64/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

注意:如果是复制我的文件配置的话记得把opencv4.5.1改为你的对应版本

验证程序:Ctrl+Shift+B编译即可生成对应exe文件,然后F5运行。

#include<opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char** argv) {
    Mat src = imread("image/girl.jpg"); //图片路径为同一文件夹
    namedWindow("window_1",0);
    imshow("window_1", src);
    waitKey(0);
    return 0;
}

更多关于VSCode的相关知识,请访问:vscode教程!!

The above is the detailed content of A brief discussion on how to build and configure the opencv environment in VSCode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete