Get Visual Studio to configure the OpenCV environment in one article


foreword

Configuring the OpenCV environment on Visual Studio is extremely annoying, especially for beginners, often hours have passed, killing our love for code in the cradle.
Based on my countless environment configuration experiences, this article summarizes a complete set of OpenCV environment configuration process, including teaching and meeting, 100% successful.


Preparation

To configure the OpenCV environment on Visual Studio, the first thing is to download and install Visual Studio and OpenCV. The official download links are provided below:

  • Visual Studio : After downloading, install the corresponding functions according to individual needs. Beginners should not be greedy, as it takes up a lot of space and downloads slowly.

  • OpenCV : After the download is complete, just decompress it to the specified path, and there should be no Chinese path.

When you download, download the corresponding version according to your own needs. If the version does not correspond to your project, it is easy to cause problems. OpenCV can install multiple versions at the same time. Although only one can be set in the system environment variable, different projects can be set separately.

System environment variable configuration

Adding OpenCV to the system environment variable is equivalent to giving the system a default OpenCV version. The specific process is as follows:

  1. Open [Advanced System Settings] -> [Advanced] -> [Environment Variables];
  2. Add the OpenCV bin folder path in [Path] , for example D:\opencv\build\x64\vc14\bin, decide whether to configure system variables or user variables according to actual needs ;
  3. Just restart the computer.

VS project environment configuration

Project environment configuration can configure different versions of OpenCV according to different requirements. The main configuration includes directories (include) and library files (lib) .

  1. Open your project, determine the configuration (Debug / Release) and platform (x86 / x64) of the project, and follow-up configuration needs;
  2. Open【Project】->【**(your project) Properties】;
  3. Configure two places [ include directory ] and [ library directory ] in [VC++ directory] :
    1. [Include directory] Add the include folder of OpenCV:

    D:\opencv\build\include
    D:\opencv\build\include\opencv
    D:\opencv\build\include\opencv2
    (Article 2 may not be available in higher versions)
    (It seems that 2 and 3 articles are not added)

    1. [Library Directory] Add the lib folder of OpenCV:

    D:\opencv\build\x64\vc14\lib
    (x64 is related to your platform environment)
    (vc14 is related to your VS version, and the vcxx folder contained in different versions of OpenCV is also different)

  4. Configure [ Additional Include Directory ] in [C/C++] -> [General] , which is also added to the include folder of OpenCV:

    D:\opencv\build\include
    D:\opencv\build\include\opencv
    D:\opencv\build\include\opencv2

  5. Configure [ Additional Dependencies ] in [Linker] -> [Input] , and add the * .lib file in the above lib folder , such as:

    opencv_world455.lib / opencv_world455d.lib
    (you only need to add one of the above two *.lib files in the higher version of OpenCV, and the one with d after it means the Debug version, otherwise it is the Release version)
    (the lower version needs to add a lot, Go to D:\opencv\build\x64\vc14\lib(the library directory added above) and add the * .lib file, add it as needed, if you don’t know it, add it all. Similarly, there is a d behind it to indicate the Debug version, otherwise it is the Release version)

Note : All the above paths and files need to be found according to your own environment, and different versions are different.

test

Practice is the sole criterion for testing truth. After the configuration is complete, find a picture at random and use the following code to test:

#include<opencv2/opencv.hpp>

using namespace cv;

int main()
{
    
    
    Mat img; 							  //定义一个Mat变量存储图像
    img = imread("D:/test.jpg");          //读取图片,图片路径记得改一下

    imshow("test", img); 				  //显示图片
    waitKey(0);							  //窗口等待
}

If the pop-up window shows a test image, congratulations, your configuration is successful. If not, the road of torture may have begun, and check step by step to see if there is any omission. good luck!

Replenish

  1. If there is %OpenCV_DIR% in the path during configuration, it means that the OpenCV path in the system environment variable is called here. Please decide whether you need to delete the change according to your needs.
  2. Different configurations (Debug/Release) and platforms (x86/x64) need to be reconfigured. After the configuration is completed, the switch does not need to be reconfigured.

Guess you like

Origin blog.csdn.net/lucifer479/article/details/125605904