工业相机:MV-CS050-10GC

安装海康威视工业相机调试软件MVS

安装MVS后,会自动添加环境变量路径,方便使用其提供的SDK二次开发

打开软件,软件左侧会扫描到电脑实际连接的相机,更改IP地址与电脑在同一个网段

电脑网卡开启巨帧,本实验使用海康威视工业相机GigE通讯,带宽要求高,需要网卡开启巨帧。

几个重要的相机参数:

  1. 图像格式,默认BGR8,如果用opencv处理需要格式转化
  2. GEV Heartbeat Time,如果程序调用相机连接中断,设定时间后相机自动释放连接
  3. 曝光时间、自动校正等参数根据现场实际调整
  4. 工具中带宽管理,多个相机同时使用时需要对每个相机进行带宽分配,否则会网络堵塞
  5. 改完参数要保存到用户参数集,否则断电上电会丢失参数,上电加载哪个参数集可以设置

MVS软件提供了二次开发相关文档、示例代码,点帮助-Development打开文件夹

提供了多种编程语言和编程环境的Sample

参考Sample_VS2015编写一个采集海康威视摄像头并用opencv显示的程序:

解决方案管理器中,右键-配置工程属性C/C++ 常规-附加包含目录

解决方案管理器中,右键-配置工程属性链接器 常规-附加库目录

解决方案管理器中,右键-配置工程属性链接器 输入-附加依赖项

在main.cpp中输入附件中代码,main函数中按顺序程序功能:

// test01.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include<windows.h>   //使用Sleep(1000)延时函数头文件
#include <stdio.h>
#include <process.h>
#include <conio.h>
#include "string.h"

#include <opencv2/opencv.hpp>   //opencv头文件
#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/videoio.hpp>

#include "MvCameraControl.h"

using namespace std;  //使用命名空间
using namespace cv;

/*声明全局变量*/
unsigned int g_nPayloadSize = 0; //海康相机用的变量

// print the discovered devices information to user
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
	if (NULL == pstMVDevInfo)
	{
		printf("The Pointer of pstMVDevInfo is NULL!\n");
		return false;
	}
	if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
	{
		int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
		int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
		int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
		int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);

		// print current ip and user defined name
		printf("CurrentIp: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4);
		printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
	}
	else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
	{
		printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
		printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
		printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
	}
	else
	{
		printf("Not support.\n");
	}

	return true;
}

int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight)
{
	if (NULL == pRgbData)
	{
		return MV_E_PARAMETER;
	}

	for (unsigned int j = 0; j < nHeight; j++)
	{
		for (unsigned int i = 0; i < nWidth; i++)
		{
			unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
			pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
			pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
		}
	}

	return MV_OK;
}

// convert data stream in Mat format
bool Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char * pData)
{
	cv::Mat srcImage;
	if (pstImageInfo->enPixelType == PixelType_Gvsp_Mono8)
	{
		srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC1, pData);
	}
	else if (pstImageInfo->enPixelType == PixelType_Gvsp_RGB8_Packed)
	{
		RGB2BGR(pData, pstImageInfo->nWidth, pstImageInfo->nHeight);
		srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC3, pData);
	}
	else
	{
		printf("unsupported pixel format\n");
		return false;
	}

	if (NULL == srcImage.data)
	{
		return false;
	}

	//save converted image in a local file
	try {
#if defined (VC9_COMPILE)
		cvSaveImage("MatImage.bmp", &(IplImage(srcImage)));
#else
		cv::imwrite("MatImage.jpg", srcImage);  //存储到工程所在的文件夹内
#endif
	}
	catch (cv::Exception& ex) {
		fprintf(stderr, "Exception saving image to bmp format: %s\n", ex.what());
	}

	srcImage.release();

	return true;
}


