zoukankan      html  css  js  c++  java
  • 海康相机抓图使用OpencvSharp转换成Mat格式

    private int m_nBuffSizeForDriver = 0;
    private int m_nBuffSizeForSaveImage = 0;
    private IntPtr m_pBuffForDriver = IntPtr.Zero;
    private IntPtr m_pBuffForSaveImage = IntPtr.Zero;
    private byte[] buffForDiraver = null;
    private byte[] buffForSaveImage =null;
    public int GrabImage(ref Mat dst)
    {

    dst = new Mat();
    int nRet = CO_FAIL;
    int nPayloadSize = 0;
    MyCamera.MVCC_INTVALUE_EX stIntValue = new MyCamera.MVCC_INTVALUE_EX();
    nRet = m_pCSI.MV_CC_GetIntValueEx_NET("PayloadSize", ref stIntValue);
    if (MyCamera.MV_OK != nRet)
    {
    return CO_FAIL;
    }
    nPayloadSize = (int)stIntValue.nCurValue;
    if (m_pBuffForDriver == IntPtr.Zero)
    {
    m_nBuffSizeForDriver = nPayloadSize;
    buffForDiraver = new byte[m_nBuffSizeForDriver];
    m_pBuffForDriver = Marshal.UnsafeAddrOfPinnedArrayElement(buffForDiraver,0);

    }
    MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
    nRet = m_pCSI.MV_CC_GetOneFrameTimeout_NET(m_pBuffForDriver, (uint)m_nBuffSizeForDriver, ref stFrameInfo, 1000);

    if (MyCamera.MV_OK != nRet)
    {
    return CO_FAIL;
    }

    bool isMono = IsMonoPixelFormat(stFrameInfo.enPixelType);//判断是否为黑白图像

    if (isMono)
    {
    dst = new Mat(stFrameInfo.nHeight, stFrameInfo.nWidth,
    MatType.CV_8UC1, m_pBuffForDriver);
    }
    else
    {
    Console.WriteLine("彩色");
    m_nBuffSizeForSaveImage = stFrameInfo.nWidth * stFrameInfo.nHeight * 3 + 2048;
    if (m_pBuffForSaveImage == IntPtr.Zero)
    {
    buffForSaveImage = new byte[m_nBuffSizeForSaveImage];
    m_pBuffForSaveImage = Marshal.UnsafeAddrOfPinnedArrayElement(buffForSaveImage,0);
    }

    MyCamera.MV_PIXEL_CONVERT_PARAM stConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
    stConvertParam.nWidth = stFrameInfo.nWidth; //ch:图像宽 | en:image width
    stConvertParam.nHeight = stFrameInfo.nHeight; //ch:图像高 | en:image height
    stConvertParam.pSrcData = m_pBuffForDriver; //ch:输入数据缓存 | en:input data buffer
    stConvertParam.nSrcDataLen = stFrameInfo.nFrameLen; //ch:输入数据大小 | en:input data size
    stConvertParam.enSrcPixelType = stFrameInfo.enPixelType; //ch:输入像素格式 | en:input pixel format
    stConvertParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed; //ch:输出像素格式 | en:output pixel format 适用于OPENCV的图像格式
    //stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed; //ch:输出像素格式 | en:output pixel format
    stConvertParam.pDstBuffer = m_pBuffForSaveImage; //ch:输出数据缓存 | en:output data buffer
    stConvertParam.nDstBufferSize = (uint)m_nBuffSizeForSaveImage; //ch:输出缓存大小 | en:output buffer size
    m_pCSI.MV_CC_ConvertPixelType_NET(ref stConvertParam);
    dst = new Mat(stFrameInfo.nHeight, stFrameInfo.nWidth,
    MatType.CV_8UC3, m_pBuffForSaveImage);

    }

    return CO_OK;

    }

    static bool IsMonoPixelFormat(MyCamera.MvGvspPixelType enType)
    {
    switch (enType)
    {
    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:
    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:
    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:
    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:
    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:
    return true;
    default:
    return false;
    }
    }

    后知后觉、越学越菜
  • 相关阅读:
    将word转化为swf 进行如同百度文库的般阅读
    最大子数组问题——编程珠玑第八章
    为什么静态成员必须在类外初始化
    C++初始化列表
    异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接
    [PLL][PM]锁相环模拟相位解调
    insertion sort
    SRM 581 D2 L2:SurveillanceSystem,重叠度
    JQuery(下)
    Ajax
  • 原文地址:https://www.cnblogs.com/chenhuanting/p/15188801.html
Copyright © 2011-2022 走看看