Skip to content

Instantly share code, notes, and snippets.

@mousetraps
Last active December 22, 2015 23:47
Show Gist options
  • Save mousetraps/865c5b689150772cdd35 to your computer and use it in GitHub Desktop.
Save mousetraps/865c5b689150772cdd35 to your computer and use it in GitHub Desktop.

Ok, so I figured out and I have it working in both C++ and C# depending on which language you prefer.

Here are the steps for each, and if you’re curious how I debugged it, I described what I did below. Note that I’m in Visual Studio Pro, not Express so certain options might be in different places.

Let me know if this still doesn’t work….

C++

  1. Download latest stable version of OpenCV (I used 2.4.2) http://sourceforge.net/projects/opencvlibrary/files/latest/download?source=files

  2. Extract it to the folder you want OpenCV in (you will reference this folder later, so don’t just keep it in downloads)

  3. Open up the command prompt, run “cmake .” in the OpenCV folder you just extracted. A bunch of new files should appear

  4. Open up the OpenCV.sln file in Visual Studio

  5. Switch to “Debug” or “Release” mode depending on which you want. “Debug” is a bit slower but it will make it easier to…. Well… debug... but use release if you plan on putting it in production. It’ll remove all the debug lines, etc.

  6. Updated PATH environment variable to include “<path_to_extracted_openCV>\bin\Debug” and removed anything that corresponded to the previous attempt to make this work

  7. Create a new project (C++, Win32 console)

  8. In your project (in Visual Studio)… Visual Studio (note that this is only for debug mode, you’d have to do the same but with the “Release” for release mode builds 1. Under VC++ Directories in Project Properties:

    1. Update “include directories” to include “<path_to_extracted_openCV>\build\include”
    2. “Library Directories” to include “<path_to_extracted_openCV>\lib\Debug”
    3. “Source Directories” to include
    “<path_to_extracted_openCV>\modules; <path_to_extracted_openCV>\modules\core; <path_to_extracted_openCV>\modules\highgui; <path_to_extracted_openCV>\modules\ml;$(SourcePath)”
    

    And of course, include more modules if your code requires it. 2. Under Linker > Input

    1. Additional Dependencies: “opencv_legacy240d.lib;opencv_core240d.lib;opencv_highgui240d.lib;%(AdditionalDependencies)”(And add more dependencies if your code requires it)

Link to my Hello World solution (note paths in references might be different on your computer)

C# (EmguCV)

EmguCV is a C# wrapper, and this one is easier. And C# is a nicer language, especially if you already know Java well.

I had a few issues with this until I found this link: http://www.codeproject.com/Articles/257502/Creating-Your-First-EMGU-Image-Processing-Project

And use Emgu version 2.3 – I had problems with 2.4 because I’m using Windows 8 but you’ll also have problems with Visual Studio 2012 on Windows 7 I believe. 2.3 is safer. Choose x86 or x64 binaries depending on which architecture you’re TARGETING

If you want to run the code on multiple platforms (non-Windows machines), look into Mono. EmguCV is compatible with it, and many high profile cross-compatible apps have been built using the Mono framework.

Link to my Hello World solution (note paths in references might be different on your computer): http://sdrv.ms/XZwoAJ

Resources


Making C++ work

C++… so this was actually kind of fun to debug and I wanted a record of what I did, so I wrote it out, but I figured I might as well share rather than making it look like magic J. First I reproduced your problem. I went, downloaded the .exe binaries for OpenCV 2.2, and added the .lib files etc. to the Visual Studio linker.

I ran the OpenCV hello world application in debug mode in visual studio to no avail. The message was a little cryptic, didn’t point to any line in the code, so it wasn’t likely that and had more to do with the configuration.

Under “additional dependencies” in the OpenCV_HelloWorld, recall that you added a bunch of lines like “opencv_core220d.lib”. Notice the “d” at the end. This means that they are the debug libraries. So first thing I tried was just switching it to the non-debug libraries by removing the ‘d’

It worked.

Okay, now we could stop there, but it’s kind of a pain not to be able to dig into the code if it’s not working. So I tried to figure out why the debug libraries weren’t working.

Next, I ran a tool called “Dependency Walker” on the .exe generated by running in debug mode. image

I tried it on the .exe generated in release mode (the one that did work), and didn’t have the side-by-side configuration errors.

Hmm… side-by-side configuration…. http://www.codeproject.com/Articles/43681/Side-by-Side-Configuration-Incorrect

That led me to a tool called SxSTrace that ships with Visual Studio Command Line tools. image

I opened up the file and one part in particular caught my interest image

I looked up Microsoft.VC90.Debug.CRT, turns out that’s a Visual Studio 9.0 thing (VS2008) and I had Visual Studio 10.0, 11.0 DLLs on my system.

First I tried just finding the 9.0 dll but realized that it was going to be getting too hacky and might cause other problems down the line.

That’s when I switched to using CMake. I downloaded and installed CMake, making sure to check the box to include it in the current user path. I ended up downloading both 2.8 and 2.6 because I read it somewhere but 2.8 only probably would have worked on its own. Downloaded and extracted OpenCV 2.4.2, extracted it… “cmake .”, which produced a .sln file, OpenCV.sln…. opened this file, built it IN visual studio using the VC++ compiler in DEBUG mode. I went back to the folder I’d extracted OpenCV too, and found a couple new directories generated (the ones that get generated whenever you build any program) – “bin” and “lib”.

If you open bin and lib, there will be one or two folders – “debug” and “release” depending on which mode you built in.

From here I did a few things to start using the new libraries. I found all this out mainly by comparing the two OpenCV attempts, and mapping the folders in the previous attempt to the folders in the new attempt:

  1. Updated PATH environment variable to include “<path_to_extracted_openCV>\bin\Debug” and removed anything that corresponded to the previous attempt to make this work
  2. In Visual Studio (note that this is only for debug mode, you’d have to do the same but with the “Release” for release mode builds 1. Under VC++ Directories in Project Properties:
    1. Update “include directories” to include “<path_to_extracted_openCV>\build\include”
    2. “Library Directories” to include ““<path_to_extracted_openCV>\lib\Debug”
    3. “Source Directories” to include “<path_to_extracted_openCV>\modules; <path_to_extracted_openCV>\modules\core; <path_to_extracted_openCV>\modules\highgui; <path_to_extracted_openCV>\modules\ml;$(SourcePath)” And of course, include more modules if your code requires it. 2. Under Linker > Input
    • Additional Dependencies: “opencv_legacy240d.lib;opencv_core240d.lib;opencv_highgui240d.lib;%(AdditionalDependencies)”

And tada! It works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment