赞
踩
工业相机:MV-CS050-10GC
安装海康威视工业相机调试软件MVS
安装MVS后,会自动添加环境变量路径,方便使用其提供的SDK二次开发
打开软件,软件左侧会扫描到电脑实际连接的相机,更改IP地址与电脑在同一个网段
电脑网卡开启巨帧,本实验使用海康威视工业相机GigE通讯,带宽要求高,需要网卡开启巨帧。
几个重要的相机参数:
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;
- }

按Ctrl+F5调试,如下图,说明通过相机采图并处理成功。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。