Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling opencv c++ code in C# application

Tags:

c++

c#

opencv

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.

like image 848
Mujahid Daud Khan Avatar asked Nov 21 '12 12:11

Mujahid Daud Khan


People also ask

Can I use OpenCV in C?

OpenCV is a popular Computer Vision library to develop applications built using C++ and C. It has several uses like Object Detection and Video Processing. Computer Vision overlaps with fields like Image Processing, Photogrammetry, and Pattern Recognition. A popular wrapper Emgu CV is used to run OpenCV using C#.

Why is OpenCV called cv2?

cv2 (old interface in old OpenCV versions was named as cv ) is the name that OpenCV developers chose when they created the binding generators. This is kept as the import name to be consistent with different kind of tutorials around the internet.

Is OpenCV an AC library?

OpenCV [OpenCV] is an open source (see http://opensource.org) computer vision library available from http://SourceForge.net/projects/opencvlibrary. The library is written in C and C++ and runs under Linux, Windows and Mac OS X. There is active development on interfaces for Python, Ruby, Matlab, and other languages.

Is OpenCV a module?

This module covers various image processing operations such as image filtering, geometrical image transformations, color space conversion, histograms, etc. In the Java library of OpenCV, this module is included as a package with the name org. opencv.


2 Answers

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.

like image 166
tillerstarr Avatar answered Oct 26 '22 23:10

tillerstarr


Although it might not be what you're looking for, I did that task with C++/CLI and exposed it through an assembly. It was straight forward and worked pretty well. Blog article here.

like image 25
plinth Avatar answered Oct 26 '22 22:10

plinth