/*主函数*/
int main()
{

	int nRet = MV_OK;
	void* handle = NULL;

	do
	{
		// Enum device
		MV_CC_DEVICE_INFO_LIST stDeviceList;
		memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
		nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
		if (MV_OK != nRet)
		{
			printf("Enum Devices fail! nRet [0x%x]\n", nRet);
			break;
		}

		if (stDeviceList.nDeviceNum > 0)
		{
			for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
			{
				printf("[device %d]:\n", i);
				MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
				if (NULL == pDeviceInfo)
				{
					break;
				}
				PrintDeviceInfo(pDeviceInfo);
			}
		}
		else
		{
			printf("Find No Devices!\n");
			break;
		}

		// input the format to convert

		unsigned int nFormat = 0; //[0] OpenCV_Mat\n,[1] OpenCV_IplImage\n,此处等于0,将海康图像转换为OpenCV4.5中的Mat格式,IplImage格式为OpenCV2用的
		
		// select device to connect

		unsigned int nIndex = 0;  //nIndex=0为枚举到的第一个摄像头,多个摄像头一次加1

		if (nIndex >= stDeviceList.nDeviceNum)
		{
			printf("Input CameraIndex error!\n");
			break;
		}

		// Select device and create handle
		nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
		if (MV_OK != nRet)
		{
			printf("Create Handle fail! nRet [0x%x]\n", nRet);
			break;
		}

		// open device
		nRet = MV_CC_OpenDevice(handle);
		if (MV_OK != nRet)
		{
			printf("Open Device fail! nRet [0x%x]\n", nRet);
			break;
		}

		// Detection network optimal package size(It only works for the GigE camera)
		if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
		{
			int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
			if (nPacketSize > 0)
			{
				nRet = MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize);
				if (nRet != MV_OK)
				{
					printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
				}
			}
			else
			{
				printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
			}
		}

		// Set trigger mode as off
		nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
		if (MV_OK != nRet)
		{
			printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
			break;
		}

		// Get payload size
		MVCC_INTVALUE stParam;
		memset(&stParam, 0, sizeof(MVCC_INTVALUE));
		nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
		if (MV_OK != nRet)
		{
			printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
			break;
		}
		g_nPayloadSize = stParam.nCurValue;

		// Start grab image
		nRet = MV_CC_StartGrabbing(handle);
		if (MV_OK != nRet)
		{
			printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
			break;
		}

		MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };
		memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
		unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * (g_nPayloadSize));
		if (pData == NULL)
		{
			printf("Allocate memory failed.\n");
			break;
		}

		// get one frame from camera with timeout=1000ms
		nRet = MV_CC_GetOneFrameTimeout(handle, pData, g_nPayloadSize, &stImageInfo, 1000);
		if (nRet == MV_OK)
		{
			printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
				stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
		}
		else
		{
			printf("No data[0x%x]\n", nRet);
			free(pData);
			pData = NULL;
			break;
		}

		// 数据去转换
		bool bConvertRet = false;
		if (0 == nFormat)
		{
			bConvertRet = Convert2Mat(&stImageInfo, pData);
		}
		else
		{
			//bConvertRet = Convert2Ipl(&stImageInfo, pData);
		}
		// print result
		if (bConvertRet)
		{
			printf("OpenCV format convert finished.\n");
			free(pData);
			pData = NULL;
		}
		else
		{
			printf("OpenCV format convert failed.\n");
			free(pData);
			pData = NULL;
			break;
		}

		// Stop grab image
		nRet = MV_CC_StopGrabbing(handle);
		if (MV_OK != nRet)
		{
			printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
			break;
		}

		// Close device
		nRet = MV_CC_CloseDevice(handle);
		if (MV_OK != nRet)
		{
			printf("ClosDevice fail! nRet [0x%x]\n", nRet);
			break;
		}

		// Destroy handle
		nRet = MV_CC_DestroyHandle(handle);
		if (MV_OK != nRet)
		{
			printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
			break;
		}

		cv::Mat img = cv::imread("MatImage.jpg");   //从工程所在的文件夹内读取图片
		cv::Mat hsv;
		cv::Mat gray;
		cv::Mat hsvsplit[3];
		cv::namedWindow("test", WINDOW_FREERATIO);
		cv::namedWindow("test1", WINDOW_FREERATIO);
		cv::namedWindow("test2", WINDOW_FREERATIO);
		cv::namedWindow("test3", WINDOW_FREERATIO);
		cv::namedWindow("test4", WINDOW_FREERATIO);

		cv::imshow("test", img);                       //在窗口显示一张图片

		cv::cvtColor(img, hsv, COLOR_BGR2HSV);
		cv::cvtColor(img, gray, COLOR_BGR2GRAY);
		cv::split(hsv, hsvsplit);

		cv::imshow("test1", hsv);
		cv::imshow("test2", hsvsplit[0]);
		cv::imshow("test3", hsvsplit[1]);
		cv::imshow("test4", hsvsplit[2]);

	} while (0);

	if (nRet != MV_OK)
	{
		if (handle != NULL)
		{
			MV_CC_DestroyHandle(handle);
			handle = NULL;
		}
	}
	cv::waitKey(0);          //等待键盘按下,再执行后面的程序
	destroyAllWindows();
	return 0;
}
  1. 枚举(找到)相机,并对相机建立索引号,实验中只有一个相机,索引号为0
  2. 根据索引号选择相机并创建句柄
  3. 打开相机
  4. 获取最佳的网络包大小
  5. 将相机触发模式置OFF
  6. 获取payload大小
  7. 开始抓取图像并抓取一帧
  8. 图像数据转换,将RGB模式转换为BGR模式,并用opencv函数imwrite将图片存储到工程所在的文件夹
  9. 停止抓图
  10. 关闭相机
  11. 用opencv函数将BGR格式转换为HSV格式,并进行通道分离,建立窗口显示图像
  12. 销毁句柄

按Ctrl+F5调试,如下图,说明通过相机采图并处理成功。

GitHub 加速计划 / opencv31 / opencv
77.38 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:2 个月前 )
48668119 dnn: use dispatching for Winograd optimizations 6 天前
3dace76c flann: remove unused hdf5 header 6 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