piaoxurenjian 2019-08-20 11:34 采纳率: 0%
浏览 836

C# 调用C++ DLL 参数Emgu.cv.Mat如何传递

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 宽度、高度、数据均不正常;不知道原因,改成参数引用传递也不行。调试信息如图
        哪位大神急救下!
        ![图片说明](https://img-ask.csdn.net/upload/201908/20/1566271847_310078.png)![图片说明](https://img-ask.csdn.net/upload/201908/20/1566271855_169575.png)
  • 写回答

1条回答 默认 最新

  • piaoxurenjian 2019-08-20 15:01
    关注

    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);
    ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配