Windows10+Cmake+VS2019 compile opencv (super detailed)

Foreword:

Complain about it. . . At the beginning, it seemed that I needed to use VS2022 to compile, but I did not compile successfully after compiling twice. The reason is that only compilers from 2017-2019 are supported. At the beginning, I thought there was something wrong when I was compiling with Cmake. As a result, I wasted time and compiled it again with Cmake, but the result still didn't work. So I chose VS2019.

1. Download Cmake

Just go to the official website and download it! !

Both versions are available, depending on your mood, use whichever one you want.

The link is here for you all. Download | CMake

Let me tell you here, when installing Cmake, don't install it blindly. Take a look during the installation. During the installation process, an environment variable is added to your computer user. You can choose it.

2. Download VS2019

You can go to the official website to download it yourself! ! You can also click on my link.

The link is here for you Downloads - Visual Studio Subscriptions Portal

Depending on your mood, download the one you want.

3. Download the version of opencv

You can go directly to the official website to download, or you can click on my link to download.

The link is here for everyone. Releases - OpenCV

Or multiple choices, hahahaha! !

You can choose the version yourself.

The tutorial uses opencv4.5.5. To download the package, just download the one I framed in the picture below.

4. Download opencv_contrib-4.5.5

The official link is here opencv/opencv_contrib: Repository for OpenCV's extra modules (github.com)

The link to opencv_contrib-4.5.5 is here opencv/opencv_contrib at 4.5.5 (github.com)

Just download the code and unzip it.

5. Unzip the downloaded opencv and opencv_contrib and place them in the same folder, and create a build folder under this folder. as the picture shows

Since compiling by yourself requires a lot of space, it is recommended that you put it on a disk with more space (mine is 13.6G).

6. Open Cmake

 1. After the path is set, click configure, and the interface below will appear.

Choose your own VS compiler version

 

After selecting, click Finish.

Many problems will arise after waiting for the operation to be completed. Don't panic at this time, we will deal with it slowly.

The errors framed in the picture below are python errors. We don't need to worry about this, because in the end we will apply it to c++.

The second is the error in the picture below.

This kind of cmake error needs to be dealt with.

 Looking at the error message, we can find something that is not found and let us download it ourselves. The download links are given to us in the CMakeDownloadLog.txt file under the build folder we created.

The information is as shown below. The information represents everything written on the picture.

Process all the following information with cmake_download in the same way as above.

Sometimes when you open a link, it has this format, as shown below

In this case, just press Ctrl+S to save. It is saved in txt format. After saving, change it to the format we need and it will be ok.

 After completing the above operations, click configure. At this time, check whether there are any errors. Mine reported an error.

Solution: Search for OPENCV_GENERATE_SETUPVARS in search and uncheck its value, as shown below.

 After completion, click configure and wait for the run to complete. If there are other errors, please solve them yourself or leave a message.

After running the ending without any problems, a small part of it is completed. Next, select what we need and search for cuda in the search box. As shown in the picture below, tick everything.

 Then search for opencv_world, as shown below. The function of this is to integrate all the packages together. Although this will cause some packages to be left behind, I personally think it is more convenient, otherwise you will need to add a lot of .lib packages when using it later. .

 Then search for OPENCV_ENABLE_NONFREE and check it, as shown in the figure below.

Then search OPENCV_EXTRA_MODULES_PATH and add the path to the opencv_contrib folder we downloaded to be accurate to models, as shown in the figure below.

After completing the above, click configure. Wait for the run to end. Yes, you will be surprised. There will be many errors, as shown below.

The solution is still the same as the above solution, open  CMakeDownloadLog.txt. There will be more saves this time, so be patient and download. Ha ha ha ha! ! !

After all downloads are completed, click configure. Most of them will not report errors.

Then search for cuda in search, as shown in the figure below, write the computing power of your graphics card in this place.

The link to check the computing power of the graphics card is here CUDA GPUs | NVIDIA Developer

 When finished, click configure

After no error is reported, click Generate, wait for the run to end, and click Open Project. It will start your vs by itself, or you can go to the build to find the Opencv.sln project and start it with vs.

7. After opening the project with vs

As shown in the figure below, right-click opencv_world under models, click Properties, and see Figure 2 for the operation.

After completion, the operation is as shown below.

 At this time, you have to wait for a while, it may be half an hour, or it may be several hours, depending on the performance of your computer.

Don’t panic if the following error occurs after running. This will not delay our use. This error is caused by python.

 

Our opencv above is compiled. After running the Debug version, it is recommended to also run the Release version, so that it can be used in both environments.

8. Configuration environment

First add environment variables on the local computer, as shown below

Then we create a C++ project and right-click the solution, as shown in the figure below. 

 Click Properties. The operation is as shown in the figure

 

 The test code is below for you.

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

int main()
{     cv::Mat src = cv::imread("E:\\image1\\0.jpg");//image path     cv::imshow("show", src);     cv::waitKey (0); }



Guess you like

Origin blog.csdn.net/on_the_fly/article/details/124041070