How to configure OpenCV on VS2019

foreword

Due to the needs of work, I need to set up the OpenCV environment on VS2019 myself. I have done it several times before, but after I finished it, I didn’t care too much, so I had to do it again. It seems that it is very troublesome on VS, otherwise, just The environment must be preserved. Anyway, one is that every time you create a new project, you have to reconfigure the configuration environment, and the other is to directly save your own configuration file, and then, if there is a new project next time, just import it directly.
So, just repost someone else's article, save it, and test it yourself, except that the original text of the attribute manager is not correct, I have modified it here, and everything else is correct. The original link is here: win10 + vs2017/2019 + OpenCV4.2 configure OpenCV development environment

text

1. Install Visual Studio 2017/2019

At present, the latest version of Visual Studio is Visual Studio 2019. Readers can install it according to their preferences. VS2017 is already installed on my computer, so I will not install the new version.

2. Configure OpenCV

2.1 Download OpenCV

Log in to the OpenCV official website to download OpenCV
opencv
and download the corresponding version. Note that the 4.x version can only be compiled under 64-bit

2.2 Install OpenCV

Open the downloaded installation package and select the path to store.
My path is D:\Lib, here we should pay attention to avoid the Chinese path, otherwise there will be errors.
decompress
Click Extract to decompress.

3.VisualStudio configures OpenCV

In fact, it is essentially the same as configuring other third-party libraries, configuring the static library into the dependent directory. If it is a Linux system, it is just a Makefile, which becomes a configuration environment in VisualStudio.

Two ways to configure the environment:

  • Method 1: Global configuration is a once-and-for-all method. It only needs to be configured once, and every time it is opened after that, it is configured; but the disadvantages are also obvious, and building another project that has nothing to do with this OpenCV will also be affected.
  • Method 2: Configure the attribute table, which needs to be added every time a new project is created.

3.1 Add environment variables

The first thing to do is to add OpenCV to the environment variable.
My operating system is English. Let’s take a look.

Computer—>Properties—>Advanced System Properties—>Advanced—>Environment Variables
environment variable
Find Path, edit, create or browse the OpenCV folder you just unzipped. D:\Lib\opencv\build\x64\vc15\bin
There are two folders under x64 vc14. vc15The difference is that vc14 is compatible with VS2015 and VS2017. vc15 is compatible with VS2017 and VS2019

添加完环境变量后需要注销或者重启一下系统,否则后面怎么配都会出问题
insert image description here

3.2 Configuration project directory

Create a new project or an empty project.
New Empty Project
First change the X86 of Debug above to X64. Opencv4.0 officially does not provide X86. If necessary, you should be able to cmake it yourself (haven’t tried it, and I’m irresponsible).
Then find the property manager, if you can't find it on the interface, select Other Windows-> property in the "View" column above. Open Debug|X64.

Next, you can set global or individual project configurations according to method 1 and method 2

  • Method 1:
    Double-click "Microsoft.Cpp.x64.user" to open the property page;

  • Method 2:
    Open the property manager:
    insert image description here

  • there is a属性管理器

  • Right-click "Debug|X64", select "Add New Project Property Sheet", enter the name and click OK. (the name suggestion has a definite meaning, like mine OpenCV420DebugX64)

  • You can also set a Release version
    Release

3.3 Configure inlcude directory

Double-click the property page OpenCV420DebugX64, and then start configuring the directory INCLUDE

Double-click the property sheet and add in General Properties—>VC++ Directories—>Include Directories:
...opencv\build\include
...opencv\build\include\opencv2

In this way, you can find the header file of OpenCV, and then set the static library.
include

3.4 Configure static library

Configuring LIB is also a static library, which is to copy some compiled execution program segments into our generated program. Contrary to dynamic libraries, static libraries can ensure that our programs can run smoothly when transplanted to machines that do not have OpenCV installed.

Add in General Properties—>VC++ Directory—>Library Directory:
...opencv\build\x64\vc15\lib
insert image description here
Add in General Properties—>Linker—>Input—>Additional Dependencies:
opencv_world420d.lib
// This line is not necessary to add if it is under Release, with d It is the other version used under Debug.
opencv_world420.lib
Just change the number "420" to the corresponding version according to its own version number (420 means version 4.20).
lib
After the Debug mode is set, don’t forget to set the Release mode as well, otherwise it will be embarrassing when using Release

So far, even if VS2017 configures Opencv4.2, it is done. If you use method 1 to configure the attribute table, you can use it directly without configuring it every time you create a new project. Note when using method 2 to configure the attribute table:
open the project directory folder, you can see the configuration file you just set up yourself.
configuration file
This is the attribute table we configured. You need to copy it to In this folder corresponding to the new project.

Then right-click "Debug|X64" (or the folder corresponding to the property sheet) in the property manager of the new project and select "Add..."

Then right-click "Debug|X64" (or the folder corresponding to the property sheet) in the property manager of the new project and select "Add..."
Select attribute table
.

4. Test OpenCV

After the above steps are completed, OpenCV can be used in the project.
Let's write a simple image reading applet to test it.
You can see that the code prompt has been successfully opened.
code hints

#include <iostream>
#include <opencv2/opencv.hpp>>


int main(int argc, char* argv[]) {
    /* Read IMG.*/
    cv::Mat src = cv::imread("D://code//project//GraduateDesigin2020//TestPic//lena.jpg");

    /* Show IMG.*/
    cv::imshow("Lena", src);

    /* Convert to grey.*/
    cv::Mat gray;
    cv::cvtColor(src, gray, 6);
    cv::imshow("Gray Lena", gray);

    cv::waitKey(0);
    return 0;
}
12345678910111213141516171819

Look at grandma Lena
you're done
, you're done, go to CHH to watch some young ladies
Miss Liza

reference

Guess you like

Origin blog.csdn.net/qq_43211060/article/details/128126305