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:
Extract it to the root directory of the D drive:
Now, your opencv
path is D:\opencv
.
3. Configure OpenCV
Search Property Manager, open:
Under the property manager, find Debug|x64
the item and right-click to open it 属性
.
Click "General Properties -> VC++ Directories -> General -> Include Directories" and select Edit:
Add the following two items:
D:\opencv\build\include
D:\opencv\build\include\opencv2
In the same-level directory, edit the "Library Directory" and add the following item:
D:\opencv\build\x64\vc15\lib
Click "Common Properties -> Linker -> Input -> Additional Dependencies" and select Edit:
Add the following item:
opencv_world455d.lib
Click Apply:
Create a new system environment variable:
Add the following variable names and variable values:
opencv
D:\opencv\build\x64\vc15\bin
Under Path D:\opencv\build\x64\vc15\bin
, select the following three files:
Copy them into the directory C:\Windows\System32
.
Restart the computer to make the environment variable take effect, open it VS2022
, and set Debug-x64
the mode:
4. Test environment
Copy the test image to your working directory:
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: