/
camerapointgrey.cpp
360 lines (309 loc) · 10.4 KB
/
camerapointgrey.cpp
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
#ifdef HAS_POINT_GREY
#include "camerapointgrey.h"
#include <stdio.h>
#include <assert.h>
CameraPointGrey::CameraPointGrey(int width, int height):
::Camera(width, height)
{
/*if (this->init() != 0) {
printf("Camera Point Grey initialization failed.\n");
}*/
}
CameraPointGrey::~CameraPointGrey()
{
for (unsigned int i = 0; i < numCameras; i++ ) {
error = ppCameras[i]->StopCapture(); // Stop capturing images
if (error != PGRERROR_OK)
PrintError( error );
error = ppCameras[i]->Disconnect(); // Disconnect the camera
if (error != PGRERROR_OK)
PrintError( error );
delete ppCameras[i];
}
delete [] ppCameras;
}
int CameraPointGrey::init(bool isTrigger)
{
PrintBuildInfo();
error = busMgr.GetNumOfCameras(&numCameras);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
printf("Number of cameras detected: %u\n", numCameras);
if (numCameras /*< 1*/ != 2)
{
printf( "Insufficient number of cameras! Please check.\n" );
return -1;
}
ppCameras = new FlyCapture2::Camera*[numCameras];
// Connect to all detected cameras and attempt to set them to
// a common video mode and frame rate
for (unsigned int i = 0; i < numCameras; i++) {
ppCameras[i] = new FlyCapture2::Camera();
// PGRGuid guid
PGRGuid guid;
error = busMgr.GetCameraFromIndex(i, &guid);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
//停止捕获和连接,以防之前的错误
ppCameras[i]->StopCapture();
ppCameras[i]->Disconnect();
// Connect to a camera
error = ppCameras[i]->Connect(&guid);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
// Get the camera information
CameraInfo camInfo;
error = ppCameras[i]->GetCameraInfo(&camInfo);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
PrintCameraInfo(&camInfo);
{
// Check for external trigger support
TriggerModeInfo triggerModeInfo;
error = ppCameras[i]->GetTriggerModeInfo(&triggerModeInfo);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
if (triggerModeInfo.present != true) {
printf( "Camera does not support external trigger!\n" );
return -1;
}
// Get current trigger settings
TriggerMode triggerMode;
error = ppCameras[i]->GetTriggerMode(&triggerMode);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
// Set camera to trigger mode 0
triggerMode.onOff = isTrigger; // on or off
triggerMode.mode = 0;
triggerMode.parameter = 0;
triggerMode.source = 0; // Triggering the camera externally using source 0, i.e., GPIO0.
error = ppCameras[i]->SetTriggerMode(&triggerMode);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
if (isTrigger) {
// Poll to ensure camera is ready
bool retVal = PollForTriggerReady(ppCameras[i]);
if (!retVal) {
printf("\nError polling for trigger ready!\n");
return -1;
}
}
}
{
Mode k_fmt7Mode = MODE_0;
PixelFormat k_fmt7PixFmt = PIXEL_FORMAT_MONO8;
// Query for available Format 7 modes
Format7Info fmt7Info;
bool supported;
fmt7Info.mode = k_fmt7Mode;
error = ppCameras[i]->GetFormat7Info(&fmt7Info, &supported);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
PrintFormat7Capabilities( fmt7Info );
if ((k_fmt7PixFmt & fmt7Info.pixelFormatBitField) == 0) {
printf("Pixel format is not supported\n"); // Pixel format not supported!
return -1;
}
Format7ImageSettings fmt7ImageSettings;
fmt7ImageSettings.mode = k_fmt7Mode;
fmt7ImageSettings.offsetX = 0;
fmt7ImageSettings.offsetY = 0;
fmt7ImageSettings.width = fmt7Info.maxWidth;
fmt7ImageSettings.height = fmt7Info.maxHeight;
fmt7ImageSettings.pixelFormat = k_fmt7PixFmt;
bool valid;
Format7PacketInfo fmt7PacketInfo;
// Validate the settings to make sure that they are valid
error = ppCameras[i]->ValidateFormat7Settings(
&fmt7ImageSettings,
&valid,
&fmt7PacketInfo);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
if (!valid ) {
printf("Format7 settings are not valid\n"); // Settings are not valid
return -1;
}
// Set the settings to the camera
error = ppCameras[i]->SetFormat7Configuration(
&fmt7ImageSettings,
// fmt7PacketInfo.recommendedBytesPerPacket );
(unsigned int)516);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
}
VideoMode videoMode = VIDEOMODE_1024x768Y8;
FrameRate frameRate = FRAMERATE_3_75;
bool isSupported;
ppCameras[i]->GetVideoModeAndFrameRateInfo(videoMode, frameRate, &isSupported);
if (!isSupported) {
printf("Video mode 1024x768 8bit, Frame rate 3.75, are not supported!\n");
return -1;
}
ppCameras[i]->SetVideoModeAndFrameRate(videoMode, frameRate);
// Camera is ready, start capturing images
error = ppCameras[i]->StartCapture();
if (error != PGRERROR_OK) {
PrintError( error );
return -1;
}
// Set all cameras to a specific mode and frame rate so they can be synchronized
// Retrieve frame rate property
Property frmRate;
frmRate.type = FRAME_RATE;
error = ppCameras[i]->GetProperty(&frmRate);
if (error != PGRERROR_OK)
{
PrintError( error );
return -1;
}
printf( "Framse rate is %3.2f fps\n", frmRate.absValue );
/*************** property setting ***************/
Property prop;
//prop.type = BRIGHTNESS;
//prop.type = AUTO_EXPOSURE;
//prop.type = SHARPNESS;
prop.type = GAMMA;
ppCameras[i]->GetProperty(&prop);
prop.absValue = 1.0f;
ppCameras[i]->SetProperty(&prop, true);
//prop.type = PAN;
//prop.type = TILT;
/*
prop.type = SHUTTER;
ppCameras[i]->GetProperty(&prop);
prop.absValue = 28.0f;
ppCameras[i]->SetProperty(&prop, true);
*/
prop.type = GAIN;
ppCameras[i]->GetProperty(&prop);
prop.absValue = 0.0f;
ppCameras[i]->SetProperty(&prop, true);
}
return 0;
}
int CameraPointGrey::connect(int devNum, int *setting, int sizeOfSetting)
{
return 0;
}
int CameraPointGrey::close()
{
return 0;
}
int CameraPointGrey::grabSingleFrame(unsigned char *imgBuf, int camIndex)
{
error = ppCameras[camIndex]->RetrieveBuffer(&image);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
//assert(image.GetDataSize() == width * height);
memcpy(imgBuf, image.GetData(), width * height);
return 0;
}
int CameraPointGrey::grabMultiFrames(unsigned char *imgBuf, int imgNum, int camIndex)
{
for (int i = 0; i < imgNum; i++) {
error = ppCameras[camIndex]->RetrieveBuffer(&image);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
assert(image.GetDataSize() == width * height);
memcpy(imgBuf + i * width * height, image.GetData(), width * height);
}
return 0;
}
void CameraPointGrey::PrintBuildInfo()
{
FC2Version fc2Version;
Utilities::GetLibraryVersion(&fc2Version);
char version[128];
sprintf(version, "FlyCapture2 library version: %d.%d.%d.%d\n\n",
fc2Version.major, fc2Version.minor, fc2Version.type, fc2Version.build);
printf("%s", version);
/*
char timeStamp[512];
sprintf(timeStamp, "Application build date: %s %s\n\n", __DATE__, __TIME__ );
printf("%s", timeStamp);
*/
}
void CameraPointGrey::PrintCameraInfo(CameraInfo *pCamInfo)
{
printf("\n*** CAMERA INFORMATION ***\n"
"Serial number - %u\n"
"Model - %s\n"
"Vendor - %s\n"
"Sensor - %s\n"
"Resolution - %s\n"
//"Interface - %u\n"
//"Bus Speed - %u\n"
"IIDC Version - %u\n"
"Firmware Version - %s\n"
"Firmware Build Time - %s\n\n",
pCamInfo->serialNumber,
pCamInfo->modelName,
pCamInfo->vendorName,
pCamInfo->sensorInfo,
pCamInfo->sensorResolution,
//pCamInfo->interfaceType,
//pCamInfo->maximumBusSpeed,
pCamInfo->iidcVer,
pCamInfo->firmwareVersion,
pCamInfo->firmwareBuildTime
);
}
void CameraPointGrey::PrintFormat7Capabilities(Format7Info fmt7Info)
{
printf("Max image pixels: (%u, %u)\n"
"Image Unit size: (%u, %u)\n"
"Offset Unit size: (%u, %u)\n"
"Pixel format bitfield: 0x%08x\n",
fmt7Info.maxWidth,
fmt7Info.maxHeight,
fmt7Info.imageHStepSize,
fmt7Info.imageVStepSize,
fmt7Info.offsetHStepSize,
fmt7Info.offsetVStepSize,
fmt7Info.pixelFormatBitField
);
}
void CameraPointGrey::PrintError(Error error)
{
error.PrintErrorTrace();
}
bool CameraPointGrey::PollForTriggerReady(FlyCapture2::Camera *pCam)
{
const unsigned int k_softwareTrigger = 0x62C;
Error error;
unsigned int regVal = 0;
do {
error = pCam->ReadRegister(k_softwareTrigger, ®Val);
if (error != PGRERROR_OK) {
PrintError(error);
return false;
}
} while ((regVal >> 31) != 0 );
return true;
}
#endif