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

    后知后觉、越学越菜
  • 相关阅读:
    ORACLE 查看进程数,已执行任务数, 剩余任务数,删除指定任务
    ORACLE 收集统计整个用户数据
    解决Hystrix dashboard Turbine 一直 Loading…… 及其他坑
    利用 Maven 构造 Spring Cloud 微服务架构 模块使用 spring Boot构建
    AES加解密
    JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案
    JAVA 文件的上传下载
    shell启停服务脚本模板
    JAVA 设计模式之 原型模式详解
    JAVA 设计模式之 工厂模式详解
  • 原文地址:https://www.cnblogs.com/chenhuanting/p/15188801.html
Copyright © 2011-2022 走看看