我是靠谱客的博主 俏皮心锁,这篇文章主要介绍linux下使用opencv读取大恒相机,现在分享给大家,希望可以做个参考。

前言

大恒网口相机真的有点坑,api很难用,还好技术支持比较有耐心,一点一点的帮着解决了问题。

驱动安装

首先去大恒官网去下载linux下面的驱动,除了C++版本,还有python版本。一定要注意:linux下面安装驱动时,要看自己的内核版本支不支持相机的驱动,这一点很坑,要问清楚,到后面,也是费了很大劲才更换的内核。
在这里插入图片描述
在这里插入图片描述这是我这个相机驱动支持的linux内核,然后,安装驱动即可。
在这里插入图片描述
安装完驱动后,在tools文件夹中,有一个可执行文件,可以用来监测网口,运行即可。除此之外,还有一个可视化界面GalaxyView。
在这里插入图片描述

搭建工程

编译完成的文件夹有一些例子,拿下图第一个例子为例,在QT中建立工程。
在这里插入图片描述在QT中配置好路径,包括头文件和库文件。
在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
INCLUDEPATH += /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/include LIBS += /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libdximageproc.so.1.0.1602.8161 /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libgxgvtl.so.1.0.1802.8051 /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libgxiapi.so.1.0.1810.8121 /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libgxu3vtl.so.1.0.1802.8051 INCLUDEPATH += /usr/local/include /usr/local/include/opencv /usr/local/include/opencv2 LIBS += /usr/local/lib/libopencv_*.so

