Configure the latest version of OpenCV and possible error solutions in visual studio 2022 C++

Earlier we wrote a blog post about configuring OpenCV in C#, but the C# version of OpenCV has relatively few learning resources, and there are more C++ and Python versions. Let me talk about how to configure the C++ version first! Complete four steps in total to use it.


Original source of the article: https://blog.csdn.net/haigear/article/details/129617330

1. Download and install OpenCV

1. Download OpenCV

Let's come to the official website . The latest version is 4.7 as of December 22. It doesn't matter which version you install. Now there are more tutorial resources for 4.5 on the market. You can consider 4.5. But I will use 4.7 for demonstration here.
insert image description here
After you come in, remember to slide to the bottom, click the release button below to have the download button, select the corresponding version to download, but I still remind you, don’t use the browser to download, the speed of using Thunder is very fast (some people have a browser The download was only a few tens of K, so I went to the domestic mirror website to download, there is no need for this). My download speed reached about 3M, which should be considered good.
insert image description here

2. Install OpenCV

After the download is complete, we will start to install it. In fact, it is not called installation in the strict sense, it is called decompression. Just choose a directory you like to decompress.
insert image description here
By default, you have already installed the c++ desktop development environment in VS. If you don't have this, simply select and install it in Visual Studio Installer. I won’t go into details here, just post a picture, I’m afraid you installed it wrong!

insert image description here
If you don't install it, your project can't be compiled even if it can be built, so I won't talk about it here! Go install it!
insert image description here

2. Configuration environment

1. External include directory

There are two main configurations, external inclusion directory and library directory and linker.
insert image description here
Don’t get the directory hierarchy wrong, otherwise you will still encounter inclusion files that cannot be found during the compilation phase.

insert image description here
This kind of mistake is because the above directory has entered one level more. When it comes to OpenCV2, it must not be like this
insert image description here

2. Library directory

insert image description here

3. Linker Input: Attachment Dependencies

When attaching dependency configuration, we still copy it under OpenCV\opencv\build\x64\vc16\lib. The directory is the same as the above library directory, but remember, don’t lose the file extension, otherwise the configuration will be invalid. Here if you If you want to publish, copy the lib without d. I need to debug here, so copy the lib file with d.
insert image description here

At this point, our configuration is over, and the opencv project can be compiled normally.

3. Test run OpenCV

1. Cannot find opencv_world470.dll

The first time you run, you may encounter this error. It is because the environment variable has not been configured. Just configure it. Add an opencv bin directory to the path of the external
insert image description here
user environment variable or
insert image description here
system environment variable . Newly added, paste the directory Here you can
insert image description here
remember to restart VS and enter the project again to take effect.

2. An exception is thrown at vcruntime140.dll

If you encounter this error, it is because the lib file at the link input configuration is wrong. You may copy the lib file without d, just modify it.
insert image description here

3. Code and run

The test code is as follows:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main()
{
    
    
   
    std::cout << "Hello World!\n";
    Mat dstimg;
    Mat srcimg = imread("d:\test.jpg");
    cvtColor(srcimg, dstimg, COLOR_RGB2GRAY);
    imshow("showimage",dstimg);

    waitKey();
   

    return 0;
}

The above code just reads the image and turns it into a grayscale image to test whether the CV call is normal.
The running effect is as follows:
original image:
Please add a picture description
grayscale image after running:
insert image description here
indicating that the configuration has been completed.

4. Save a configuration PropertySheet

This is relatively simple, that is, in the property management, click the second icon to create a new PropertySheet, double-click this PropertySheet, it is exactly the same as the properties of the project we opened, then you will configure the properties above, such as the input configuration of the linker, external It is easy to configure the directory, directory library, etc. here once, or copy it. This PropertySheet can be imported directly when you create a new project next time, which can save the whole set of tedious configuration work above.

insert image description here
insert image description here
This is very easy, so I won’t be verbose anymore. Remember to save this PropertySheet with a name you remember, put it in a directory that you can easily find, and save it as a valuable “reuse resource”.

Code words are not easy, please indicate the source for reprinting: https://blog.csdn.net/haigear/article/details/129617330

Guess you like

Origin blog.csdn.net/haigear/article/details/129617330
Recommended