Adding OpenCV 4.5.5 to Visual Studio 2022

Environment

  • Windows 10 (64-bit)
  • Visual Studio 2022

1. Download and extract the pre-built library

Download the latest binary from opencv’s Github repository.
Release files for 4.2.0 are listed at https://github.com/opencv/opencv/releases/tag/4.5.5.

Github Release Page

Run the downloaded .exe file to extract the archive.

For the sake of uniformity, this tutorial will assume that you’ve extracted the contents to c:\.
If you’d like to use a different path, it is completely fine. You simply need to adjust the paths in the later sections of this post.

Extract to C:\

Extracted OpenCV directory in C:\

2. Add to path

Add opencv’s bin directory to path. If you’ve extracted the downloaded .exe file into c:\, the path to the bin directory should be C:\opencv\build\x64\vc15\bin.

Add to path

3. Create a project in Visual Studio 2022

First, select Create a new project.

New Project Window

Project Properties

VC++ Directories Tab
In Include Directories window, add c:\opencv\build\include.

Add c:\opencv\build\include

Click OK. In the same tab, look for Library Directories. Again, click on the down arrow and select <Edit…>.

Again, in VC++ Directories tab, but a different item
c:\opencv\build\x64\vc15\lib
Add c:\opencv\build\x64\vc15\lib

The VC++ Directories tab should look like below:

It’s time to list the module dependencies. As mentioned above, we are only going to add a single all-in-one module named opencv_world.

Linker — Input — Additional Dependencies

opencv_world450d.lib
Add opencv_world450d.lib

The Linker tab should look like below:

We now should be ready to write some OpenCV code and see it in action.

4. Check out demo code!

Copy and paste the code below and press F5 to run (or click on the Run button with the label “Local Windows Debugger”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
Mat image = Mat::zeros(300, 600, CV_8UC3);
circle(image, Point(250, 150), 100, Scalar(0, 255, 128), -100);
circle(image, Point(350, 150), 100, Scalar(255, 255, 255), -100);
imshow("Display Window", image);
waitKey(0);
return 0;
}

Code

Result Window

But wait — I’m still having issues!

Please go over the previous steps carefully to see if you’ve made a typo or missed a step.
Also, if you’re getting an build error with the message “LNK 1104: Cannot open file ‘opencv_world4xxd.lib’, go to the lib directory at C:\opencv\build\x64\vc15\lib to check that the opencv_world450d.lib file exists. If it does, make sure the file name matches the additional dependency we’ve added to Linker Inputs.

A possible error message you may encounter

Check the file name

Check if the file name matches the additional dependency we’ve added earlier


Adding OpenCV 4.5.5 to Visual Studio 2022
https://www.hardyhu.cn/2022/02/24/Adding-OpenCV-4-5-5-to-Visual-Studio-2022/
Author
John Doe
Posted on
February 24, 2022
Licensed under