对源文件进行修改,修改的内容主要在void ProcGetImage(void pParam)函数中,还有定义char* m_rgb_image=NULL,下面有标注。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//------------------------------------------------------------- /** file GxAcquireContinuous.cpp brief sample to show how to acquire image continuously. version 1.0.1605.9041 date 2016-05-04 */ //------------------------------------------------------------- #include "GxIAPI.h" #include"DxImageProc.h" #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <iostream> #include<sstream> #include<fstream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; using namespace cv::dnn; #define MEMORY_ALLOT_ERROR -1 char* m_rgb_image=NULL; //增加的内容 GX_DEV_HANDLE g_device = NULL; ///< 设备句柄 GX_FRAME_DATA g_frame_data = { 0 }; ///< 采集图像参数 pthread_t g_acquire_thread = 0; ///< 采集线程ID bool g_get_image = false; ///< 采集线程是否结束的标志:true 运行;false 退出 //获取图像大小并申请图像数据空间 int PreForImage(); //释放资源 int UnPreForImage(); //采集线程函数 void *ProcGetImage(void* param); //获取错误信息描述 void GetErrorString(GX_STATUS error_status); int main() { uid_t user = 0; user = geteuid(); if(user != 0) { printf("n"); printf("Please run this application with 'sudo -E ./GxAcquireContinuous' or" " Start with root !n"); printf("n"); return 0; } printf("n"); printf("-------------------------------------------------------------n"); printf("sample to show how to acquire image continuously.n"); #ifdef __x86_64__ printf("version: 1.0.1605.8041n"); #elif __i386__ printf("version: 1.0.1605.9041n"); #endif printf("-------------------------------------------------------------n"); printf("n"); printf("Press [x] or [X] and then press [Enter] to Exit the Programn"); printf("Initializing......"); printf("nn"); usleep(2000000); //API接口函数返回值 GX_STATUS status = GX_STATUS_SUCCESS; uint32_t device_num = 0; uint32_t ret = 0; GX_OPEN_PARAM open_param; //初始化设备打开参数,默认打开序号为1的设备 open_param.accessMode = GX_ACCESS_EXCLUSIVE; open_param.openMode = GX_OPEN_INDEX; open_param.pszContent = "1"; //初始化库 status = GXInitLib(); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); return 0; } //获取枚举设备个数 status = GXUpdateDeviceList(&device_num, 1000); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); status = GXCloseLib(); return 0; } if(device_num <= 0) { printf("<No device>n"); status = GXCloseLib(); return 0; } else { //默认打开第1个设备 status = GXOpenDevice(&open_param, &g_device); if(status == GX_STATUS_SUCCESS) { printf("<Open device success>n"); } else { printf("<Open device fail>n"); status = GXCloseLib(); return 0; } } //设置采集模式为连续采集 status = GXSetEnum(g_device, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); status = GXCloseDevice(g_device); if(g_device != NULL) { g_device = NULL; } status = GXCloseLib(); return 0; } //设置触发开关为OFF status = GXSetEnum(g_device, GX_ENUM_TRIGGER_MODE, GX_TRIGGER_MODE_OFF); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); status = GXCloseDevice(g_device); if(g_device != NULL) { g_device = NULL; } status = GXCloseLib(); return 0; } //为采集做准备 ret = PreForImage(); if(ret != 0) { printf("<Failed to prepare for acquire image>n"); status = GXCloseDevice(g_device); if(g_device != NULL) { g_device = NULL; } status = GXCloseLib(); return 0; } //启动接收线程 ret = pthread_create(&g_acquire_thread, 0, ProcGetImage, 0); if(ret != 0) { printf("<Failed to create the collection thread>n"); status = GXCloseDevice(g_device); if(g_device != NULL) { g_device = NULL; } status = GXCloseLib(); return 0; } bool run = true; while(run == true) { int c = getchar(); switch(c) { //退出程序 case 'X': case 'x': run = false; break; default: break; } } //为停止采集做准备 ret = UnPreForImage(); if(ret != 0) { status = GXCloseDevice(g_device); if(g_device != NULL) { g_device = NULL; } status = GXCloseLib(); return 0; } //关闭设备 status = GXCloseDevice(g_device); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); if(g_device != NULL) { g_device = NULL; } status = GXCloseLib(); return 0; } //释放库 status = GXCloseLib(); return 0; } //------------------------------------------------- /** brief 获取图像大小并申请图像数据空间 return void */ //------------------------------------------------- int PreForImage() { GX_STATUS status = GX_STATUS_SUCCESS; int64_t payload_size = 0; status = GXGetInt(g_device, GX_INT_PAYLOAD_SIZE, &payload_size); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); return status; } g_frame_data.pImgBuf = malloc(payload_size); if(g_frame_data.pImgBuf == NULL) { printf("<Failed to allot memory>n"); return MEMORY_ALLOT_ERROR; } return 0; } //------------------------------------------------- /** brief 释放资源 return void */ //------------------------------------------------- int UnPreForImage() { GX_STATUS status = GX_STATUS_SUCCESS; uint32_t ret = 0; //发送停采命令 status = GXSendCommand(g_device, GX_COMMAND_ACQUISITION_STOP); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); return status; } g_get_image = false; ret = pthread_join(g_acquire_thread,NULL); if(ret != 0) { printf("<Failed to release resources>n"); return ret; } //释放buffer if(g_frame_data.pImgBuf != NULL) { free(g_frame_data.pImgBuf); g_frame_data.pImgBuf = NULL; } return 0; } //------------------------------------------------- /** brief 采集线程函数 param pParam 线程传入参数 return void* */ //------------------------------------------------- void *ProcGetImage(void* pParam) { GX_STATUS status = GX_STATUS_SUCCESS; //接收线程启动标志 g_get_image = true; //发送开采命令 status = GXSendCommand(g_device, GX_COMMAND_ACQUISITION_START); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); } while(g_get_image) { if(g_frame_data.pImgBuf == NULL) { continue; } status = GXGetImage(g_device, &g_frame_data, 100); if(status == GX_STATUS_SUCCESS) { if(g_frame_data.nStatus == 0) { //增加的内容 Mat src; m_rgb_image=new char[g_frame_data.nWidth*g_frame_data.nHeight*3]; src.create(g_frame_data.nHeight,g_frame_data.nWidth,CV_8UC3); memcpy(src.data,g_frame_data.pImgBuf,g_frame_data.nWidth*g_frame_data.nHeight); DxRaw8toRGB24(g_frame_data.pImgBuf,m_rgb_image,g_frame_data.nWidth, g_frame_data.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERBG),false); memcpy(src.data,m_rgb_image,g_frame_data.nWidth*g_frame_data.nHeight*3); namedWindow("test",0); imshow("test",src); waitKey(10); //到这里增加结束 printf("<Successful acquisition : Width: %d Height: %d >n", g_frame_data.nWidth, g_frame_data.nHeight); } } } } //---------------------------------------------------------------------------------- /** brief 获取错误信息描述 param emErrorStatus 错误码 return void */ //---------------------------------------------------------------------------------- void GetErrorString(GX_STATUS error_status) { char *error_info = NULL; size_t size = 0; GX_STATUS status = GX_STATUS_SUCCESS; // 获取错误描述信息长度 status = GXGetLastError(&error_status, NULL, &size); if(status != GX_STATUS_SUCCESS) { GetErrorString(status); return; } error_info = new char[size]; if (error_info == NULL) { printf("<Failed to allocate memory>n"); return ; } // 获取错误信息描述 status = GXGetLastError(&error_status, error_info, &size); if (status != GX_STATUS_SUCCESS) { printf("<GXGetLastError call fail>n"); } else { printf("%sn", (char*)error_info); } // 释放资源 if (error_info != NULL) { delete []error_info; error_info = NULL; } }

显示结果

在这里插入图片描述在这里插入图片描述

最后

以上就是俏皮心锁最近收集整理的关于linux下使用opencv读取大恒相机的全部内容,更多相关linux下使用opencv读取大恒相机内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部