Install OpenCV C++ with Visual Studio

In this article, we will see the procedure of installing and linking OpenCV C++ to Visual Studio on Windows PC.  This procedure will be the same for PC with Mac OS with some minor modifications. We will also see how to create and execute projects in Visual Studio. So, let’s get started to install OpenCV C++ with Visual Studio.

Step 1: Install Visual Studio

  1. Go to the official Microsoft Visual Studio site.
  2. Select Visual Studio IDE community edition.
  3. It will download the .exe file of the visual studio for you.
  4. Now, install the visual studio. Installation steps are easy and self-explanatory.

Step 2: Install OpenCV

  1. Go to the official OpenCV site.
  2. Click on the Releases tab from the top menu bar. You will see all the releases of OpenCV.
  3. Select the Windows option of the desired OpenCV release.
  4.  It will download the .exe file of the OpenCV. It is just an extractor.
  5. Extract openCV in the C directory of your PC.

Step 3: Include OpenCV to the system path

  1. Go to advanced system settings > Environment Variables.
  2. Now, under the system variables section select Path and click on Edit.
  3. Click on New and enter the path of the bin folder inside the OpenCV package. The path should be something like this ( depending on your installation location) :C:\opencv\build\x64\vc14\bin.
  4. Press OK and exit the environment variable window clicking OK.

now we will see how to create and execute projects in Visual Studio.

Step 4: Create a new empty console application

  1. Open Visual Studio.
  2. Click on Create a new project.
  3. Select Console App and press next.
  4. Type project name and click on Create.

Step 5: Include OpenCV in Visual Studio

  1.  Select the debug environment as per your PC specifications. I am using the ‘x64’ version hence, I have selected x64 as the debug environment.
  2. Now, click on Debug and go to the properties of your project.
  3. Under C/C++ > General section paste path of include folder of OpenCV in front of  Additional Include Directories. The path will be similar to C:\opencv\build\include. Then, click on Apply.
  4. Go to the linker > General and paste the path to a folder containing OpenCV lib files in front of Additional Library DirectoriesThe path will look similar to C:\opencv\build\x64\vc14\lib. Then, click Apply.
  5. Go to linker > Input and enter the name of the library file that ends with ‘d’ in front of Additional Dependencies. The file name will be similar to opencv_world341d.lib. Then, click Apply.
  6. Exit Project Properties by clicking OK.

Step 6: Write and test the code

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
  VideoCapture cap(0);
  Mat frame;
  if (cap.isOpened() != 0)
  {
    while (cap.read(frame))
    {
      namedWindow("Webcam Running", WINDOW_AUTOSIZE);
      imshow("Webcam Running", frame);
      if (waitKey(100) == 27)
        break;
    }
  }
  cap.release();
  destroyAllWindows();
  return 0;
}
  1.  Type the above code (it is code for opening web camera of your PC)
  2.  Click on Build > Build Solution or press  Ctrl+Shift+B on the keyboard to build the code.
  3.  To run the code click on Local Windows Debugger.
  4. Wait for some seconds and the web camera of your PC will open. (Press Esc to close the camera.)
  5. If you get the output then it means you have successfully installed and linked OpenCV to Visual Studio.

Hope you enjoyed learning how to Install OpenCV C++ with Visual Studio.

(*Note: For each new project you must have to perform step 5 before writing your code.)

You may also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *