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 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件