开发者

How do I use a C# library in C++ OpenCV project with a VB.net GUI?

开发者 https://www.devze.com 2023-02-21 22:33 出处:网络
I have developed an application written in unmanaged C++ that makes use of OpenCV and till now have just been using the built in highgui lib to display the results.

I have developed an application written in unmanaged C++ that makes use of OpenCV and till now have just been using the built in highgui lib to display the results.

Together with VB.net developer I would now like to package it into a product suitable to sell.

The two enhancements I have decided to make are add a GUI, and secondly improve the face recognition module by replacing it with a 3rdParty solution.

The problems are that the 3rdParty SDK only has a C# API, and obviously the other programmer would like to implement the interface开发者_C百科 in VB.

I could port my code to C# and use EmguCV killing two birds with one stone, however the code base is quite large and I dont think I have the time, but it is an option.

What I would like to know is where to go from here. Do I port to MC++, use mixed Native and Managed assemblies, write the GUI in Qt myself, etc

Also how would I display an Iplimage stream in a .NET WinForm control?


Is it possible in your situation to implement some simple, high level API of your OpenCV native code, so that it is easly interoperable with C# using PInvoke? I had similar problem in the past, what I did was to pack my native code with about 15 functions that were easly usable from C# (using PInvoke). I don't know if it's possible to do it easly in your case, if not, you might try to code few classes in MC++ and try to compile your OpenCV code using MC++, afterall, main usage od C++/CLI is to create a bridge between native c++ and managed languages (altough, I personally still prefer to make nice native API and PInvoke it from C#.

As for IplImage, if you are using EmguCV, there's a .Bitmap property that gives you managed System.Drawing.Bitmap class that can be used, without copying underlying buffers (I was rendering 5 live streams from cameras in my application and it worked pretty well). If on the other hand you want to do it manually, without using EmguCV, I encourage you to check their source code and see how they implement this functionality and do it by yourself (basically, you need to grab pointer to memory and get few properties, like image stride, width, height and create Bitmap class by yourself using those).

Edit: I've found some code I've used to render an image that was processed in OpenCV:

C++ code:

void __stdcall GetRectifiedImage(Calibration* calib, int left, int** dataPtr, int* stride)
{
    CvMat* mat = ((CalibrationImpl*)calib)->imagesRectified[left > 0 ? 0 : 1];

    *dataPtr = mat->data.i;
    *stride = mat->step;
}

This code is basically filling dataPtr and stride (assuming that width and height is known)

C# PInvoke:

[DllImport("Stereo.dll")]
public static extern void GetRectifiedImage(IntPtr calib, int left, out IntPtr data, out int stride);

C# usage (with EmguCV):

StereoNative.GetDepthImage(calib, out ptr, out stride);
pictureBox5.Image = new Image<Gray, byte>(640, 480, stride, ptr).Bitmap;

Without Emgu you would need to see how they implement .Bitmap property and just do it by yourself to make your own Bitmap object.


I've used EmguCV and that works well. I've also put my OpenCV C++ code in a managed DLL and that works well too. You can also have the .NET code source and sink to COM interfaces that you can call out on the C++ side.


Although I have decided to port to MC++ for completeness and because I have already done the research there are two other solutions I could have used.

Firstly I could have called the C# component from native C++ by embedding the mono runtime into the project. Looked interesting and I will be researching it for use in other projects.

Or I could have used the CLR Hosting API. Looked more of hack then the first but doable.

Lastly for anyone interested in finding information needed in porting C++ to the its managed form see this link. Looks like native C++ is the only language that can be compiled as managed without modification to the code. Correct me if I am wrong. EDIT: I'm wrong..

0

精彩评论

暂无评论...
验证码 换一张
取 消