[英]Returning a Mat from C++ dll to VB.Net
我已经用C ++编写了一个函数,该函数接受Mat类对象,并在处理后返回Mat。 我想将其集成到用VB.NET编写的UI中
我用C ++编写了这段代码
extern "C" _declspec(dllexport) Mat cropImage(Mat matA){
Mat matB = processingObject.doSomething(matA);
return matB;
}
processingObject.doSomething(matA)正常工作。
如何在VB中使用此dll? 我不介意更改C ++代码以使其工作。
谢谢
更新 >>>>
找到了解决该问题的方法。 虽然不是我要找的100%
C ++
extern "C" __declspec(dllexport) int* cropImage(char* path, int& size)
{
Mat matA = pavObj.cropped_liquid_region(path);
int rows = matA.rows;
int cols = matA.cols;
int channels = matA.channels();
int length = rows*cols*channels + 3;
size = length;
int* arr = new int[length];
arr[0] = rows;
arr[1] = cols;
arr[2] = channels;
Mat layer[3];
split(matA, layer); // split into color layers BGR
int count = 3;
for(int i=0;i<3;i++){
for(int j=0;j<layer[i].rows;j++){
for(int k=0;k<layer[i].cols;k++){
arr[count] = layer[i].at<uchar>(j, k);
count++;
}
}
}
return arr;
}
extern "C" __declspec(dllexport) int ReleaseMemory(int* pArray)
{
delete[] pArray;
return 0;
}
VB签名
<DllImport("mat.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function cropImage(ByVal path As String, ByRef sz As Integer) As IntPtr
End Function
<DllImport("mat.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function ReleaseMemory(ByVal ptr As IntPtr) As Integer
End Function
VB调用dll
Dim size As Integer
Dim ptr As IntPtr = cropImage(path, size)
Dim result(size) As Integer
Marshal.Copy(ptr, result, 0, size)
ReleaseMemory(ptr)
因此,基本上发生的是将图像的内容带入一维数组,然后传递到被管理端,在此再次重构图像。
您需要在C#端使用DllImport调用来调用您的
http://msdn.microsoft.com/en-us/library/aa984739(v=vs.71).aspx
如何在VB.NET中使用<DllImport>?
http://www.codeproject.com/Articles/552348/C-23-2fCplusinteroppluswithplusDllImport
要点是,您需要像这样导入dll:
<System.Runtime.InteropService.DllImport("mat.dll", _
SetLastError:=True, CharSet:=CharSet.Auto)> _
<< VB Signature of your cropImage function >>
您可能还需要做一些参数编组,以在VB和C ++之间“翻译”您的Mat类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.