Wednesday, March 31, 2010

Configure OpenCV 2.0 on Visual Studio 2008 for VC++

You must be hating OpenCV 2.0 by now :) But don’t worry your pain will go soon (if yes, please write a comment). The biggest pain is that you will have to compile your OpenCV using CMake and all those stuff (as explained here). But if you are content with C or C++ and not interested in debugging inside OpenCV or Python then read further.

Download and Install, Emgu which is actually .NET wrapper for OpenCV but we will just use their precious .lib files which otherwise is pain in neck to generate (Download Emgu from here: http://sourceforge.net/projects/emgucv/)

After Emgu installation, Open Visual Studio and follow these steps:

1. Go to Tool –> Options –> Projects and Solutions –> VC++ Directories

2. Now see a combobox “Show Directories for” and select “Include files” and add, “C:\Program Files\Emgu\Emgu CV\opencv\include\opencv” (verify, as program file location may differ if you are using 64 bit operating system)

3. Now in the same combobox select “Library files” and add, “C:\Program Files\Emgu\Emgu CV\lib\release

4.  Now go to your VC++ project, right click and go to Properties –> Configuration Properties –> Linker –> Input –> Additional Dependencies and add these lib files (which is at  C:\Program Files\Emgu\Emgu CV\lib\release) cv200.lib cvaux200.lib cxcore200.lib highgui200.lib (check library name, it may change according to OpenCV version used for the Emgu)

5. That’s it. Done for now. Take shot at compiling your VC++ code downloaded from internet ;)

NOTE: If you are interested in going into OpenCV library code while debugging then this may not be the best solution for you as Emgu only gives release .lib files. You will have to generate ‘debug’ lib files as described in http://mirror2image.wordpress.com/2009/10/20/switching-to-opencv-2-0-with-vs2005/

I am not sure but this same should help to configure VS 2010 also.

Monday, March 01, 2010

Wondering about System.Int32 implementation

If you are a .NET programmer (and do not use reflector), you would look at int and System.Int32 as same. But if you are also interested in reverse engineering, you would surprise how would they implement something like this.
This is how Int32 (part of mscorelib.dll) may look like in reflector,

namespace System
{
//attributes blah blah blah
public struct Int32  //implements blah blah blah....
{
internal int m_value;
//… other code with methods with ‘int’ as return type or parameter
}
}

But if int and Int32 are same, wouldn’t it become circular reference? (not ‘reference’ exactly has int is value type ;). The answer is NO. as we use them in real life. It seems reflector’s code is hardcoded to show it or mscorelib is compiled in some alien world.

I read[1] that there a special treatment given for 'int' (and so all other keywords). Interestingly, I learned creating my Int32 (and other predefined types). When I created something like this, in my project,
public struct Int32
{
public Int32 m_value;

public Int32 GetMe()
{
return this;
}
}

But after compilation, I saw (in reflactor) compiler did this to my class,
public struct Int32
{
// Fields
public int m_value;

// Methods
public unsafe int GetMe()
{
return *(((int*) this));
}
}

hmm.. Interesting. It looks like (also suggested in link [1]) when the compiler encounter an 'int' or 'Int32' in the structure which cannot be replace by pre-compiler as Int32, it gives 32 bit to variable m_value there. So, basically variable m_value doesn't exist as int or Int32 but it is just 32 bits which can be retrieved using unsafe code as shown above. Combination of visual studio intellisence, pre-compiler, compiler are specially designed to handle this scenario.
Note: For losers using Java, int is value type and Integer is reference type. Both are not same there. What a shame!

[1] C# compiler magic regarding int vs System.Int32 Thread on MSDN