zoukankan      html  css  js  c++  java
  • 海康工业相机 MVS 抓图并转为Mat格式,支持彩色相机

    int CMyCamera::startGrabImage(Mat &image, string &info)
    {

        unsigned int nRecvBufSize = 0;
        MVCC_INTVALUE stParam;
        memset(&stParam, 0, sizeof(MVCC_INTVALUE));
        int nRet = MV_CC_GetIntValue(m_hDevHandle, "PayloadSize", &stParam);
        if (nRet != 0)
        {
            return -1;
        }
        nRecvBufSize = stParam.nCurValue;
        m_pBufForDriver = (unsigned char *)malloc(nRecvBufSize);
        MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };
        nRet = MV_CC_GetOneFrameTimeout(m_hDevHandle, m_pBufForDriver, nRecvBufSize, &stImageInfo, 1000);
        if (nRet != 0)
        {
            return -1;
        }
        m_nBufSizeForSaveImage = stImageInfo.nWidth * stImageInfo.nHeight * 3 + 2048;
        m_pBufForSaveImage = (unsigned char*)malloc(m_nBufSizeForSaveImage);

        bool isMono;//判断是否为黑白图像
        switch (stImageInfo.enPixelType)
        {
        case PixelType_Gvsp_Mono8:
        case PixelType_Gvsp_Mono10:
        case PixelType_Gvsp_Mono10_Packed:
        case PixelType_Gvsp_Mono12:
        case PixelType_Gvsp_Mono12_Packed:
            isMono = true;
            break;
        default:
            isMono = false;
            break;
        }

        if (isMono)
        {
            image = Mat(stImageInfo.nHeight, stImageInfo.nWidth, CV_8UC1, m_pBufForDriver);
        }
        else
        {
            //转换图像格式为BGR8
            MV_CC_PIXEL_CONVERT_PARAM stConvertParam = { 0 };
            memset(&stConvertParam, 0, sizeof(MV_CC_PIXEL_CONVERT_PARAM));
            stConvertParam.nWidth = stImageInfo.nWidth;             
            stConvertParam.nHeight = stImageInfo.nHeight;           
            stConvertParam.pSrcData = m_pBufForDriver;             
            stConvertParam.nSrcDataLen = stImageInfo.nFrameLen;   
            stConvertParam.enSrcPixelType = stImageInfo.enPixelType;  
            stConvertParam.enDstPixelType = PixelType_Gvsp_BGR8_Packed;
            //stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed;
            stConvertParam.pDstBuffer = m_pBufForSaveImage;                
            stConvertParam.nDstBufferSize = m_nBufSizeForSaveImage;        
            MV_CC_ConvertPixelType(m_hDevHandle, &stConvertParam);
            image = Mat(stImageInfo.nHeight, stImageInfo.nWidth, CV_8UC3, m_pBufForSaveImage);
            return MV_OK;
        }
        return MY_FAIL;
    }

    后知后觉、越学越菜
  • 相关阅读:
    一个C#读写Dxf的类库DXFLibrary
    我的敏捷之路
    C#+GDAL读写文件
    IIS并发连接数和数据库连接池
    .net网站iis应用池完美解决方案
    超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
    C#代码连接Oracle数据库一段时间以后[connection lost contact]的问题
    C#程序以管理员权限运行
    C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
    Redis连接的客户端(connected_clients)数过高或者不减的问题解决方案
  • 原文地址:https://www.cnblogs.com/chenhuanting/p/11678685.html
Copyright © 2011-2022 走看看