Code OpenCV in Visual Studio

We will learn how to configure Visual Studio projects and build OpenCV applications.
Code OpenCV in VS

In this article, we explain how to build applications with OpenCV using Visual Studio. We will be using Visual Studio 16 2019. If you write code on Windows, there is a high probability that you work on Visual Studio. It is used to develop computer programs, websites, web apps, web services, and mobile apps.

  1. Step 0: Prerequisites
  2. Step 1: Create an Empty C++ Project
  3. Step 2: Add a new C++ file to project
  4. Step 3: Setup Solution Platform
  5. Step 4: Set Additional Include Directories
  6. Step 5: Set Additional Library Directories
  7. Step 6: Set Additional Dependencies
    1. Debug
    2. Release
  8. Step 7: Set Environment
  9. Step 8: Implement the Project
  10. Step 9: Build and Run the Project

Step 0: Prerequisites

Throughout the post, we assume that you have installed Visual Studio 16 2019, and have installed OpenCV 4.5.0 in C: using the installers. If you have any problems with the OpenCV installation, please check the earlier post.

Step 1: Create an Empty C++ Project

First, we start by opening Visual Studio and creating an empty Windows console project.

  1. Start Visual Studio
  2. Click on Create a new project
  3. Select Empty Project and click on Next
  4. Specify Project Name and Project Location
  5. Click Create

Step 2: Add a new C++ file to project

We can now start adding new .cpp files or import existing code.

  1. Right-click on Source Files, then click on Add→New Item
  2. Click on C++ File (.cpp), specify Name and Location of the cpp file and click Add

Step 3: Setup Solution Platform

The files which we have provided work for x64 configuration.

  1. Set Solution Platform as x64
  2. Right-click on Project and click Properties

Step 4: Set Additional Include Directories

We give paths to the OpenCV header files. This will, in turn, tell the compiler how the OpenCV library looks.

  1. Set All Configuration and x64 Platform
  2. Go to C/C++ → General → Additional Include Directories and Click Edit
  3. Browse to add folders
  4. Navigate and select the OpenCV include folder
  5. Click OK and then Click Apply

Step 5: Set Additional Library Directories

Now we give paths to the OpenCV library files. This will tell the linker where to get the functions or data structures of OpenCV when needed.

  1. Set All Configuration and x64 Platform
  2. Go to Linker → General → Additional Library Directories and Click Edit
  3. Browse to add folders
  4. Navigate and select the OpenCV lib folder
  5. Click OK and then Click Apply

Step 6: Set Additional Dependencies

We need to list the .lib files for the modules which will be used.

Debug

  1. Set Configuration to Debug and x64 Platform
  2. Go to Linker → Input → Additional Dependencies and Click Edit
  3. Specify the list of all lib file for Debug Mode
    • If you have installed OpenCV using the Installers, you will find the list of additional dependencies for Debug mode in the root of your OpenCV installation (debug_files_list.txt)
    • If not, you will have to type all the library files names in the text box
  4. Click OK then Click Apply

Release

  1. Set Configuration to Release and x64 Platform
  2. Go to Linker → Input → Additional Dependencies and Click Edit
  3. Specify the list of all lib files for Release Mode
    • If you have installed OpenCV using the Installers, you will find the list of additional dependencies for Release mode in the root of your OpenCV installation (release_files_list.txt)
    • If not, you will have to type all the library files names in the text box
  4. Click OK then Click Apply

Step 7: Set Environment

  • You don’t have to do this step if you have used OpenCV installers. This step is required if you have installed OpenCV from source.
  1. Set All Configuration and x64 Platform
  2. Go to Configuration Properties → Debugging → Environment and Edit the field
  3. Specify the address to the OpenCV bin folder containing all .dll files, by entering the following value in Environment field
PATH=C:OpenCVx64vc16bin;%PATH%

4. Click Apply then Click OK

    Step 8: Implement the Project

    #include <iostream>
    #include <opencv2/opencv.hpp>
    using namespace std;
    using namespace cv;
    int main() {
        cout << "Hello, World!" << endl;
        cout << "OpenCV version is " << CV_VERSION << endl;
        return 0;
    }
    

    Step 9: Build and Run the Project

    At this point, the project is completely configured and now is ready to be executed.

    1. Select your choice of Solution Configuration
    2. Build the project by pressing Ctrl+B or navigate to Build → Build opencv_example_project
    3. To run the project With/Without Debugging press F5/Ctrl+F5, or Navigate to Debug → Start Debugging/Debug → Start Without Debugging

      Summary

      In this post, we learned how to configure a Visual Studio project to use OpenCV. First we set the Additional Include Directories, which is required for the #include commands- this tells the compiler how the OpenCV library looks. We also set Additional Library Directories- which tells the linker where to get the required OpenCV functions and data structures from. We configured Additional Libraries for Debug and Release mode separately. Finally, we learned how to set the path environment variable for the .dll files and build and run the code.



      Read Next

      VideoRAG: Redefining Long-Context Video Comprehension

      VideoRAG: Redefining Long-Context Video Comprehension

      Discover VideoRAG, a framework that fuses graph-based reasoning and multi-modal retrieval to enhance LLMs' ability to understand multi-hour videos efficiently.

      AI Agent in Action: Automating Desktop Tasks with VLMs

      AI Agent in Action: Automating Desktop Tasks with VLMs

      Learn how to build AI agent from scratch using Moondream3 and Gemini. It is a generic task based agent free from…

      The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

      The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

      Get a comprehensive overview of VLM Evaluation Metrics, Benchmarks and various datasets for tasks like VQA, OCR and Image Captioning.

      Subscribe to our Newsletter

      Subscribe to our email newsletter to get the latest posts delivered right to your email.

      Subscribe to receive the download link, receive updates, and be notified of bug fixes

      Which email should I send you the download link?

       

      Get Started with OpenCV

      Subscribe To Receive

      We hate SPAM and promise to keep your email address safe.​