opencv调用海康威视摄像头

 这里使用的是海康威视的MV-CA020-20GC摄像头,软件使用的是VS2017与opencv3.1.1 (已经配置完成)。

调取摄像头画面需要先按照海康威视的说明书进行设置,设置完成后,安装海康威视的客户端MVS。

客户端安装完成后需要在VS中进行配置,具体配置方法CSDN里有很多,这里就不详细说明了。

配置完成后运行以下代码就可以调用摄像头的画面:

#include "MvCameraControl.h"
#include 
#include 

using namespace std;
#define MAX_BUF_SIZE    (1920*1200*3)


void main()
{
	int nRet = -1;
	void* m_handle = NULL;

	//枚举子网内指定的传输协议对应的所有设备
	unsigned int nTLayerType = MV_GIGE_DEVICE;
	MV_CC_DEVICE_INFO_LIST m_stDevList = { 0 };
	nRet = MV_CC_EnumDevices(nTLayerType, &m_stDevList);
	if (nRet != 0)
	{
		printf("error: EnumDevices fail [%x]\n", nRet);
	}

	if (m_stDevList.nDeviceNum == 0)
	{
		printf("no camera found!\n");
	}

	//选择查找到的第一台在线设备,创建设备句柄
	int nDeviceIndex = 0;

	MV_CC_DEVICE_INFO m_stDevInfo = { 0 };
	memcpy(&m_stDevInfo, m_stDevList.pDeviceInfo[nDeviceIndex], sizeof(MV_CC_DEVICE_INFO));

	nRet = MV_CC_CreateHandle(&m_handle, &m_stDevInfo);

	if (nRet != 0)
	{
		printf("error: CreateHandle fail [%x]\n", nRet);
	}



	//连接设备
	unsigned int nAccessMode = MV_ACCESS_Exclusive;
	unsigned short nSwitchoverKey = 0;
	nRet = MV_CC_OpenDevice(m_handle, nAccessMode, nSwitchoverKey);
	if (nRet != 0)
	{
		printf("error: OpenDevice fail [%x]\n", nRet);
	}
	//...其他处理 

	//开始采集图像
	nRet = MV_CC_StartGrabbing(m_handle);
	if (nRet != 0)
	{
		printf("error: StartGrabbing fail [%x]\n", nRet);
	}

	//获取一帧数据的大小
	MVCC_INTVALUE stIntvalue = { 0 };
	nRet = MV_CC_GetIntValue(m_handle, "PayloadSize", &stIntvalue);
	if (nRet != 0)
	{
		printf("Get PayloadSize failed! nRet [%x]\n", nRet);
	}
	//int nBufSize = stIntvalue.nCurValue; //一帧数据大小
	int nBufSize = MAX_BUF_SIZE;
	unsigned int    nTestFrameSize = 0;
	unsigned char*  pFrameBuf = NULL;
	pFrameBuf = (unsigned char*)malloc(nBufSize);

	MV_FRAME_OUT_INFO_EX stInfo;
	memset(&stInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));


	//上层应用程序需要根据帧率,控制好调用该接口的频率
	//此次代码仅供参考,实际应用建议另建线程进行图像帧采集和处理
	while (1)
	{
		if (nTestFrameSize > 50)
		{
			break;
		}
		nRet = MV_CC_GetImageForBGR(m_handle, pFrameBuf, nBufSize, &stInfo, 1000);
		if (nRet != 0)
		{
			cout << "error:GetImageForRGB:" << setbase(16) << nRet << endl;
			break;
		}
		else
		{
			//...图像数据处理
			cout << "imagetype:" << setbase(16) << stInfo.enPixelType << endl;

			int width = stInfo.nWidth;
			int height = stInfo.nHeight;

			if (stInfo.enPixelType == PixelType_Gvsp_BGR8_Packed)
			{
				cv::Mat pImg(height, width, CV_8UC3, pFrameBuf);
				cv::imshow("1", pImg);
				cv::waitKey(10);
			}

			



			nTestFrameSize++;
		}
	}

	//...其他处理

	//停止采集图像 
	nRet = MV_CC_StopGrabbing(m_handle);
	if (nRet != 0)
	{
		printf("error: StopGrabbing fail [%x]\n", nRet);
	}

	//关闭设备,释放资源
	nRet = MV_CC_CloseDevice(m_handle);
	if (nRet != 0)
	{
		printf("error: CloseDevice fail [%x]\n", nRet);
	}

	//销毁句柄,释放资源
	nRet = MV_CC_DestroyHandle(m_handle);
	if (nRet != 0)
	{
		printf("error: DestroyHandle fail [%x]\n", nRet);
	}
	system("pause");
}

以上代码,通过海康所提供的damo进行修改得到的,通过while循环调用GetImageForBGR得到每一帧的图像,最后转换成opencv的Mat类,并显示出来。

你可能感兴趣的:(opencv调用海康威视摄像头)