So, I’ve been wanting to play around with the OpenCV Open Source Computer Vision tools. I thought this counting cars video pretty clever.

So with a new installation of Windows 10 (64bit), I thought let’s see what we can do.

Question #1: What Integrated Development Environment (IDE) should I use for the project? For now I’m assuming a no cost open source solution. Here are a few I looked closely at:

  • CodeLite (95 questions in stackoverflow)
  • Code::Blocks (2.3K questions in stackoveflow)
  • Windows Visual Studio (49K questions in stackoverflow)

I started this journey working with Codelite, but it soon became obvious I had no idea what I was doing when it came to using the library. I couldn’t tell if my failures were because of the IDE tool or my poor understanding of the OpenCV library and its compilation. Oh, and using either Codelite or Code::Blocks would imply using the C++ compiler suite from TDM-GCC So then, I figured I start instead with something that would be likely to have a robust error / help lookup for things gone wrong. Based on the Stackoverflow #’s, Microsoft’s Visual Studio it is. Note, it took me a full day and a half of dinking around to come to this conclusion.

Step #1: Install Microsoft Visual Studio Community 2015.

  • First stop is Join Visual Studio Dev Essentials Hit the Join button, sign up for the service, then select the Visual Studio Community Full Featured Extensible IDE. Go ahead and install it.
  • There was one surprise here. The download package only offered up a 32bit (x86) version of the software. There was no choice for the 64 bit version. Oh, well. As I understand it, that means whatever compiler you use for the IDT tool has to match (x86 versus x64 versions). Drive on.
  • Update, 19 Jun 2016. So apparently you can download Visual Studio Community 2015 with Update 2 (x86 and x64) – Released: 3/29/2016… That means you can download a 64bit version. Awesome.

Step #2, C++ Compiler.

  • Probably best to attempt to run a simple C++ Hello_World sample right away.
  • From top menu, File –> New –> Project. You get a pop up window, New Project.
  • Go ahead and select Win32 Console Application. Accept all the defaults.
  • #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int main(){
        cout << "Hello World\n";
        return 0;
    }
  • The first time you go to build and run this application it will fail. Why is that? Oops, because we didn't have any compatible C++ compiler installed. That doesn't come native with the Visual Studio package. I had TDM-GCC previously installed on this computer, but the Visual Studio doesn't use that at all.
  • The good news is that you should see a clickable pop up / error message that will go ahead download and install the package you need, Something called "Windows Software Development Kit - Windows 10.0.xxxxx" In my case, 10.0.26624
  • visual studio problem   visual studio problem2

  • Attempt to run the Hello World project again, and you should see success.

Step #3, Obtain a functional OpenCV library that works with Visual Studio IDE 2015.

  • I spent a whole lot of time here, finding out what doesn't work, so that should make it easier for you to be successful.
  • I initially downloaded the Windows pre-compiled version of OpenCV, available here
  • As of 27 Nov 2015, the date I'm writing this entry, the 3.0.0 windows package from that site only includes the compiled libraries for VC11 and VC12. As I understand it, Visual Studio 2015 uses VC14.
  • I spent a lot of time discovering different ways to generate error messages. Hindsight is 20/20. That precompiled version just won't work with Visual Studio 2015.
  • Save time, don't bother downloading it from there.

Step #3a: Download the Cmake tool.

  • Navigate over to the CMake Download webpage
  • Select the latest release of the Windows (Win32 Installer) .exe version. You definitely want the .exe version, else you won't see the graphic interface tool that makes things so easy.
  • Go ahead and install CMake onto your computer. You will see a CMake (Cmake-Gui) tool in your app list on Win10.

Step #3b: Download the latest version of the OpenCV library directly from the main OpenCV depository at GitHub.

  • If you are not already a Github user, sign up now.
  • Go ahead and clone the depository to your local hard drive. I used the Git Bash Command Line tool for this.
  • Place the cloned copy of the OpenCV library at a convenient location. The C:/ (root) location will be easy to deal with.
  • I've named my directory "c:/opencv_git" to keep it separate from the precompiled version I downloaded previously.
  • There has been some pretty awesome work done in that library. Its set up for very easy compilation.
  • Navigate over to your opencv folder and inside there, create a new directory named "mybuild".

