Contents
  1. I. Software Preparation
  2. 1.1 Download and Extract OpenCV
  3. 1.2 Download and Install Visual Studio
  4. II. Configure the OpenCV Environment
  5. 2.1 Create a Project
  6. 2.2 Configure Properties
  7. 2.3 Configure Environment Variables
  8. III. Test Program

I. Software Preparation

1.1 Download and Extract OpenCV

  I recommend downloading the Windows installer from the official OpenCV website.

Download and Extract OpenCV

Download and Extract OpenCV (2)

  After the download is complete, double-click the file to run it. Make sure to extract it to an English-only path. Wait for the extraction to finish.

Download and Extract OpenCV (3)

1.2 Download and Install Visual Studio

  I am using Visual Studio 2019 Professional here. You can download it from the official website. Other versions of VS will also work; there are no specific requirements.

  During installation, select Desktop development with C++, then change the installation location. You can leave the other settings at their defaults.

Download and Install Visual Studio

Download and Install Visual Studio (2)

After installation, if you need a VS2019 Pro registration key: NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y

II. Configure the OpenCV Environment

2.1 Create a Project

  Create a new console project.

Create a Project

Create a Project (2)

  Set the configuration to Release and the platform to x64, as shown below.

Create a Project (3)

2.2 Configure Properties

  Open View > Other Windows > Property Manager (in other versions of VS, this may be View > Property Manager).

Configure Properties

  In Property Manager, right-click “Microsoft Cpp x64 user” and select Properties to open its property pages.

Configure Properties (2)

  ① Configure VC++ Directories > Include Directories

Configure Properties (3)

  Add two new lines on the edit page. The first is the opencv/build/include directory under the extracted OpenCV folder, and the second is the opencv/build/include/opencv2 directory. After adding them, click OK to return to the property pages.

Configure Properties (4)

  

Configure Properties (5)

  

Configure Properties (6)

  

Configure Properties (7)

  ② Next, configure VC++ Directories > Library Directories

Configure Properties (8)

  Add opencv/build/x64/vc15 on the edit page. After adding it, click OK again to return to the property pages.

If you are using VS2019, the latest vc15 is a better choice. If you are using an earlier version, you can select the vc14 directory.

Configure Properties (9)

  

Configure Properties (10)

  ③ Next, configure Linker > Input > Additional Dependencies

Configure Properties (11)

  Copy the name of the lib file for the Release version and paste it into the edit field.

Configure Properties (12)

  

Configure Properties (13)

  After completing the configuration above, click Apply > OK in the lower-right corner.

Configure Properties (14)

2.3 Configure Environment Variables

  Copy the bin folder path.

Configure Environment Variables

  Return to the desktop, right-click Computer > Properties > Advanced system settings, and open Environment Variables.

Configure Environment Variables (2)

Configure Environment Variables (3)

  Find Path under System variables and click Edit.

Configure Environment Variables (4)

  Add a new entry, paste in the bin folder path you just copied, and then click OK.

Configure Environment Variables (5)

  Close Visual Studio, then reopen it.

III. Test Program

  Add the following code to the cpp file you created:

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

using namespace cv;

int main(int argc, char** argv) {
	Mat src = imread("F:/NutStore/Documents/素材库/Logo/Huffie.jpg");//自己找一张图片
	imshow("input", src);
	waitKey(0);
	destroyAllWindows();;
	return 0;
}

Then build the solution. If there are no errors, congratulations! If an error occurs, study and search for its cause on your own, or carefully check each step above.

III. Test Program

Test > Start Without Debugging

III. Test Program (2)

III. Test Program (3)