opencv+海康采集卡的使用

最近项目中需要用到opencv处理海康视频采集卡的单张图片数据,在这里记录一下使用过程。

在这里使用的是opencv 3.2 的库和海康DS-4300HW-E。板卡sdk版本6.52。
1.初始化板卡

unsigned int m_iDefVideoStandard = 1;
unsigned int m_iHDDefVideoStandard = 6;
unsigned int m_nTotalEncChannels = 0;
unsigned int mOpenChannelIndex = 0;
unsigned int mImageQuality = 50;
HANDLE mChannelHandle;
const unsigned long mImageSize = 1920 * 1080 * 2;
unsigned char *pImageBuf = NULL;

bool initBoardSystem()
{
    VideoStandard_t SDvs;
    switch (m_iDefVideoStandard)
    {
    case 0:
        SDvs = StandardNTSC;
        break;
    case 1:
        SDvs = StandardPAL;
        break;
    default:
        SDvs = StandardPAL;
        break;
    }
    //设置默认的视频制式
    SetDefaultVideoStandard(SDvs);

    VideoStandard_t HDvs;
    switch (m_iHDDefVideoStandard)
    {
    case 0:
        HDvs = Standard720P_25HZ;
        break;
    case 1:
        HDvs = Standard720P_30HZ;
        break;
    case 2:
        HDvs = Standard720P_50HZ;
        break;
    case 3:
        HDvs = Standard720P_60HZ;
        break;
    case 4:
        HDvs = Standard1080I_50HZ;
        break;
    case 5:
        HDvs = Standard1080I_60HZ;
        break;
    case 6:
        HDvs = Standard1080P_25HZ;
        break;
    case 7:
        HDvs = Standard1080P_30HZ;
        break;
    default:
        HDvs = Standard1080P_25HZ;
        break;
    }
    //设置默认的高清视频制式
    SetDefaultHDVideoStandard(HDvs);

    int encRet = 0;
    if ((encRet = InitDSPs()) <= 0){
        return false;
    }

    m_nTotalEncChannels = GetEncodeChannelCount();
    if(m_nTotalEncChannels <= 0){
        return false;
    }
    //打开板卡上的第几个通道
    mChannelHandle = ChannelOpen(mOpenChannelIndex);
    if((unsigned int)mChannelHandle == 0xffffffff){
        return false;
    }

    pImageBuf = new unsigned char[mImageSize];

    return true;
}

2.从通道获取单张图片并转换成Mat

    int ret;
    unsigned length = 0;
    // 捕获图片到缓存中
    ret= GetJpegImage(mChannelHandle,pImageBuf,&length, mImageQuality);
    if (ret == 0) {
        Mat rawData = Mat(1,length, CV_8UC1, pImageBuf);
        Mat image = imdecode(rawData,1);
    } 

3.释放板卡

    StopVideoCapture(mChannelHandle);
    StopSubVideoCapture(mChannelHandle);
    ChannelClose(mChannelHandle);
    delete [] pImageBuf;
    DeInitDSPs();

你可能感兴趣的:(opencv+海康采集卡的使用)