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;
    }
    }

    后知后觉、越学越菜
  • 相关阅读:
    LeetCode 297. 二叉树的序列化与反序列化
    LeetCode 14. 最长公共前缀
    LeetCode 1300. 转变数组后最接近目标值的数组和
    bigo一面凉经
    LeetCode 128.最长连续序列
    LeetCode中二分查找算法的变种
    LeetCode 93. 复原IP地址
    LeetCode 1004. 最大连续1的个数 III
    LeetCode 1282. 用户分组
    多线程理解
  • 原文地址:https://www.cnblogs.com/chenhuanting/p/15188801.html
Copyright © 2011-2022 走看看