Mujahid Daud Khan Mujahid Daud Khan - 9 days ago 5
C# Question

calling opencv c++ code in C# application

I have developed a c++ program which uses OpenCV. Now i want to develop a windows form based application in C#.
As C# can only handle managed code it is nearly impossible to run OpenCV directly on C# application. I have searched for different ways to create C# application using OpenCV, one of which is EmguCV and the other method that i am much more interested in is importing the c++ .dll file in C# application and calling the unmanaged functions this way.

I started by creating simple functions in c++ and i was able to use

cout
and
cin
in my C# application by importing the dll. The problem comes when i try to include OpenCV header files in my c++ application and when i compile i get this error


error LNK1104: cannot open file 'tbb_debug.lib'


Some one has done this before but i cant figure out how he interface c++ with C# in
Displaying webcam feed in cv::Mat format in a picturebox

The Question is that i have function which takes in a cv::Mat variable and performs some image processing on it and returns the processed matrix. i want to use that function written in c++ in my C# application. but the problem is that i am unable to create the dll when i include OpenCV library in c++.


So please Don't Suggest me to use EmguCV or any other .NET wrappers
for OpenCV.


i am using Visual Studio 2010 for my Project.

Answer

I would explicitly export methods that wrap your entry points in a C++ header, and then use P/Invoke to reference them:

extern "C" __declspec(dllexport) BOOL DoSomething();

Then consume them in the C#:

[DllImport("MyOpenCVWrapper.dll")]
private static extern bool DoSomething();

I wouldn't try to reference the OpenCV headers.

Comments