Opencv | 图形学 | Mingw64 | 如何正确地用MinGW64编译与配置vscode的Opencv环境

如何正确地用MinGW64编译与配置vscode的Opencv环境

1.前情提要

最近有关于图形学的授课,教授开始布置的上机打码的代码实现作业了。虽说教授为了让我们省心,直接就整了个环境已经配置好的几个G的虚拟机给我们用。但是你也懂的,虚拟机打码,卡得要命。一开始还以为会用到什么高级的库,看了下就是个Eigen3和Opencv,对于Opencv,感觉自己手动在本地配一个就OK了。在配置的过程中才发现这玩意坑是尼玛的多,而且还是不带附加件的情况下,因此本篇博客应然而生。

2.准备工作

  1. 从https://github.com/opencv/opencv/releases下载合适版本的Opencv源码,笔者当时演示的是编译4.5.1版本的Opencv

  1. 下载安装、并正确配置MingW64(必须是64位,32位不支持Posix-thread,编译过程会报错),如果配置正确,在命令行输入命令应该会有正确的响应。

  1. 下载安装并正确配置cmake-gui

当然如果你足够geek的话,也可以直接用命令行,反正我是受不了

3.搭建,生成编译项目

首先把源码解压到一个文件夹中,并新建一个文件夹作为项目的输出

打开cmake-gui,填好源码以及输出的路径

点击Configure,就会弹出要你选择指定的编译环境的对话框了。我们妥妥地选择MinGW,并且指定Native编译器

把你MinGW64的gcc和g++的路径填进去(Fortran空着就行,这个项目用不上)

然后就开始配置了...

过了一会你回来发现,坑爹呢这是

配置显示你没有这两个第三方库,开始下载,并且由于中美两岸的缘故,半天都下不动。这个时候你就要明智地选择Stop了(网速好或者肉身FQ者另说)。

打开build/CMakeDownloadLog.txt,并找到

找到这两个库的地址,用尽你一切魔法上网手段去下载他们,这里是Opencv4.5.1两个库的地址分别是:

https://raw.githubusercontent.com/opencv/opencv_3rdparty/6152e132572dfdaa32887eabeb7199bef49b14dc/ffmpeg/opencv_videoio_ffmpeg.dll

https://raw.githubusercontent.com/opencv/opencv_3rdparty/6152e132572dfdaa32887eabeb7199bef49b14dc/ffmpeg/opencv_videoio_ffmpeg_64.dll

下好之后就可以放到build\3rdparty\ffmpeg

顺利配置完毕

(可选)找到BUILD_opencv_world并勾选,这样做可以把opencv各种散装的库集中在一个库libopencv_world中,当然如果你只需要其中的几个库也可以不勾选,或者只勾选需要的库。

然后点击生成(Generate)

很快就生成完毕了,这时候就可以退出cmake了

4.编译Opencv

在build目录下打开命令行,并输入mingw32-make -j4

其中-j4是指四核异步进行编译,按下回车就正式开始编译了

目前我们就完成二分之一的工作了,接下来就剩下编译和配置了,注意编译时间比较长,可以喝杯茶适当休息一下

另外编译中途可能会报出各种奇奇怪怪的构造错误,并终止编译。你可以重复尝试更换不同数量的多核异步编译,基本都能解决。但如果出现编译错误就需要谷歌一下了,因为中途报出来的错误种类非常多非常杂。总之最终编译进度达到[100%]就是胜利了。

你可以再运行一次检查有没有疏漏

可以看到编译好的库都在\build\bin中了

如果你没有勾选libopencv_world的话,则是生成各种散装的库

5.配置运行环境

打开环境变量,找到PATH

将库的路径添加进去

6.配置vscode编译项目环境

首先在build\目录下输入mingw32-make install以生成用以导入的头文件

build/install/include/下你就可以看到这些头文件了

新建一个文件夹作为项目目录

在vscode中打开,并如图新建三个项目设置文件

按照下面进行配置,注意其中mingw64以及opencv改成你的目录路径

  • c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win64",
            "includePath": [
                "${workspaceFolder}\\**",
                "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\include",
                "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include",
                "E:\\Library\\opencv\\build\\install\\include"
            ],
            "defines": [
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "cStandard": "c11",
            "cppStandard": "c++11"
        }
    ],
    "version": 4
}
  • launch.json
{
    "version": "0.2.0",
    "configurations": [
        {   
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}\\bin\\Assignment.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}\\bin",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "preLaunchTask":"g++",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        }
    ]
}
  • tasks.json
{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g", "${file}",
        "-o", "bin/Assignment.exe",
        "-std=c++11",
        "E:\\Library\\opencv\\build\\bin\\libopencv_world451.dll",
        "-I",
        "E:\\Library\\opencv\\build\\install\\include"]

}

如果你在编译时没有勾选BUILD_opencv_world,就要如下配置了......

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g", "${file}",
        "-o", "bin/Assignment.exe",
        "-std=c++11",        
	"E:\\Library\\opencv\\build\\bin\\libopencv_aruco451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_bioinspired451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_calib3d451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_ccalib451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_core451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_datasets451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_dnn451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_dnn_objdetect451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_dnn_superres451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_dpm451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_face451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_features2d451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_flann451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_fuzzy451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_gapi451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_hfs451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_highgui451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_imgcodecs451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_imgproc451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_img_hash451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_intensity_transform451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_line_descriptor451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_mcc451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_ml451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_objdetect451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_phase_unwrapping451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_photo451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_plot451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_quality451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_rapid451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_reg451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_rgbd451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_saliency451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_shape451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_stitching451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_structured_light451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_surface_matching451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_text451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_video451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_videoio451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_xobjdetect451.dll",
        "E:\\Library\\opencv\\build\\bin\\libopencv_xphoto451.dll",
        "-I",
        "E:\\Library\\opencv\\build\\install\\include"]

}

或者写个程序自动生成......

写个例程试试看......

成功编译运行。

(图片pid:74015542)

附录

A.用到的代码

  • 例程main.cpp代码
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
int main()
{
    cv::Mat src = cv::imread("uzuki.png", -1), dst;

    resize(src, dst, cv::Size(int(src.cols / 4), int(src.rows / 4)));

    cv::imshow("Uzuki", dst);

    cv::waitKey();

    return 0;
}
  • 文件自动生成
import os  
  
def file_name(file_dir):   
    for root, dirs, files in os.walk(file_dir):   
        for filess in files :
            if("libopencv" in filess) : print("\"E:\\\\Library\\\\opencv\\\\build\\\\bin\\\\" + filess + "\",")


file_name("E:\\Library\\opencv\\build\\bin\\")
posted @ 2021-01-31 14:06  Uzuki  阅读(1115)  评论(0编辑  收藏  举报