Install OpenCV with Visual Studio


In this lesson, I will show you how to install OpenCV 3.3.1 with Visual Studio 2015 on Microsoft Windows 7. But it is almost same for other versions of OpenCV, Microsoft Visual Studio and Microsoft Windows.

If you want, you can refer to my older posts

There are 2 ways to install OpenCV into your computer. One way is to build the OpenCV from the latest source in the GitHub and use it. But for a beginner, the most suitable way is to use OpenCV pre-built libraries. Therefore I am going to discuss with you how to install and configure OpenCV pre-built libraries with your the most popular C++ IDE, Visual Studio.



Finding the System Type of your Windows 7 Computer


Right click on My Computer icon on your Desktop and click Properties in the context menu. Then you will see a window like this.

Finding the System Type in the Properties of My Computer


Here the system type is 64 bit operating system. So the system architecture is x64. If your system type is 32-bit operating system, please refer my old post Installing & Configuring OpenCV 2.4.5 with Visual Studio 2012.



What you should download


First of all, you should have to download Microsoft Visual Studio 2015, if it is not installed in your computer. I download Visual Studio Community 2015 which is a free, fully-featured Visual Studio edition for students, open-source and individual developers for their commercial and non-commercial development activities. (You will need to register by giving your email address to obtain the free product key).

Then you have to download the OpenCV pre-built, self-extracting executable on Microsoft Windows from SourceForge. You can choose whatever version, you want. But I choose OpenCV 3.3.1 which was the latest stable release when I wrote this tutorial.



Installation


First you have to Install Visual Studio 2015 if it is not installed in your computer. Because this step is straightforward, I'm not going to explain it here.

Then double click the downloaded opencv-3.3.1-vc14.exe file which was downloaded from SourceForge and OpenCV pre-built libraries will be extracted to whatever location, you give. I gave C:\ as the Extract To location in the popped up dialog box.

The default compiler type of the Microsoft Visual Studio 2015 is VC14. And the opencv-3.3.1-vc14.exe file also contains OpenCV libraries, built with VC14 compiler. Therefore if you are using the OpenCV 3.3.1 downloaded from SourceForge and Visual Studio 2015, you are good to go.

You can find supporting compiler types of the installed OpenCV pre-built libraries, if you go to C:\opencv\build\x64. In this particular OpenCV pre-built version, the only supporting compiler type is VC14.

If you want to use a newer version of Microsoft Visual Studio, you should change the compiler type of your Visual Studio project to VC14. Please refer this page on MSDN.

The next step is to set up environment variables correctly in your computer.



Set up Environment Variables


  • Open start menu and search for 'edit environment variables' in the search bar in the bottom of the start menu. 

Opening the Environment Variables Window from the Start Menu



  • Then click the 'Edit the system environment variables' link in the top of the search results as highlighted in the above image to open the 'System Properties' dialog box. 
  • In the 'System Properties' dialog box, click the 'Environment Variables...' button window as highlighted in the below image to open the 'Environment Variables' dialog box. 


Opening the Environment Variables Window from the System Properties window



  • Click 'New' button at the bottom of the Environment Variables window to create a new environment variable.

Creating a new Environment Variable



  • Type OPENCV_DIR against Variable name field and type the location C:\opencv\build\ against the Variable value field in the popped up dialog box. Please note that I have installed OpenCV pre-built libraries into C: location. If you have used another location as the OpenCV installation location, the Variable value field should be updated accordingly. (e.g. OPENCV_DIR=<OpenCV installation location>\opencv\build\). 

Inserting an Environment Variable Name and Value



Hooray!! You have completed the installation of OpenCV. Next thing you have to do is to configure Visual Studio to use OpenCV pre-built libraries in your computer vision application.



Configure Visual Studio Project


  • Start Microsoft Visual Studio 
  • Go to menu item 'File' > 'New' > 'Project...' to open the 'New Project' window 
  • Do everything as highlighted in the below image and click OK. 

Creating a new Visual Studio Project



  • Click 'Next' button in the popped up 'Win32 Application Wizard' dialog box. 
  • Then click 'Finish' button in the next window as shown below. 

Finished Creating a new Visual Studio Project



  • Then right click on the project name (you have entered this name in a previous step) on the solution explorer and then click 'Properties' in the opened context menu to open the Property Pages window. 

Opening the Property Pages of the Visual Studio Project



  • In Property Pages window, choose 'All Configurations' as the configuration and 'x64' as the Platform as shown in the below image. 

Selecting the Configuration and Platform in the Property Pages window



  • Click the 6 places in the given order in the Property Pages window as shown in the below image. Then copy and paste $(OPENCV_DIR)\include in the opened dialog box and click OK. 

Opening the dialog box to update Additional Include Directories



Updating Additional Include Directories



  • Click the 6 places in the given order in the Property Pages window as shown in the below image. Then copy and paste $(OPENCV_DIR)\x64\vc14\lib in the opened dialog box and click OK. 

Opening the dialog box to update Additional Library Directories


Update Additional Library Directories



  • Click the 5 places in the given order in the Property Pages window as shown in the below image. Then copy and paste PATH=$(OPENCV_DIR)\x64\vc14\bin;%PATH% in the opened dialog box and click OK. 

Opening the dialog box to update Environment Variables of the Visual Studio Project


Update Environment Variables of the Visual Studio Project



  • Click the Configuration Manager in the top right corner of the Property Pages window to open the Configuration Manager dialog box. 

Opening the Configuration Manager of the Visual Studio Project



  • In the Configuration Manager dialog box, set the solution platform to x64 in both locations as highlighted in the below image. Then click Close button to close the Configuration Manager dialog box. 

Updating the Platform of the Visual Studio project in the Configuration Manager Window



  • Change the Configuration of the Property Pages window to 'Debug' as shown in the below image. 

Changing the Configuration of the Property Pages window



  • Click the 6 places in the given order in the Property Pages window as shown in the below image. Then copy and paste the opencv_world331d.lib in the opened dialog box and click OK. Please verify that this filename can be found in the %OPENCV_DIR%\x64\vc14\lib location. 

Opening the Dialog box to update the Additional Dependencies of the Visual Studio Project


Updating the Additional Dependencies of the Visual Studio Project



  • Change the Configuration of the Property Pages window to 'Release' as shown in the below image. 

Changing the Configuration of the Property Pages to Release



  • Click the 6 places in the given order in the Property Pages window as shown in the below image. Then copy and paste the opencv_world331.lib in the opened dialog box and click OK. Please verify that this filename can be found in the %OPENCV_DIR%\x64\vc14\lib location. 

Opening the Dialog box to update the Additional Dependencies of the Release Configuration


Updating the Additional Dependencies of the Release Configuration



  • Click OK button in the bottom right corner of the Property Pages window to save all configurations you have done in previous steps. 

Pressing OK button to save the changes done in Property Pages window



Congratulations!! You have done with all of the configurations of your visual studio IDE. Now you are ready to write your first HelloWorld OpenCV application.



Your First OpenCV HelloWorld Application


#include "stdafx.h"

#include <opencv2/opencv.hpp>

#include <iostream>
#include <string>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
 // Read the image file
  Mat image = imread("D:/My OpenCV Website/Eagle.jpg");

  if (image.empty()) // Check for failure
  {
   cout << "Could not open or find the image" << endl;
   system("pause"); //wait for any key press
   return -1;
  }

  String windowName = "My HelloWorld Window"; //Name of the window

  namedWindow(windowName); // Create a window

  imshow(windowName, image); // Show our image inside the created window.

  waitKey(0); // Wait for any keystroke in the window

  destroyWindow(windowName); //destroy the created window

  return 0;
}


Copy and paste above simple code snippet into the text area of your visual studio project and run it. Please note that you have to replace "D:/My OpenCV Website/Eagle.jpg" in the code with a valid location to an image in your computer. Then you should see a output like the below image.

(If this is not working, try restarting the Visual Studio IDE)


Output of the First HelloWorld OpenCV application in Visual Studio



Next Lesson : OpenCV C++ API



29 comments:

  1. This tutorial series is very helpfull!

    ReplyDelete
  2. Nice tutorial, so helpfull. I want to ask something, each time I create a project I have to configure all the steps above, why this configuration cannot be saved?

    ReplyDelete
    Replies
    1. You can save the settings in a property sheet. For all your new vs projects, you may use this property sheet easily.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Hi Shermal Fernando and thank you for this tutorial! How can I save the settings in a property sheet?
      Thanks :)

      Delete
    4. Please refer to https://msdn.microsoft.com/en-us/library/669zx6zc.aspx#Anchor_6

      Delete
    5. Hi Shermal ! I create a property sheet in a project (project 1) ; then I create
      another project (project 2) and in this one I add the existing property sheet created previously in project 1; why it doesn't works? Merry Christmas

      Delete
    6. Please share the output when you try to compile the code

      Delete
  3. thank you very much!

    ReplyDelete
  4. Hi
    This page and your other tutorial pages could have been a lot more helpful, but you left out the downloads for your projects. Yes its good to know how to set it up step by step, but a simple project download would have got the user up and working a lot faster and made the tutorials a lot easier to go through. Having fixed folders on c:\ drive would have been fine.

    Nonetheless thanks for the effort still useful!

    Brian

    ReplyDelete
  5. Very helpful! But one question: Debugging works fine and it shows me the image I've entered. But the generated Hello World.exe in the 'Debug' folder of my project does not work. A system error occurs and tells me that opencv_world340d.dll is missing on my computer.. But when I run the program in VS it is obviously not. Can you help me? Thank you in advance.. :)

    ReplyDelete
    Replies
    1. When you run the program inside VS, VS add the %OPENCV_DIR%\x64\vc14\bin to the PATH environment variable. Therefore the Windows OS can find the opencv_world340d.dll when the program is running.
      But if you try to run the Hello World.exe outside the VS, the Windows OS cannot find the opencv_world340d.dll because PATH environment variable does not contain a path where this dll can be found.
      Therefore 2 options available for you. One is to copy and paste the opencv_world340d.dll from %OPENCV_DIR%\x64\vc14\bin location to the location where the Hello World.exe binary is. Other option is to append '%OPENCV_DIR%\x64\vc14\bin' to the PATH environment variable.

      Delete
  6. Hello - I am getting the library linking error "cannot find the opencv_world331d.lib file". I have followed each step to the letter. What am I doing wrong on the linking step?

    ReplyDelete
    Replies
    1. Also thank you for the step by step.

      Delete
    2. Have you followed following steps correctly.

      1. Change the Configuration of the Property Pages window to 'Debug' as shown in the below image.

      2. Click the 6 places in the given order in the Property Pages window as shown in the below image. Then copy and paste the opencv_world331d.lib in the opened dialog box and click OK. Please verify that this filename can be found in the %OPENCV_DIR%\x64\vc14\lib location.

      Delete
    3. first check opencv_world331d.lib in c:\windows\system32 folder. if this file doesn't exist then download this file and copy and paste it in system32 folder.then rerun your program

      Delete
  7. Simple effective descriptions worked on first trial... thank you

    ReplyDelete
  8. Thankkkkk youuuuuu,loved it..so easy!!

    ReplyDelete
  9. this tutorial is very helpful and simple
    thanks a lot the team

    ReplyDelete

  10. hello I've done all steps but at the end my code didn't run.

    Error C1083 Cannot open include file: 'opencv2/opencv.hpp'...........

    ReplyDelete
    Replies
    1. Please go to $(OPENCV_DIR)\include\opencv2 location and verify that the opencv.hpp file is present

      Delete
    2. Hi, I have the same problem here but I do have the opencv.hpp file present. What can be the problem ?

      Delete
    3. I have the same problem.

      Delete
  11. Can you please describes their entire steps to install opencv in centos to execute this code ?
    because I am suffering from not found exception.

    ReplyDelete
  12. Thank you, it works for me.
    It was easy to use and helpfull.

    ReplyDelete
  13. I am getting system cannot find the file specified error can you please help me to solve it?

    ReplyDelete