当前位置:   article > 正文

VS2015+Qt+OpenCV+open62541开发过程(03_VS2015读取海康威视工业相机-方法1 利用海康威视提供的SDK)_qt框架下接收海康工业相机

qt框架下接收海康工业相机

工业相机: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函数中按顺序程序功能:

  1. // test01.cpp : 定义控制台应用程序的入口点。
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include<windows.h> //使用Sleep(1000)延时函数头文件
  5. #include <stdio.h>
  6. #include <process.h>
  7. #include <conio.h>
  8. #include "string.h"
  9. #include <opencv2/opencv.hpp> //opencv头文件
  10. #include<opencv2/core.hpp>
  11. #include<opencv2/imgproc.hpp>
  12. #include<opencv2/highgui.hpp>
  13. #include<opencv2/videoio.hpp>
  14. #include "MvCameraControl.h"
  15. using namespace std; //使用命名空间
  16. using namespace cv;
  17. /*声明全局变量*/
  18. unsigned int g_nPayloadSize = 0; //海康相机用的变量
  19. // print the discovered devices information to user
  20. bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
  21. {
  22. if (NULL == pstMVDevInfo)
  23. {
  24. printf("The Pointer of pstMVDevInfo is NULL!\n");
  25. return false;
  26. }
  27. if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
  28. {
  29. int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
  30. int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
  31. int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
  32. int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
  33. // print current ip and user defined name
  34. printf("CurrentIp: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4);
  35. printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
  36. }
  37. else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
  38. {
  39. printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
  40. printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
  41. printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
  42. }
  43. else
  44. {
  45. printf("Not support.\n");
  46. }
  47. return true;
  48. }
  49. int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight)
  50. {
  51. if (NULL == pRgbData)
  52. {
  53. return MV_E_PARAMETER;
  54. }
  55. for (unsigned int j = 0; j < nHeight; j++)
  56. {
  57. for (unsigned int i = 0; i < nWidth; i++)
  58. {
  59. unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
  60. pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
  61. pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
  62. }
  63. }
  64. return MV_OK;
  65. }
  66. // convert data stream in Mat format
  67. bool Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char * pData)
  68. {
  69. cv::Mat srcImage;
  70. if (pstImageInfo->enPixelType == PixelType_Gvsp_Mono8)
  71. {
  72. srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC1, pData);
  73. }
  74. else if (pstImageInfo->enPixelType == PixelType_Gvsp_RGB8_Packed)
  75. {
  76. RGB2BGR(pData, pstImageInfo->nWidth, pstImageInfo->nHeight);
  77. srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC3, pData);
  78. }
  79. else
  80. {
  81. printf("unsupported pixel format\n");
  82. return false;
  83. }
  84. if (NULL == srcImage.data)
  85. {
  86. return false;
  87. }
  88. //save converted image in a local file
  89. try {
  90. #if defined (VC9_COMPILE)
  91. cvSaveImage("MatImage.bmp", &(IplImage(srcImage)));
  92. #else
  93. cv::imwrite("MatImage.jpg", srcImage); //存储到工程所在的文件夹内
  94. #endif
  95. }
  96. catch (cv::Exception& ex) {
  97. fprintf(stderr, "Exception saving image to bmp format: %s\n", ex.what());
  98. }
  99. srcImage.release();
  100. return true;
  101. }
  102. /*主函数*/
  103. int main()
  104. {
  105. int nRet = MV_OK;
  106. void* handle = NULL;
  107. do
  108. {
  109. // Enum device
  110. MV_CC_DEVICE_INFO_LIST stDeviceList;
  111. memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
  112. nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
  113. if (MV_OK != nRet)
  114. {
  115. printf("Enum Devices fail! nRet [0x%x]\n", nRet);
  116. break;
  117. }
  118. if (stDeviceList.nDeviceNum > 0)
  119. {
  120. for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
  121. {
  122. printf("[device %d]:\n", i);
  123. MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
  124. if (NULL == pDeviceInfo)
  125. {
  126. break;
  127. }
  128. PrintDeviceInfo(pDeviceInfo);
  129. }
  130. }
  131. else
  132. {
  133. printf("Find No Devices!\n");
  134. break;
  135. }
  136. // input the format to convert
  137. unsigned int nFormat = 0; //[0] OpenCV_Mat\n,[1] OpenCV_IplImage\n,此处等于0,将海康图像转换为OpenCV4.5中的Mat格式,IplImage格式为OpenCV2用的
  138. // select device to connect
  139. unsigned int nIndex = 0; //nIndex=0为枚举到的第一个摄像头,多个摄像头一次加1
  140. if (nIndex >= stDeviceList.nDeviceNum)
  141. {
  142. printf("Input CameraIndex error!\n");
  143. break;
  144. }
  145. // Select device and create handle
  146. nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
  147. if (MV_OK != nRet)
  148. {
  149. printf("Create Handle fail! nRet [0x%x]\n", nRet);
  150. break;
  151. }
  152. // open device
  153. nRet = MV_CC_OpenDevice(handle);
  154. if (MV_OK != nRet)
  155. {
  156. printf("Open Device fail! nRet [0x%x]\n", nRet);
  157. break;
  158. }
  159. // Detection network optimal package size(It only works for the GigE camera)
  160. if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
  161. {
  162. int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
  163. if (nPacketSize > 0)
  164. {
  165. nRet = MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize);
  166. if (nRet != MV_OK)
  167. {
  168. printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
  169. }
  170. }
  171. else
  172. {
  173. printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
  174. }
  175. }
  176. // Set trigger mode as off
  177. nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
  178. if (MV_OK != nRet)
  179. {
  180. printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
  181. break;
  182. }
  183. // Get payload size
  184. MVCC_INTVALUE stParam;
  185. memset(&stParam, 0, sizeof(MVCC_INTVALUE));
  186. nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
  187. if (MV_OK != nRet)
  188. {
  189. printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
  190. break;
  191. }
  192. g_nPayloadSize = stParam.nCurValue;
  193. // Start grab image
  194. nRet = MV_CC_StartGrabbing(handle);
  195. if (MV_OK != nRet)
  196. {
  197. printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
  198. break;
  199. }
  200. MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };
  201. memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
  202. unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * (g_nPayloadSize));
  203. if (pData == NULL)
  204. {
  205. printf("Allocate memory failed.\n");
  206. break;
  207. }
  208. // get one frame from camera with timeout=1000ms
  209. nRet = MV_CC_GetOneFrameTimeout(handle, pData, g_nPayloadSize, &stImageInfo, 1000);
  210. if (nRet == MV_OK)
  211. {
  212. printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
  213. stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
  214. }
  215. else
  216. {
  217. printf("No data[0x%x]\n", nRet);
  218. free(pData);
  219. pData = NULL;
  220. break;
  221. }
  222. // 数据去转换
  223. bool bConvertRet = false;
  224. if (0 == nFormat)
  225. {
  226. bConvertRet = Convert2Mat(&stImageInfo, pData);
  227. }
  228. else
  229. {
  230. //bConvertRet = Convert2Ipl(&stImageInfo, pData);
  231. }
  232. // print result
  233. if (bConvertRet)
  234. {
  235. printf("OpenCV format convert finished.\n");
  236. free(pData);
  237. pData = NULL;
  238. }
  239. else
  240. {
  241. printf("OpenCV format convert failed.\n");
  242. free(pData);
  243. pData = NULL;
  244. break;
  245. }
  246. // Stop grab image
  247. nRet = MV_CC_StopGrabbing(handle);
  248. if (MV_OK != nRet)
  249. {
  250. printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
  251. break;
  252. }
  253. // Close device
  254. nRet = MV_CC_CloseDevice(handle);
  255. if (MV_OK != nRet)
  256. {
  257. printf("ClosDevice fail! nRet [0x%x]\n", nRet);
  258. break;
  259. }
  260. // Destroy handle
  261. nRet = MV_CC_DestroyHandle(handle);
  262. if (MV_OK != nRet)
  263. {
  264. printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
  265. break;
  266. }
  267. cv::Mat img = cv::imread("MatImage.jpg"); //从工程所在的文件夹内读取图片
  268. cv::Mat hsv;
  269. cv::Mat gray;
  270. cv::Mat hsvsplit[3];
  271. cv::namedWindow("test", WINDOW_FREERATIO);
  272. cv::namedWindow("test1", WINDOW_FREERATIO);
  273. cv::namedWindow("test2", WINDOW_FREERATIO);
  274. cv::namedWindow("test3", WINDOW_FREERATIO);
  275. cv::namedWindow("test4", WINDOW_FREERATIO);
  276. cv::imshow("test", img); //在窗口显示一张图片
  277. cv::cvtColor(img, hsv, COLOR_BGR2HSV);
  278. cv::cvtColor(img, gray, COLOR_BGR2GRAY);
  279. cv::split(hsv, hsvsplit);
  280. cv::imshow("test1", hsv);
  281. cv::imshow("test2", hsvsplit[0]);
  282. cv::imshow("test3", hsvsplit[1]);
  283. cv::imshow("test4", hsvsplit[2]);
  284. } while (0);
  285. if (nRet != MV_OK)
  286. {
  287. if (handle != NULL)
  288. {
  289. MV_CC_DestroyHandle(handle);
  290. handle = NULL;
  291. }
  292. }
  293. cv::waitKey(0); //等待键盘按下,再执行后面的程序
  294. destroyAllWindows();
  295. return 0;
  296. }
  1. 枚举(找到)相机,并对相机建立索引号,实验中只有一个相机,索引号为0
  2. 根据索引号选择相机并创建句柄
  3. 打开相机
  4. 获取最佳的网络包大小
  5. 将相机触发模式置OFF
  6. 获取payload大小
  7. 开始抓取图像并抓取一帧
  8. 图像数据转换,将RGB模式转换为BGR模式,并用opencv函数imwrite将图片存储到工程所在的文件夹
  9. 停止抓图
  10. 关闭相机
  11. 用opencv函数将BGR格式转换为HSV格式,并进行通道分离,建立窗口显示图像
  12. 销毁句柄

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/709875
推荐阅读
相关标签
  

闽ICP备14008679号