Getting OpenCV to Work with Visual Studio on Windows

There’s a chance (almost a certainty) that if you’re working in Robotics, Computer Vision or Machine Learning, you are required to work on Linux. In fact irrespective of the field you’re working in, Linux always plays a very important role in the life of a software developer.

There’s always been a tussle between which OS is best but when it comes to engineering and software development, Linux wins hands down and the fact that most engineering tools are based on or developed for Linux OS just proves it further.

But, having worked on Windows systems all my life, I still haven’t warmed up to Linux and I make best attempts to get my applications working on Windows before finally giving up and switching to Ubuntu. I haven’t had the heart to remove Windows from my laptop and instead operate with a dual boot.

Yet, with a lot of effort I got OpenCV working with Windows. Configuring it on Visual Studio was still quite a task. It did take a lot of time and effort and after fishing for information on gazillions of online forum, I finally succeeded. Was it worth it the effort, you’d ask? Windows for me is my comfort zone, so definitely, yeah!

So, here are the steps if you’d want to get OpenCV working with Visual Studio (17 in my case) on Windows.

  1. Download OpenCV. I use OpenCV 3.0.0. There are plenty of setups available.Pick one :)
  2. Setting up the environment variable : Add the OpenCV path

3. On to Visual Studio now!

4. Start Visual Studio as an “Administrator”. It doesn’t work otherwise. Trust me, I tried!

5. Then the usual shebang, “Create a Project”

6. For OpenCV, the configuration manager should be changed to x64 instead of default x86. Irrespective of whether you’re running the code in Debug or Release mode

7. Expect a million errors now :)

8. Go to the properties of the “Project”

i. In C/C++ -> General -> Additional Include Directories, add C:\opencv\build\include

ii. In Linker -> General -> Additional Include Directories, add C:\opencv\build\x64\vc12\lib

iii. Linker -> Input -> Additional Dependencies, add opencv_ts300d.lib;opencv_world300d.lib to the end of the list

And that’s all you need to get OpenCV up and running with Visual Studio on Windows.

Pros:

Visual Studio makes it so much more convenient. It executes cmake automatically.

Cons:

Visual Studios compiler is different from other open source C++ compiler versions and hence there were times when I had a successful build on VS but it crashed on those compiler because OpenCV behaves differently.

P.S : I hope this read helps you solve your OpenCV problems with Visual Studio on Windows. And if there are subsequent issues that I manage solve, I would keep adding them!

Edit:

This is something entirely non-CV related but concerns one of the most basic file operations in C++. There are times when you’re required to iterate over files in a folder. For GNU GCC, there are headers like “dirent.h” but it is not supported by visual C++. The replacement is “filesystem”. Follow these steps to include it:

#include <filesystem>

namespace fs = std::filesystem;

Here’s attaching a small code snippet to show how to iterate across a directory

There is a chance, however that namespace filespace will not be identified by the compiler, it can be fixed by making this small change:

Change the C++ Language Standard to the latest draft standard in C/C++ Language settings.

Happy Coding!

Student for life. An aspiring researcher in CV and ML. Here to share all I’ve done or learnt. https://www.linkedin.com/in/anupama-rajkumar-910a222a/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store