Open In App

OpenCV C++ Windows Setup using Visual Studio 2019

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

OpenCV is the Real-Time Computer Vision library which provides various real-time computer vision, video capturing, image processing, and machine learning functionalities. Using OpenCV with Visual Studio you can build robust applications for object detection, image transformation, video capturing, and analysis with fast C++ computations. Follow the given steps and instructions for your OpenCV C++ Windows Setup:

OpenCV Download and Installation:

Follow the below steps to download and install OpenCV on your local machine:

Step 1: Go to https://opencv.org/releases/  and under the latest version (4.5.0) select the windows button. You will be directed to another link and the setup file will be downloaded.

Step 2: Open the downloaded file. A self-extracting zip will open. Before proceeding with installation go and create a folder named OpenCV inside your C:\ drive. Run the installer and extract the zip file to the created folder C:\opencv

Step 3: Now that you have successfully installed OpenCV. The next step is to add binaries to the system path. Open a command prompt with admin privileges and write the command:  setx -m OPENCV_DIR C:\opencv\build\x64\vc15 This will add OPENCV_dir as a system variable.

 Step 4: Open Environment Variables Settings. Go to Path and add %OPENCV_DIR%\bin

OpenCV Setup is done here. The next step is to go ahead with the Visual Studio Project Configurations.

Visual Studio Project Configuration:

Step 1: Start a new Visual Studio project, and choose the C++ Console App template.

Step 2: After the Project has been created change the Debug to x64 platforms since we are using the x64 version at C:\opencv\build\x64\vc15

Step 3: Inside the Project, tab Open the Properties. 

Step 4: Inside Configuration properties select VC++ Directories. The next step is to edit the included directories and library directories to add the OpenCV include directories and library directories. First Select Include Directories then Click on edit. A new window Pops up. Click on Add a new line which is a yellow colored button and enter the path for OpenCV include directories  C:\opencv\build\include

Similarly for Library Directories Select Library directories and repeat the above steps. The only change will be the Library Directories path for OpenCV directories i.e. C:\opencv\build\x64\vc15\lib

After this is done go to Linker/Input inside the Project Properties and edit the Additional Dependencies to include the OpenCV DLL opencv_world450d.lib inside our linker.

Apply all the changes made and close the properties. Your OpenCV setup is done. Now you are ready to build amazing apps using OpenCV in C++. You have to repeat all these property changes every time you start a new OpenCV project with Visual Studio 2019. 

Try this demo code to check your installation works properly.

C++




#include <opencv2/opencv.hpp>
#include <iostream>
  
using namespace cv;
using namespace std;
  
int main(int argc, char** argv)
{
    // Read the image file
    Mat image = imread("Image Address Here ");
    // Check for failure
    if (image.empty())
    {
        cout << "Image Not Found!!!" << endl;
        cin.get(); //wait for any key press
        return -1;
    }
    // Show our image inside a window.
    imshow("Image Window Name here", image); 
  
    // Wait for any keystroke in the window
    waitKey(0); 
    return 0;
}




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads