C++ 做了视频识别的DLL动态库,导出函数
extern "C" __declspec(dllexport)float Recognition(Mat frame,int type);
C#主程序调用C++的视频识别动态库
[DllImport(@"Recognition.dll", EntryPoint = "Recognition", CallingConvention = CallingConvention.Cdecl)]
public extern static float Recognition(Mat frame, int iType);
C#调用内容如下:
Bitmap bmp = cameraControl1.TakeSnapshot();
Image<Bgr, byte> image = new Image<Bgr, byte>(bmp);
//Image<Bgr, byte>转Mat
Mat mat = image.Mat;
Mat Graymat = mat.Clone();
float Value = Recognition(Graymat, 1);
运行到float Value = Recognition( Graymat, 1);行,查看 Graymat 变量内容正常;
按F11 调试到C++工程;查看传入的Mat 变量 frame 宽度、高度、数据均不正常;不知道原因,改成参数引用传递也不行。调试信息如图
哪位大神急救下!

C# 传递图像数组
[DllImport(@"Recognition.dll", EntryPoint = "Recognition", CallingConvention = CallingConvention.Cdecl)]
public extern static float Recognition(byte[] ImageBuffer, int imageWedth, int imageHeight, int imageStride, int iType);
图片转换成byte数组:
Bitmap bmp = cameraControl1.TakeSnapshot();
System.Drawing.Imaging.PixelFormat rgb = bmp.PixelFormat;
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
int ImageWedth = bmp.Width;
int ImageHeight = bmp.Height;
int ImageStride = bmpData.Stride;
Recognition(rgbValues, ImageWedth, ImageHeight, ImageStride,1);
C++ 动态库修改:
extern "C" __declspec(dllexport)float Recognition(byte *ImageBuffer, int imageWedth, int imageHeight, int imageStride,int type)
{
Mat frame = cv::Mat(imageHeight, imageWedth, CV_8UC4, ImageBuffer, imageStride);
...
}