Getting Started With OpenCV in C/C++ in Windows

3 minute read

How to configure OpenCV for C/C++ in Windows?

Installing OpenCV is very hard task because of various things like build error and Compiler Error. So in this tutorial, I will write in brief about how can one install OpenCV for C++ in Windows machine.

Getting Systems Ready!

  • First check your system if it is 64-bit or 32-bit because we will need separate files for each.
  • Download VS-Code for your system. The reason for using VS Code is because of its features. It provides extensions like C/C++, Cmake, Intellsense and it even makes our things easier as it builds our code with just single click.
  • Download and install Visual Studio 2019 (not the VS Code) is also installed and during the installation, make sure to download feature Desktop Development with C++.
  • Download and Install Cmake from Cmake is a build tool which will work on backend to make a executable file for us. If we want to build any file without using VS code then we must use Cmake from CMD.
  • Download and Install mingw (32-bit or 64-bit according to your system) from, it provides us the compiler. Also remember the installation path.
    • Now lets add mingw in system variable so that the path could be accessible from anywhere.
    • For me the paths are just under C drive.
      • C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin
      • C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0
    • If looked closely. ....\bin folder contains compiler g++, c++ and lots of other tools.
  • Download and Extract OpenCV from to C drive. The sole purpose of choosing C drive is because of simplicity.
    • Again we have to add the build path (both lib and bin) into the system variable.
      • C:\OpenCV\opencv\build\x64\vc15\lib
      • C:\OpenCV\opencv\build\x64\vc15\bin
    • If looked carefully inside C"OpenCV\opencv\include\opencv2 we can find lots of *.h C/C++ header files there. Which will allow us to use OpenCV.

Configure Build Operations

  • Start by making a new project on VS Code. i.e. start on new folder.
  • Find a command Cmake Configure. By hitting ctrl+shift+p.
  • Choose your Visual Studio version.
  • A warning will be shown on bottom right, click on create.
  • Give your project a name (like in this case opencv-test).
  • Pick to make executable.
  • Now CMakeLists.txt, main.cpp and build folder will be generated by CMake.
  • On CMakeLists.txt we have to give some build information and informations about OpenCV directories.
    • We have to tell that, find the required package OpenCV and include the directory too.
    • Then add target_link_libraries, i.e. link the libraries to our project.
  • CMakeLists.txt:

          cmake_minimum_required(VERSION 3.0.0)
          project(opencv-test VERSION 0.1.0)
          find_package(OpenCV REQUIRED)
          add_executable(opencv-test main.cpp)
          target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
  • If everything goes okay, then after saving a file, on the output console, it will be shown. If no error is shown then we can proceed.

Create and Run first OpenCV program in C++.

  • Head over to the main.cpp. Copy below and replace the codes.
      #include <iostream> //handeling input/output
      #include <stdio.h> // standard i/o?
      #include <opencv2/opencv.hpp> // main opencv
      #include <opencv2/highgui/highgui.hpp> // opencv gui
      #include "opencv2/imgproc.hpp" //image processing
      #include "opencv2/imgcodecs.hpp" //image codecs i.e. png, jpeg etc
      using namespace cv;
      using namespace std;
      Mat image;
      int main()
          // get full path of any image near your directory!
          string impath="F:\\Desktop\\guiconcept.png";
          image = imread(impath, 1);
          imshow("Image", image);
          return 0;
  • Make sure to change the impath variable!
  • Now press on play button on the bottom, it will take few seconds to build it then if everything is okay, it will show our image.

Why not read more?