VS2022 configures OpenCV

VS2022 configure OpenCV

1. Environment

OS Version: Microsoft Windows 10 x86-64 based
VS Version: Microsoft Visual Studio Community 2022 (64 位) 
OpenCV Version: OpenCV-4.5.5 (2021-12-30)

2. Download OpenCV

Enter OpenCV official website: Releases - OpenCV to download the latest version OpenCV, or click here to download . The downloaded installation file is as follows:

image-20220529175113727

Extract it to the root directory of the D drive:

image-20220529175315437

Now, your opencvpath is D:\opencv.

3. Configure OpenCV

Search Property Manager, open:

image-20220529175913212

Under the property manager, find Debug|x64the item and right-click to open it 属性.

image-20220529180042595

Click "General Properties -> VC++ Directories -> General -> Include Directories" and select Edit:

image-20220529180257587

Add the following two items:

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

image-20220529180524187

In the same-level directory, edit the "Library Directory" and add the following item:

D:\opencv\build\x64\vc15\lib

image-20220529185655415

Click "Common Properties -> Linker -> Input -> Additional Dependencies" and select Edit:

image-20220529180454993

Add the following item:

opencv_world455d.lib

Click Apply:

image-20220529180858162

Create a new system environment variable:

image-20220529180543779

Add the following variable names and variable values:

opencv
D:\opencv\build\x64\vc15\bin

image-20220529181059729

Under Path D:\opencv\build\x64\vc15\bin, select the following three files:

image-20220529181204411

Copy them into the directory C:\Windows\System32.

Restart the computer to make the environment variable take effect, open it VS2022, and set Debug-x64the mode:

image-20220529181605899

4. Test environment

Copy the test image to your working directory:

midnight

Enter the following test code:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
int main()
{
    std::string image_path = samples::findFile("midnight.png");
    Mat img = imread(image_path, IMREAD_COLOR);
    if (img.empty())
    {
        std::cout << "Could not read the image: " << image_path << std::endl;
        return 1;
    }
    imshow("Display window", img);
    int k = waitKey(0); // Wait for a keystroke in the window
    if (k == 's')
    {
        imwrite("starry_night.png", img);
    }
    return 0;
}

The following output indicates a successful configuration:

image-20220529185357582

Guess you like

Origin blog.csdn.net/Elford/article/details/125034607