如何在 C++ 中使用 C# 库带有 VB.net GUI 的 OpenCV 项目?

发布于 2024-10-29 05:07:57 字数 408 浏览 7 评论 0原文

我开发了一个用非托管 C++ 编写的应用程序,它使用 OpenCV,到目前为止一直在使用内置的 highgui 库来显示结果。

我现在想与 VB.net 开发人员一起将其打包成适合销售的产品。

我决定进行的两项增强功能是添加 GUI,其次通过将其替换为 3rdParty 解决方案来改进人脸识别模块。

问题是 3rdParty SDK 只有 C# API,显然其他程序员想用 VB 实现该接口。

我可以将我的代码移植到 C# 并使用 EmguCV 一石二鸟,但是代码库相当大,我认为我没有时间,但这是一个选择。

我想知道的是从这里去哪里。我是否要移植到 MC++、使用混合的本机和托管程序集、自己在 Qt 中编写 GUI 等等

另外,我将如何在 .NET WinForm 控件中显示 Iplimage 流?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风尘浪孓 2024-11-05 05:07:57

在您的情况下,是否可以实现 OpenCV 本机代码的一些简单、高级 API,以便使用 PInvoke 轻松与 C# 进行互操作?我过去也遇到过类似的问题,我所做的是将我的本机代码打包为大约 15 个可以从 C# 轻松使用的函数(使用 PInvoke)。我不知道在你的情况下是否可以轻松做到这一点,如果没有,你可能会尝试在 MC++ 中编写一些类,并尝试使用 MC++ 编译你的 OpenCV 代码,毕竟,C++/CLI 的主要用途是创建一个本机 C++ 和托管语言之间的桥梁(尽管如此,我个人仍然更喜欢制作漂亮的本机 API 并从 C# 中 PInvoke 它。

至于 IplImage,如果您使用 EmguCV,那么有一个.Bitmap 属性为您提供可以使用的托管 System.Drawing.Bitmap 类,无需复制底层缓冲区(我在应用程序中渲染来自摄像机的 5 个实时流,并且它另一方面,如果您想手动完成此操作,而不使用 EmguCV,我鼓励您检查他们的源代码,看看他们如何实现此功能并自己完成(基本上,您需要获取指向的指针)。内存并获得一些属性,例如图像步长、宽度、高度并使用这些自行创建 Bitmap 类

编辑:我找到了一些用于渲染在 OpenCV 中处理的图像的代码:

C++ 代码:

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;
}

此代码是。基本上填充 dataPtr 和 stride(假设宽度和高度已知)

C# PInvoke:

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

C# 用法(使用 EmguCV):

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

如果没有 Emgu,您需要了解它们如何实现 .Bitmap 属性,然后自己完成制作你自己的位图对象。

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.

他不在意 2024-11-05 05:07:57

我用过 EmguCV,效果很好。我还将 OpenCV C++ 代码放入托管 DLL 中,效果也很好。您还可以拥有可在 C++ 端调用的 COM 接口的 .NET 代码源和接收器。

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.

时光暖心i 2024-11-05 05:07:57

尽管为了完整性我已经决定移植到 MC++,并且因为我已经完成了研究,但我还可以使用其他两种解决方案。

首先,我可以通过将 mono 运行时嵌入到项目中来从本机 C++ 调用 C# 组件。看起来很有趣,我将研究它以用于其他项目。

或者我可以使用 CLR Hosting API。看起来比第一个更像黑客但可行。

最后,对于任何有兴趣查找将 C++ 移植到其托管形式所需信息的人,请参阅此< /a> 链接。看起来原生 C++ 是唯一可以在不修改代码的情况下进行托管编译的语言。如果我错了请纠正我。编辑:我错了..

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文