Install OpenCV C++ with Visual Studio on Windows PC

This tutorial gives the procedure to install OpenCV C++ in Visual Studio environment. I will be illustrating on a Windows machine, but the procedure will be as well applicable to Mac OS with slight modifications. You may follow the tutorial video on my Youtube channel.

So, without much further ado, we will start the installation.

Step 1: Install Visual Studio 2017

Download the Visual Studio Community, which is a free fully featured IDE. A network installer of only a couple of megabytes, can be downloaded. Running the installer will download typically 2-3 GB of files; the installation will be self explanatory. Just click ‘Next’ and ‘Next’ with express settings.

Step 2: Install OpenCV

Download latest OpenCV package for Windows from opencv official page. It will be an executable file anywhere between 100 to 200 MB. Don’t worry, it’s just an extracter. I would suggest you to extract OpenCV package in your root directory such as C: drive.

Step 3: Include OpenCV to system path

Go to Advanced System Settings > Environment Variables >System Variables > Path.

Click on ‘Edit’. Now, click ‘New’ to add new environment variable.

Copy and paste the path of bin folder inside OpenCV package. The path will look similar to C:\opencv\build\x64\vc14\bin.

Press OK and exit the environment variable dialog by clicking OK again.

Step 4: Create a new empty console application

Open up Visual Studio. Create a new project as a Win32 Console application using Visual C++. Make the Empty project and Finish.

Add a new .cpp file inside the Source Files. This will open up the newly created cpp file in the editor.

Type in the following code inside the editor and save the source file. You may copy paste the entire code from below.

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
  Mat img = imread("lena.jpg");
  namedWindow("image", WINDOW_NORMAL);
  imshow("image", img);
  waitKey(0);
  return 0;
}

Download a color image from Google. I downloaded Lena image by just searching for lena.jpg 

Save the image inside your project location.

Step 5: Include OpenCV in Visual Studio

Till now, the opencv c++ functions & variables are not identifiable by visual studio. This is due to the fact that we haven’t linked opencv with visual studio.

Before proceeding, make sure solution platform has been chosen correctly. I am using x64 version hence, I toggled the Debug environment to x64.

Inside Properties of the project,

then C/C++ > General. Copy the path to include folder of opencv and paste it inside Additional Include Directories. The path will look similar to C:\opencv\build\include. Then, click Apply.

Go to linker > General. Copy the path to folder containing opencv  lib files and paste it inside Additional Library Directories. The path will look similar to C:\opencv\build\x64\vc14\lib. Then, click Apply.

Go to Input. Edit Additional Dependencies and paste the .lib file’s name. Choose the .lib file according to your configuration. For debug mode, the file ends with ‘d’, e. g., opencv_world341d.lib.. Then, click Apply.

Exit the Properties by clicking OK.

Note: make sure you edit the properties, using Configuration and Platform same as in which the source file is saved.

Step 6: Test the code

Build the code by clicking Build Solution or just pressing Ctrl+Shift+B on keyboard. This should end up with output “Build Succeeded”.

Run the Local Windows Debugger. This should display the image on a new screen. Making any keypress will make the make window close.  This signifies that you made success to install OpenCV C++ with Visual Studio on your Windows PC.

Wish you Good Luck for computer vision projects !

 

 

 

 

 

You may also like...

15 Responses

  1. PM says:

    nice tutorial!

  2. Sajad says:

    Thank you
    It would be great if you could share a sample project file with us.

  3. I like the helpful info you provide in your articles. I’ll bookmark your blog and check again here regularly.
    I am quite certain I’ll learn many new stuff right here! Good luck for the next!

  4. Paul C says:

    Great blog and thanks for putting together these tutorials. They are super helpful for those of us just diving in to computer vision and open-source coding.

  5. PolishMIDAS says:

    Thanks to you I finally managed to get it working! You’re great!

  6. The Dank Yeet says:

    WHERE IS ADVANCED SETTINGS?!?

  7. Nolan Baldwin says:

    You may experience an error where VS can’t find the dll and asks you to reinstall the program. To fix this try restarting your computer to update the system path. 🙂

  8. Johanna says:

    unfortunately this is not working for me.
    I have done everything as described in the tutorial. But I keep getting the error that the image img cannot be read. img is always empty. The path I provided is correct. I am working with Win10 and Visual Studio 2019. Any suggestions on what else I could try?

  9. Your article about Install OpenCV C++ with Visual Studio on Windows PC
    is the best I have read!
    I found a way to make money from home that will interest you,
    I leave the link here: https://bit.ly/Teaching-Online
    The deciphertechnic.com site is interesting and useful, keep it that
    way! Kisses everyone! 🙂

  10. mina says:

    hi

    I do all the above steps but when build ther are this errors
    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol “private: char * __cdecl cv::String::allocate(unsigned __int64)” ([email protected]@[email protected]@[email protected]) referenced in function “public: __cdecl cv::String::String(char const *)” ([email protected]@@[email protected]@Z) loadimage E:\my opencv\loadimage\loadimage\loadimage.obj 1

    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol “private: void __cdecl cv::String::deallocate(void)” ([email protected]@[email protected]@AEAAXXZ) referenced in function “public: __cdecl cv::String::~String(void)” ([email protected]@@[email protected]) loadimage E:\my opencv\loadimage\loadimage\loadimage.obj 1
    Severity Code Description Project File Line Suppression State
    Error LNK1120 5 unresolved externals loadimage E:\my opencv\loadimage\x64\Debug\loadimage.exe 1

  11. continuously i used to read smaller articles that
    also clear their motive, and that is also happening with this post which I
    am reading at this time.

Leave a Reply

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

1 × three =