Step #3c: Start the CMake Gui tool.

   cmake3_w_text

  • Where is the source code? = C:/opencv_git
  • Where to build the binaries? = C:/opencv_git/mybuild
  • Hit the configure button.
  • Specify the Generator for the project, the default "Visual Studio 14 2015" is just fine
  • Use default native compilers, Click Finish button.
  • At this point you will see log being generated in the bottom box of the CMake tool. Hopefully you get a completion without errors.
  • You should see a long list of items with red-pink background. Accept the default selections.
  • Finally click on the Generate button to create the new content within your mybuild directory.

Step #3d: Compile the OpenCV library using Visual Studio.

  • Using Windows Explorer navigate over to your mybuild directory.
  • Locate the file "OpenCV.sln" in that directory. Click on the file to open it with Visual Studio.
  • Visual Studio should open, with a populated "Solution Explorer" box.
  • I'd recommend collapsing all the Solution Explorer entries down to their headers, it makes things easier to see.
  • At to top of Visual Studio, note what configuration you are working in Debug or Release . Let's choose Debug first.
  • visual_studio_wText

  • Right Click on ALL_BUILD selection in Solution Explorer, then select Build. This will take a few minutes to run. This will build the appropriate xxxxx300d.lib, xxxxx300d.exp and xxxxx300d.pdb files.
  • Swap configurations (Debug <--> Release) so Release is selected, then Right Click on ALL_BUILD again and select Build. This will build all xxxxx300.lib, xxxxx300.exp and xxxxx300.dll files.
  • After both sets of builds are complete, open up the expand the values for "CMakeTargets" Right click on "INSTALL" and then Build once more. This will join both Debug and Release libraries into a single "lib" and "bin" folder.
  • visual_studio_3

  • My new directories ended up as: C:\opencv_git\mybuild\install\x86\vc14\bin and C:\opencv_git\mybuild\install\x86\vc14\lib. I also see a staticlib directory in there too.
  • This completes the compiled build of the updated OpenCV library.

Step #4, Use our new library to compile and run an OpenCV program within Visual Studio. The only real tricks here are you have to tell the C/C++ compiler where the header files are, and you have to tell the Linker system where the compiled library files are located.

  • In Solution Explorer, right click on the project title, open up Properties.
    SolutionExplorer1wTxt
  • On the Configuration Properties, C/C++, General settings for Additional Include Directories, enter "C:\opencv_git\mybuild\install\include\"
  • On the Configuration Properties, Linker, General settings for Additional Library Directories, enter "C:\opencv_git\mybuild\install\x86\vc14\lib"
  • On the Configuration Properties, Linker, Input, Additional Dependencies, add the following libraries:
    opencv_core300d.lib
    opencv_highgui300d.lib
    opencv_imgproc300d.lib
    opencv_hal300d.lib
    opencv_imgcodecs300d.lib
    opencv_video300d.lib
    opencv_videoio300d.lib
  • NOTE: There are SEPARATE Property entries, depending on if you are in Debug or Release mode.
  • And finally, you have to add the .dll library locations to the Environment Variables / System Variables PATH. In this case I've added both C:\opencv_git\mybuild\install\x86\vc14\bin and C:\opencv_git\mybuild\install\x86\vc14\lib to my Path entry.
  • Reboot the Operating System to engage the new Path.

And there you have it. One thing I do need to mention. The one posting that really helped me figure this stuff out was something I saw over at Mines.edu. Not sure who posted it, but it really helped me move this along.

Finally, here is my OpenCV test program, courtesy of this video tutorial from Kyle Hounslow entitled OpenCV (All Versions) - Easy Installation Guide and Sample Project -- VS 2010 C++

//#include <opencv\cv.h>   // This is the original code, but I couldn't get VideoCapture work correctly.
#include <opencv2/opencv.hpp>
#include <opencv\highgui.h>

using namespace cv;

int main() {
    Mat image;          //Create Matrix to store image
    VideoCapture cap;          //initialize capture
    cap.open(0);
    namedWindow("window", 1);          //create window to show image
    while (1) {
        cap >> image;          //copy webcam stream to image
        imshow("window", image);          //print image to screen
        waitKey(33);          //delay 33ms
    }
    return 0;
}

VideoCaptureHappyFace