source: pacpussensors/trunk/PtGreyCameras/Flea3Grabber.cpp@ 54

Last change on this file since 54 was 53, checked in by phudelai, 10 years ago

StereoVisionDisparity updated

File size: 41.1 KB
RevLine 
[51]1/*******************************************************************************
2// created: 2013/10/25 - 19:36
3// filename: Flea3Grabber.cpp
4//
5// author: Danilo Alves de Lima and Pierre Hudelaine
6// Copyright Heudiasyc UMR UTC/CNRS 6599
7//
8// version: $Id: $
9//
10// purpose: Grabbe images from multiple flea3
11//
12*******************************************************************************/
13
14#include "Flea3Grabber.h"
15
16#include <iomanip>
[53]17#include <iostream>
[51]18
19#include "Pacpus/kernel/DbiteException.h"
20#include "Pacpus/kernel/DbiteFileTypes.h"
21#include "Pacpus/kernel/ComponentFactory.h"
22#include "Pacpus/kernel/Log.h"
23
24using namespace std;
25using namespace pacpus;
26
27DECLARE_STATIC_LOGGER("pacpus.base.Flea3Grabber");
28
29// Construct the factory
30static ComponentFactory<Flea3Grabber> sFactory("Flea3Grabber");
31
32const int kMaxFilepathLength = 512; // TODO: should be same for all images
33
34
35////////////////////////////////////////////////////////////////////////////////
36/// Constructor
37////////////////////////////////////////////////////////////////////////////////
38Flea3Grabber::Flea3Grabber(QString name)
39 : ComponentBase(name)
40{
41 LOG_TRACE(Q_FUNC_INFO);
42
43 this->nbrCamera_ = 1;
44 this->masterCamera_ = 0;
45}
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Destructor
50////////////////////////////////////////////////////////////////////////////////
51Flea3Grabber::~Flea3Grabber()
52{
53 LOG_TRACE(Q_FUNC_INFO);
54
55 for (int i = 0; i < nbrCamera_; i++)
56 {
57 if (this->shmem_images_[i])
58 delete this->shmem_images_[i];
59
60 this->shmem_images_[i] = NULL;
61 }
62}
63
64
65////////////////////////////////////////////////////////////////////////////////
66/// Called by the ComponentManager to start the component
67////////////////////////////////////////////////////////////////////////////////
68void Flea3Grabber::startActivity()
69{
70 LOG_TRACE(Q_FUNC_INFO);
71
72 for (int i = 0; i < nbrCamera_; i++)
73 {
74
75 if (((FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat == FlyCapture2::PIXEL_FORMAT_MONO8)||
76 ((FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat == FlyCapture2::PIXEL_FORMAT_MONO12)||
77 ((FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat == FlyCapture2::PIXEL_FORMAT_MONO16))
78 {
79 this->settings_[i].cam_channels = 1;
80 }
81 else
82 {
83 if (this->settings_[i].cam_ColorProcessingAlgorithm == 1)
84 this->settings_[i].cam_channels = 1;
85 else
86 this->settings_[i].cam_channels = 3;
87 }
88
89 // If do not rescale the image before save
90 this->settings_[i].mMaxImageOutputSize = sizeof(unsigned char) * this->settings_[i].cam_width * this->settings_[i].cam_height * this->settings_[i].cam_channels;
91
92 if (this->settings_[i].image_scale == 1.0)
93 this->settings_[i].mSaveImageSize = this->settings_[i].mMaxImageOutputSize;
94 else
95 this->settings_[i].mSaveImageSize = sizeof(unsigned char)*((int)(this->settings_[i].cam_width * this->settings_[i].image_scale)) * ((int)(this->settings_[i].cam_height * this->settings_[i].image_scale)) * this->settings_[i].cam_channels;
96
97 // Create output files for recording
98 if (isRecording())
99 {
100 try
101 {
102 QString dbtFileName_ = this->settings_[i].mOutputDirectory.filePath(name() + ".dbt");
103
104 if (this->settings_[i].save2dbt)
105 {
106 //std::cout << "Save file : " << dbtFileName_.toStdString() << std::endl;
107 this->mDbtImage.open(dbtFileName_.toStdString(), WriteMode, FILE_DBT_UNKNOWN, this->settings_[i].mSaveImageSize);
108 //this->mDbtImage.open(dbtFileName_.toStdString(), WriteMode, FILE_JPEG, kMaxFilepathLength);
109 }
110 else
111 {
112 //std::cout << "Save file : " << dbtFileName_.toStdString() << std::endl;
113 this->mDbtImage.open(dbtFileName_.toStdString(), WriteMode, FILE_IMAGE, kMaxFilepathLength);
114 //this->mDbtImage.open(dbtFileName_.toStdString(), WriteMode, FILE_JPEG, kMaxFilepathLength);
115 }
116 }
117 catch (DbiteException & e)
118 {
119 cerr << "error opening dbt file: " << e.what() << endl;
120 }
121 }
122 }
123
124 // Run thread
125 THREAD_ALIVE = true;
126 start();
127}
128
129
130////////////////////////////////////////////////////////////////////////////////
131/// Called by the ComponentManager to stop the component
132////////////////////////////////////////////////////////////////////////////////
133void Flea3Grabber::stopActivity()
134{
135 LOG_TRACE(Q_FUNC_INFO);
136
137 if (THREAD_ALIVE)
138 {
139 // Stop thread
140 THREAD_ALIVE = false;
141
142 while(is_running)
143 {
144 msleep(10);
145 }
146
147 }
148
149 // Close DBT file
150 if (isRecording())
151 {
152 this->mDbtImage.close();
153 }
154
155 LOG_INFO("stopped component '" << name() << "'");
156}
157
158
159////////////////////////////////////////////////////////////////////////////////
160/// Called by the ComponentManager to pass the XML parameters to the
161/// component
162////////////////////////////////////////////////////////////////////////////////
163ComponentBase::COMPONENT_CONFIGURATION Flea3Grabber::configureComponent(XmlComponentConfig config)
164{
165 LOG_TRACE(Q_FUNC_INFO);
166
167 if (config.getProperty("number_of_cameras") != QString::null)
168 this->nbrCamera_ = config.getProperty("number_of_cameras").toInt();
169
170 if (config.getProperty("master_indice") != QString::null)
171 this->masterCamera_ = config.getProperty("master_indice").toInt();
172
173 if (config.getProperty("use_shmem") != QString::null)
[53]174 this->use_shmem = (bool)config.getProperty("use_shmem").toInt();
[51]175
[53]176 if (config.getProperty("shmem_corrected") != QString::null)
177 this->shmem_corrected = (bool)config.getProperty("shmem_corrected").toInt();
178
179 if (config.getProperty("cam_translationx") != QString::null)
180 trans_[0] = config.getProperty("cam_translationx").toFloat();
181 if (config.getProperty("cam_translationy") != QString::null)
182 trans_[1] = config.getProperty("cam_translationy").toFloat();
183 if (config.getProperty("cam_translationz") != QString::null)
184 trans_[2] = config.getProperty("cam_translationz").toFloat();
185
186 if (config.getProperty("cam_rotationx") != QString::null)
187 rot_[0] = config.getProperty("cam_rotationx").toFloat();
188 if (config.getProperty("cam_rotationy") != QString::null)
189 rot_[1] = config.getProperty("cam_rotationy").toFloat();
190 if (config.getProperty("cam_rotationz") != QString::null)
191 rot_[2] = config.getProperty("cam_rotationz").toFloat();
192
[51]193 for (int i = 0; i < nbrCamera_; i++)
194 {
195 // Initialize with default values
196 settings_.push_back(camSetting());
197 this->InitDefault(i);
198
199 if (config.getProperty("cam_serial" + QString::number(i)) != QString::null)
200 settings_[i].cam_serial = config.getProperty("cam_serial" + QString::number(i)).toInt();
201
202 //---------------------------------------- Camera configuration --------------------------------------------
203 if (config.getProperty("auto_FrameRate" + QString::number(i)) != QString::null)
204 settings_[i].auto_FrameRate = (config.getProperty("auto_FrameRate" + QString::number(i)) == "true" ? true : false);
205
206 if (config.getProperty("cam_FrameRate" + QString::number(i)) != QString::null)
207 settings_[i].cam_FrameRate = config.getProperty("cam_FrameRate" + QString::number(i)).toDouble();
208
209 if (config.getProperty("auto_Gain" + QString::number(i)) != QString::null)
210 settings_[i].auto_Gain = (config.getProperty("auto_Gain" + QString::number(i)) == "true" ? true : false);
211
212 if (config.getProperty("cam_Gain" + QString::number(i)) != QString::null)
213 settings_[i].cam_Gain = config.getProperty("cam_Gain" + QString::number(i)).toDouble();
214
215 if (config.getProperty("auto_Exposure" + QString::number(i)) != QString::null)
216 settings_[i].auto_Exposure = (config.getProperty("auto_Exposure" + QString::number(i)) == "true" ? true : false);
217
218 if (config.getProperty("cam_Exposure" + QString::number(i)) != QString::null)
219 settings_[i].cam_Exposure = config.getProperty("cam_Exposure" + QString::number(i)).toDouble();
220
221 if (config.getProperty("auto_Shutter" + QString::number(i)) != QString::null)
222 settings_[i].auto_Shutter = (config.getProperty("auto_Shutter" + QString::number(i)) == "true" ? true : false);
223
224 if (config.getProperty("cam_Shutter" + QString::number(i)) != QString::null)
225 settings_[i].cam_Shutter = config.getProperty("cam_Shutter" + QString::number(i)).toDouble();
226
227 if (config.getProperty("auto_ExposurebyCode" + QString::number(i)) != QString::null)
228 settings_[i].auto_ExposurebyCode = (config.getProperty("auto_ExposurebyCode" + QString::number(i)) == "true" ? true : false);
229
230 if (config.getProperty("cam_ExposurebyCode_tshold" + QString::number(i)) != QString::null)
231 settings_[i].cam_ExposurebyCode_tshold = config.getProperty("cam_ExposurebyCode_tshold" + QString::number(i)).toDouble();
232 //----------------------------------------------------------------------------------------------------------
233
234 if (config.getProperty("cam_video_mode" + QString::number(i)) != QString::null)
235 settings_[i].cam_video_mode = config.getProperty("cam_video_mode" + QString::number(i)).toInt();
236
237 if (config.getProperty("cam_mode" + QString::number(i)) != QString::null)
238 settings_[i].cam_mode = config.getProperty("cam_mode" + QString::number(i)).toInt();
239
240 if (config.getProperty("cam_PixelFormat" + QString::number(i)) != QString::null)
241 settings_[i].cam_PixelFormat = config.getProperty("cam_PixelFormat" + QString::number(i)).toInt();
242
243 if (config.getProperty("cam_ColorProcessingAlgorithm" + QString::number(i)) != QString::null)
244 settings_[i].cam_ColorProcessingAlgorithm = config.getProperty("cam_ColorProcessingAlgorithm" + QString::number(i)).toInt();
245
246 if (config.getProperty("cam_start_point_left" + QString::number(i)) != QString::null)
247 settings_[i].cam_start_point_left = config.getProperty("cam_start_point_left" + QString::number(i)).toInt();
248
249 if (config.getProperty("cam_start_point_top" + QString::number(i)) != QString::null)
250 settings_[i].cam_start_point_top = config.getProperty("cam_start_point_top" + QString::number(i)).toInt();
251
252 if (config.getProperty("cam_width" + QString::number(i)) != QString::null)
253 settings_[i].cam_width = config.getProperty("cam_width" + QString::number(i)).toInt();
254
255 if (config.getProperty("cam_height" + QString::number(i)) != QString::null)
256 settings_[i].cam_height = config.getProperty("cam_height" + QString::number(i)).toInt();
257
258 //-------------------------------- Trigger/strobe options -------------------------------------------
259
260 if (config.getProperty("cam_trigger_mode" + QString::number(i)) != QString::null)
261 settings_[i].cam_trigger_mode = config.getProperty("cam_trigger_mode" + QString::number(i)).toInt();
262
263 if (config.getProperty("cam_trigger_enable" + QString::number(i)) != QString::null)
264 settings_[i].cam_trigger_enable = config.getProperty("cam_trigger_enable" + QString::number(i)).toInt();
265
266 if (config.getProperty("cam_trigger_parameter" + QString::number(i)) != QString::null)
267 settings_[i].cam_trigger_parameter = config.getProperty("cam_trigger_parameter" + QString::number(i)).toInt();
268
269 if (config.getProperty("cam_trigger_polarity" + QString::number(i)) != QString::null)
270 settings_[i].cam_trigger_polarity = config.getProperty("cam_trigger_polarity" + QString::number(i)).toInt();
271
272 if (config.getProperty("cam_trigger_source" + QString::number(i)) != QString::null)
273 settings_[i].cam_trigger_source = config.getProperty("cam_trigger_source" + QString::number(i)).toInt();
274
275 if (config.getProperty("cam_trigger_dest" + QString::number(i)) != QString::null)
276 settings_[i].cam_trigger_dest = config.getProperty("cam_trigger_dest" + QString::number(i)).toInt();
277
278 if (config.getProperty("cam_strobe_enable" + QString::number(i)) != QString::null)
279 settings_[i].cam_strobe_enable = config.getProperty("cam_strobe_enable" + QString::number(i)).toInt();
280
281 if (config.getProperty("cam_strobe_source" + QString::number(i)) != QString::null)
282 settings_[i].cam_strobe_source = config.getProperty("cam_strobe_source" + QString::number(i)).toInt();
283
284 if (config.getProperty("cam_strobe_polarity" + QString::number(i)) != QString::null)
285 settings_[i].cam_strobe_polarity = config.getProperty("cam_strobe_polarity" + QString::number(i)).toInt();
286
287 if (config.getProperty("cam_strobe_delay" + QString::number(i)) != QString::null)
288 settings_[i].cam_strobe_delay = config.getProperty("cam_strobe_delay" + QString::number(i)).toInt();
289
290 if (config.getProperty("cam_strobe_duration" + QString::number(i)) != QString::null)
291 settings_[i].cam_strobe_duration = config.getProperty("cam_strobe_duration" + QString::number(i)).toInt();
292
[53]293 //-------------------------------- Camera settings --------------------------------------------------
294
295 if (config.getProperty("cam_fx" + QString::number(i)) != QString::null)
296 settings_[i].tmp_matrix[0][0] = config.getProperty("cam_fx" + QString::number(i)).toDouble();// / settings_[i].cam_width;
297 if (config.getProperty("cam_fy" + QString::number(i)) != QString::null)
298 settings_[i].tmp_matrix[1][1] = config.getProperty("cam_fy" + QString::number(i)).toDouble();// / settings_[i].cam_height;
299 if (config.getProperty("cam_cx" + QString::number(i)) != QString::null)
300 settings_[i].tmp_matrix[0][2] = config.getProperty("cam_cx" + QString::number(i)).toDouble();// / settings_[i].cam_width;
301 if (config.getProperty("cam_cy" + QString::number(i)) != QString::null)
302 settings_[i].tmp_matrix[1][2] = config.getProperty("cam_cy" + QString::number(i)).toDouble();// / settings_[i].cam_height;
303 settings_[i].tmp_matrix[2][2] = 1.0;
304
305 for (int j = 0; j < 8; j++) // 8 max parameters for the distortion in OpenCV
306 if (config.getProperty("cam_distk" + QString::number(j + 1) + QString::number(i)) != QString::null)
307 settings_[i].distCoe[j] = config.getProperty("cam_distk" + QString::number(j + 1) + QString::number(i)).toDouble();
308
[51]309 //-------------------------------- Recording options ------------------------------------------------
310 if (config.getProperty("recording") != QString::null)
311 setRecording(config.getProperty("recording").toInt());
312
313 if (config.getProperty("image_scale" + QString::number(i)) != QString::null)
314 settings_[i].image_scale = config.getProperty("image_scale" + QString::number(i)).toDouble();
315
316 if (config.getProperty("image_compact" + QString::number(i)) != QString::null)
317 settings_[i].image_compact = config.getProperty("image_compact" + QString::number(i)).toInt();
318
319 if (config.getProperty("save2dbt" + QString::number(i)) != QString::null)
320 settings_[i].save2dbt = config.getProperty("save2dbt" + QString::number(i)).toInt();
321 //---------------------------------------------------------------------------------------------------
322
323 if (config.getProperty("showdebug" + QString::number(i)) != QString::null)
324 settings_[i].showdebug = (bool)config.getProperty("showdebug" + QString::number(i)).toInt();
325
326 if (config.getProperty("outputdir" + QString::number(i)) != QString::null)
327 {
328 if (isRecording())
329 {
330 settings_[i].mOutputDirectory.mkdir(config.getProperty("outputdir" + QString::number(i)));
331 settings_[i].mOutputDirectory.mkdir(config.getProperty("outputdir" + QString::number(i) + "/" + name()));
332 settings_[i].mOutputDirectory.setPath(config.getProperty("outputdir" + QString::number(i)));
333 }
334 }
335 else
336 {
337 if (isRecording())
338 {
339 settings_[i].mOutputDirectory.mkdir(name());
340 settings_[i].mOutputDirectory.setPath(settings_[i].mOutputDirectory.currentPath());
341 }
342 }
343 }
344
345 if (masterCamera_ >= nbrCamera_)
346 {
347 LOG_ERROR("masterCamera number error");
348 ComponentManager::getInstance()->stop(name());
349 }
350
351 LOG_INFO("configured component '" << name() << "'");
352 return ComponentBase::CONFIGURED_OK;
353}
354
355
356////////////////////////////////////////////////////////////////////////////////
357/// Initialize default values
358////////////////////////////////////////////////////////////////////////////////
359void Flea3Grabber::InitDefault(int indice)
360{
361 // Default
362 setRecording(0);
363
364 this->settings_[indice].cam_serial = 0; // Camera index to connect
365
366 // Camera configuration
367 this->settings_[indice].auto_FrameRate = false; // Set auto frame rate
368 this->settings_[indice].cam_FrameRate = 15.0; // Frame rates in frames per second = FlyCapture2::FRAMERATE_15
369 this->settings_[indice].auto_Gain = false; // Set auto gain
370 this->settings_[indice].cam_Gain = 1.0; // Gain value in db
371 this->settings_[indice].auto_Exposure = true; // Set auto exposure
372 this->settings_[indice].cam_Exposure = 2.414; // Auto exposure in EV
373 this->settings_[indice].auto_Shutter = true; // Set auto shutter
374 this->settings_[indice].cam_Shutter = 66.639; // Shutter in miliseconds
375 this->settings_[indice].auto_ExposurebyCode = false; // Set auto exposure by pos processing method
376 this->settings_[indice].cam_ExposurebyCode_tshold = 20.0; // Pecentage of white pixels for threshold
377 this->settings_[indice].cam_start_point_left = 0; // Image left point (for standard modes only)
378 this->settings_[indice].cam_start_point_top = 0; // Image top point (for standard modes only)
379 this->settings_[indice].cam_width = 4096; // Image width (for standard modes only)
380 this->settings_[indice].cam_height = 2160; // image height (for standard modes only)
381 this->settings_[indice].cam_video_mode = FlyCapture2::VIDEOMODE_FORMAT7; // DCAM video modes
382 this->settings_[indice].cam_mode = FlyCapture2::MODE_0; // Camera modes for DCAM formats as well as Format7
383 this->settings_[indice].cam_PixelFormat = FlyCapture2::PIXEL_FORMAT_RAW8; // Pixel formats available for Format7 modes7
384 this->settings_[indice].cam_trigger_mode = 0; // Trigger mode
385 this->settings_[indice].cam_trigger_enable = 0; // Trigger enable (1 on // 0 off)
386 this->settings_[indice].cam_trigger_parameter = 0;
387 this->settings_[indice].cam_trigger_polarity = 0; // Trigger polarity (0 low // 1 high)
388 this->settings_[indice].cam_trigger_source = 0; // Source (GPIO)
389 this->settings_[indice].cam_trigger_dest = 0; // Dest (GPIO) input/output
390 this->settings_[indice].cam_strobe_source = 0;
391 this->settings_[indice].cam_strobe_enable = 0; // Enable strobe mode
392 this->settings_[indice].cam_strobe_polarity = 0; // Strobe polarity
393 this->settings_[indice].cam_strobe_delay = 0;
394 this->settings_[indice].cam_strobe_duration = 0;
395 this->settings_[indice].cam_ColorProcessingAlgorithm = FlyCapture2::NEAREST_NEIGHBOR; /**
396 * Color processing algorithms. Please refer to our knowledge base at
397 * article at http://www.ptgrey.com/support/kb/index.asp?a=4&q=33 for
398 * complete details for each algorithm.
399 */
400 this->settings_[indice].showdebug = false; // Show frame acquired
401
402 if (this->settings_[indice].cam_ColorProcessingAlgorithm == 1)
403 this->settings_[indice].cam_channels = 1;
404 else
405 this->settings_[indice].cam_channels = 3;
406
407 // Size of the image data sizeof(char)*width*height*channels
408 this->settings_[indice].mMaxImageOutputSize = sizeof(char) * this->settings_[indice].cam_width * this->settings_[indice].cam_height * this->settings_[indice].cam_channels;
409 this->settings_[indice].image_scale = 1.0; // Default scale
410 this->settings_[indice].image_compact = 0; // Don't compact the image data
411 this->settings_[indice].save2dbt = 0;
[53]412
413 for (int i = 0; i < 3; i++)
414 for (int j = 0; j < 3; j++)
415 settings_[indice].tmp_matrix[i][j] = 0.0;
416
417 for (int i = 0; i < 8; i++) // 8 max for dist coeffs
418 settings_[indice].distCoe[i] = 0.0;
[51]419}
420
421
422////////////////////////////////////////////////////////////////////////////////
423/// Thread loop
424////////////////////////////////////////////////////////////////////////////////
425void Flea3Grabber::run()
426{
427 LOG_TRACE(Q_FUNC_INFO);
428
429 this->is_running = true;
430
[53]431 // Tmp matrice used because of intelligent pointer of OpenCV and "intelligent" memory free
432 cv::Mat_<double> tmp_cameraMatrix(3,3); // 3x3 matrix
433 cv::Mat_<double> tmp_distCoeffs(8,1); // 5x1 matrix
434 cv::Mat_<double> R(3,1); // 3x1 matrix, rotation left to right camera
435 cv::Mat_<double> T(3,1); // 3x1 matrix, translation left to right proj. center
436 cv::Mat R1, R2, P1, P2, Q;
437 std::vector<cv::Mat> map1(nbrCamera_);
438 std::vector<cv::Mat> map2(nbrCamera_);
439 std::vector<cv::Mat> correctedImg(nbrCamera_);
440
441 for (int i = 0; i < nbrCamera_; i++)
442 {
443 correctedImg.push_back(cv::Mat(settings_[i].cam_height, settings_[i].cam_width, CV_MAKE_TYPE(CV_8U, this->settings_[i].cam_channels)));
444 map1.push_back(cv::Mat());
445 map2.push_back(cv::Mat());
446
447 tmp_cameraMatrix << settings_[i].tmp_matrix[0][0], settings_[i].tmp_matrix[0][1], settings_[i].tmp_matrix[0][2], settings_[i].tmp_matrix[1][0], settings_[i].tmp_matrix[1][1], settings_[i].tmp_matrix[1][2], settings_[i].tmp_matrix[2][0], settings_[i].tmp_matrix[2][1], settings_[i].tmp_matrix[2][2];
448 settings_[i].matrix = tmp_cameraMatrix;
449 LOG_INFO(settings_[i].matrix);
450 tmp_distCoeffs << settings_[i].distCoe[0], settings_[i].distCoe[1], settings_[i].distCoe[2], settings_[i].distCoe[3], settings_[i].distCoe[4], settings_[i].distCoe[5], settings_[i].distCoe[6], settings_[i].distCoe[7];
451 settings_[i].distCoeffs = tmp_distCoeffs;
452 LOG_INFO(settings_[i].distCoeffs);
453 }
454
455 // Used only for two cameras
456 if (nbrCamera_ == 2)
457 {
458 // Initialisation des matrices pour les calculs OpenCV
459 R << rot_[0], rot_[1], rot_[2];
460 T << trans_[0], trans_[1], trans_[2];
461
462 cv::stereoRectify(settings_[0].matrix, settings_[0].distCoeffs, settings_[1].matrix, settings_[1].distCoeffs, cv::Size(settings_[0].cam_width, settings_[0].cam_height), R, T, R1, R2, P1, P2, Q, CV_CALIB_ZERO_DISPARITY);
463
464 cv::initUndistortRectifyMap(settings_[0].matrix, settings_[0].distCoeffs, R1, P1, cv::Size(settings_[0].cam_width, settings_[0].cam_height), CV_16SC2, map1.at(0), map2.at(0));
465 cv::initUndistortRectifyMap(settings_[1].matrix, settings_[1].distCoeffs, R2, P2, cv::Size(settings_[1].cam_width, settings_[1].cam_height), CV_16SC2, map1.at(1), map2.at(1));
466 }
467
[51]468 std::vector<FlyCapture2::Camera *> cams;
[53]469 std::vector<FlyCapture2::Image> convertedImage;
[51]470
471 //==================================== Camera initialization =================================================
472 // Check the number of cameras
473 FlyCapture2::BusManager busMgr;
474 unsigned int numCameras;
475 FlyCapture2::Error error = busMgr.GetNumOfCameras(&numCameras);
476
477 if (error != FlyCapture2::PGRERROR_OK)
478 {
479 LOG_FATAL(error.GetDescription());
480 ComponentManager::getInstance()->stop(name());
481 }
482
483 if (this->nbrCamera_ != numCameras)
484 {
485 LOG_FATAL("Incoorect number of cameras...");
486 ComponentManager::getInstance()->stop(name());
487 }
488
489 for (int i = 0; i < this->nbrCamera_; i++)
[53]490 {
[51]491 if (this->use_shmem)
492 {
493 this->settings_[i].ImageHeader.image.width = this->settings_[i].cam_width;
494 this->settings_[i].ImageHeader.image.height = this->settings_[i].cam_height;
495 this->settings_[i].ImageHeader.image.channels = this->settings_[i].cam_channels;
496 this->settings_[i].ImageHeader.image.width_step = this->settings_[i].cam_width * this->settings_[i].cam_channels * sizeof(unsigned char);
497 this->settings_[i].ImageHeader.image.data_size = this->settings_[i].cam_width * this->settings_[i].cam_height;
498
499 this->settings_[i].img_mem_size = sizeof(TimestampedStructImage) + settings_[i].mMaxImageOutputSize;
500 this->settings_[i].img_mem = malloc(this->settings_[i].img_mem_size);
501
502 ShMem * tmp_shmem = new ShMem((name() + "_" + QString().setNum(settings_[i].cam_serial)).toStdString().c_str(), this->settings_[i].img_mem_size);
503
504 shmem_images_.push_back(tmp_shmem);
505 }
506
507 // Get camera from serial (in this case first camera in the bus)
508 FlyCapture2::PGRGuid guid;
509 error = busMgr.GetCameraFromSerialNumber(this->settings_[i].cam_serial, &guid);
510 if (error != FlyCapture2::PGRERROR_OK)
511 {
512 LOG_ERROR(error.GetDescription());
513 ComponentManager::getInstance()->stop(name());
514 }
515
516 cams.push_back(new FlyCapture2::Camera); // FlyCapture camera class
517
518 // Connect to a camerak
519 error = cams[i]->Connect(&guid);
520 if (error != FlyCapture2::PGRERROR_OK)
521 {
522 LOG_ERROR(error.GetDescription());
523 ComponentManager::getInstance()->stop(name());
524 }
525
526 // Get the camera information
527 FlyCapture2::CameraInfo camInfo;
528 error = cams[i]->GetCameraInfo(&camInfo);
529 if (error != FlyCapture2::PGRERROR_OK)
530 {
531 LOG_ERROR(error.GetDescription());
532 ComponentManager::getInstance()->stop(name());
533 }
534
535 // Just for visualization
[53]536 /*printf(
[51]537 "\n*** CAMERA INFORMATION ***\n"
538 "Serial number - %u\n"
539 "Camera model - %s\n"
540 "Camera vendor - %s\n"
541 "Sensor - %s\n"
542 "Resolution - %s\n"
543 "Firmware version - %s\n"
544 "Firmware build time - %s\n\n",
545 camInfo.serialNumber,
546 camInfo.modelName,
547 camInfo.vendorName,
548 camInfo.sensorInfo,
549 camInfo.sensorResolution,
550 camInfo.firmwareVersion,
[53]551 camInfo.firmwareBuildTime);*/
[51]552
553 // Query for available Format 7 modes
554 FlyCapture2::Format7Info fmt7Info;
555 bool supported;
556 fmt7Info.mode = (FlyCapture2::Mode)this->settings_[i].cam_mode;
557 error = cams[i]->GetFormat7Info(&fmt7Info, &supported);
558 if (error != FlyCapture2::PGRERROR_OK)
559 {
560 LOG_ERROR(error.GetDescription());
561 ComponentManager::getInstance()->stop(name());
562 }
563
564 if (((FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat & fmt7Info.pixelFormatBitField) == 0)
565 {
566 // Pixel format not supported!
567 LOG_ERROR("Pixel format is not supported\n");
568 ComponentManager::getInstance()->stop(name());
569 }
570
571 // Configurate custom image
572 FlyCapture2::Format7ImageSettings fmt7ImageSettings;
573 fmt7ImageSettings.mode = (FlyCapture2::Mode)this->settings_[i].cam_mode;
574 fmt7ImageSettings.offsetX = this->settings_[i].cam_start_point_left;
575 fmt7ImageSettings.offsetY = this->settings_[i].cam_start_point_top;
576 fmt7ImageSettings.width = this->settings_[i].cam_width;
577 fmt7ImageSettings.height = this->settings_[i].cam_height;
578 fmt7ImageSettings.pixelFormat = (FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat;
579
580 bool valid;
581 FlyCapture2::Format7PacketInfo fmt7PacketInfo;
582
583 // Validate the settings to make sure that they are valid
584 error = cams[i]->ValidateFormat7Settings(
585 &fmt7ImageSettings,
586 &valid,
587 &fmt7PacketInfo);
588 if (error != FlyCapture2::PGRERROR_OK)
589 {
590 LOG_ERROR(error.GetDescription());
591 ComponentManager::getInstance()->stop(name());
592 }
593
594 if (!valid)
595 {
596 // Settings are not valid
597 LOG_ERROR("Format7 settings are not valid");
598 ComponentManager::getInstance()->stop(name());
599 }
600
601 // Set the settings to the camera
602 error = cams[i]->SetFormat7Configuration(
603 &fmt7ImageSettings,
604 fmt7PacketInfo.recommendedBytesPerPacket);
605 if (error != FlyCapture2::PGRERROR_OK)
606 {
607 LOG_ERROR(error.GetDescription());
608 ComponentManager::getInstance()->stop(name());
609 }
610
611 // Define the color algorithm
612 FlyCapture2::Image::SetDefaultColorProcessing((FlyCapture2::ColorProcessingAlgorithm)this->settings_[i].cam_ColorProcessingAlgorithm);
613
614 // Start capturing images
615 error = cams[i]->StartCapture();
616 if (error != FlyCapture2::PGRERROR_OK)
617 {
618 LOG_ERROR(error.GetDescription());
619 ComponentManager::getInstance()->stop(name());
620 }
621
622 // Set frame rate property
623 FlyCapture2::Property cam_property;
624 cam_property.type = (FlyCapture2::PropertyType)16; //FlyCapture2::PropertyType::FRAME_RATE;
625 cam_property.absControl = true;
626 cam_property.autoManualMode = this->settings_[i].auto_FrameRate;
627 cam_property.onOff = true;
628 cam_property.onePush = false;
629 cam_property.absValue = (float)this->settings_[i].cam_FrameRate;
630 error = cams[i]->SetProperty(&cam_property);
631 if (error != FlyCapture2::PGRERROR_OK)
632 {
633 LOG_ERROR(error.GetDescription());
634 ComponentManager::getInstance()->stop(name());
635 }
636
637 // Set gain property
638 cam_property.type = (FlyCapture2::PropertyType)13; //FlyCapture2::PropertyType::GAIN;
639 cam_property.absControl = true;
640 cam_property.autoManualMode = this->settings_[i].auto_Gain;
641 cam_property.onOff = true;
642 cam_property.onePush = false;
643 cam_property.absValue = (float)this->settings_[i].cam_Gain;
644 error = cams[i]->SetProperty(&cam_property);
645 if (error != FlyCapture2::PGRERROR_OK)
646 {
647 LOG_ERROR(error.GetDescription());
648 ComponentManager::getInstance()->stop(name());
649 }
650
651 // Set exposure property
652 cam_property.type = (FlyCapture2::PropertyType)1; //FlyCapture2::PropertyType::AUTO_EXPOSURE;
653 cam_property.absControl = true;
654 cam_property.onePush = false;
655 cam_property.onOff = true;
656 cam_property.autoManualMode = this->settings_[i].auto_Exposure;
657 cam_property.absValue = (float)this->settings_[i].cam_Exposure;
658 error = cams[i]->SetProperty(&cam_property);
659 if (error != FlyCapture2::PGRERROR_OK)
660 {
661 LOG_ERROR(error.GetDescription());
662 ComponentManager::getInstance()->stop(name());
663 }
664
665 // Set shutter property
666 cam_property.type = (FlyCapture2::PropertyType)12; //FlyCapture2::PropertyType::SHUTTER;
667 cam_property.absControl = true;
668 cam_property.onePush = false;
669 cam_property.onOff = true;
670 cam_property.autoManualMode = this->settings_[i].auto_Shutter;
671 cam_property.absValue = (float)this->settings_[i].cam_Shutter;
672 error = cams[i]->SetProperty(&cam_property);
673 if (error != FlyCapture2::PGRERROR_OK)
674 {
675 LOG_ERROR(error.GetDescription());
676 ComponentManager::getInstance()->stop(name());
677 }
678
679 if (settings_[i].cam_trigger_enable)
680 {
681 FlyCapture2::TriggerMode trigger;
682 trigger.mode = settings_[i].cam_trigger_mode;
683 trigger.onOff = settings_[i].cam_trigger_enable;
684 trigger.parameter = settings_[i].cam_trigger_parameter;
685 trigger.polarity = settings_[i].cam_trigger_polarity;
686 trigger.source = settings_[i].cam_trigger_source;
687 error = cams[i]->SetTriggerMode(&trigger);
688 if (error != FlyCapture2::PGRERROR_OK)
689 {
690 LOG_ERROR(error.GetDescription());
691 ComponentManager::getInstance()->stop(name());
692 }
693
694 error = cams[i]->SetGPIOPinDirection(settings_[i].cam_trigger_dest, (i == masterCamera_)? 1 : 0);
695 if (error != FlyCapture2::PGRERROR_OK)
696 {
697 LOG_ERROR(error.GetDescription());
698 ComponentManager::getInstance()->stop(name());
699 }
700 }
701
702 if (settings_[i].cam_strobe_enable)
703 {
704 FlyCapture2::StrobeControl strobe;
705 strobe.onOff = settings_[i].cam_strobe_enable;
706 strobe.source = settings_[i].cam_strobe_source;
707 strobe.delay = settings_[i].cam_strobe_delay;
708 strobe.duration = settings_[i].cam_strobe_duration;
709 strobe.polarity = settings_[i].cam_strobe_polarity;
710 error = cams[i]->SetStrobe(&strobe);
711 if (error != FlyCapture2::PGRERROR_OK)
712 {
713 LOG_ERROR(error.GetDescription());
714 ComponentManager::getInstance()->stop(name());
715 }
716 }
717 //============================================================================================================
718
719 //---------------------------------- Recording parameters ------------------------------------
720 //size_t imageCount = 0;
721
722 // Timestamp
723 //road_time_t time = 0;
724 //road_timerange_t tr = 0;
725
726 const char * const imagePrefix = "image";
727 const char * const imageNameDelimiter = "-";
728 const char * const imageExtension = this->settings_[i].image_compact ? ".jpg" :
729 (((FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat == FlyCapture2::PIXEL_FORMAT_MONO8)||
730 ((FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat == FlyCapture2::PIXEL_FORMAT_MONO12)||
731 ((FlyCapture2::PixelFormat)this->settings_[i].cam_PixelFormat == FlyCapture2::PIXEL_FORMAT_MONO16)) ? ".pgm" : ".ppm";
732
733 const char kFillCharacter = '0';
734 const int kNumberWidth = 5;
735 //--------------------------------------------------------------------------------------------
[53]736 FlyCapture2::Image tmp_img;
737 convertedImage.push_back(tmp_img);
[51]738 }
739
740 // Raw image
[53]741 FlyCapture2::Image rawImage;
[51]742
743 //bool shmen_init = false;
[53]744 // Because the API doesn't work without (saw this with FlyCapture2.exe)
745 if (settings_[masterCamera_].cam_trigger_enable)
746 {
747 cams[masterCamera_]->FireSoftwareTrigger();
748 usleep(50000);
749 cams[masterCamera_]->FireSoftwareTrigger();
750 usleep(50000);
751 }
[51]752
753 road_time_t time;
[53]754
[51]755 while (THREAD_ALIVE)
756 {
[53]757 if (settings_[masterCamera_].cam_trigger_enable)
758 {
759 error = cams[masterCamera_]->FireSoftwareTrigger();
760 if (error != FlyCapture2::PGRERROR_OK)
761 {
762 LOG_ERROR(error.GetDescription());
763 continue;
764 }
765 time = road_time();
766 }
[51]767
[53]768 //cout << settings_[1].timeStamp.microSeconds - settings_[0].timeStamp.microSeconds << endl;
[51]769
[53]770 for (int i = 0; i < this->nbrCamera_; i++)
[51]771 {
772 // Retrieve an image
773 error = cams[i]->RetrieveBuffer(&rawImage);
774 if (error != FlyCapture2::PGRERROR_OK)
775 {
776 LOG_ERROR(error.GetDescription());
777 continue;
778 }
779
780 // Image timestamp
[53]781 //settings_[i].timeStamp = rawImage.GetTimeStamp();
[51]782
783 //time = road_time(); // Use the same time base of the other sensors
784
785 if (this->settings_[i].cam_channels == 1)
786 {
787 // Convert the raw image
[53]788 error = rawImage.Convert(FlyCapture2::PIXEL_FORMAT_MONO8, &convertedImage[i]);
[51]789 if (error != FlyCapture2::PGRERROR_OK)
790 {
791 LOG_ERROR(error.GetDescription());
792 ComponentManager::getInstance()->stop(name());
793 }
794 }
795 else
796 {
797 // Convert the raw image
[53]798 error = rawImage.Convert(FlyCapture2::PIXEL_FORMAT_BGR, &convertedImage[i]);
[51]799 if (error != FlyCapture2::PGRERROR_OK)
800 {
801 LOG_ERROR(error.GetDescription());
802 ComponentManager::getInstance()->stop(name());
803 }
804 }
805
[53]806 cv::remap(cv::Mat(convertedImage[i].GetRows(), convertedImage[i].GetCols(), CV_MAKE_TYPE(CV_8U, this->settings_[i].cam_channels), convertedImage[i].GetData(), (int)((double)convertedImage[i].GetReceivedDataSize()/(double)convertedImage[i].GetRows())), correctedImg[i], map1.at(i), map2.at(i), CV_INTER_LINEAR);
807
[51]808 if (this->use_shmem)
809 {
810 // Complete timestamp header of the right image
811 this->settings_[i].ImageHeader.time = time;
812 this->settings_[i].ImageHeader.timerange = 0;
813
814 // Copy images header and data to memory
815 memcpy(this->settings_[i].img_mem, &settings_[i].ImageHeader, sizeof(TimestampedStructImage));
[53]816
817 if (shmem_corrected)
818 memcpy((void*)((TimestampedStructImage*)this->settings_[i].img_mem + 1), (void*)correctedImg[i].data, this->settings_[i].ImageHeader.image.data_size);
819 else
820 memcpy((void*)((TimestampedStructImage*)this->settings_[i].img_mem + 1), (void*)convertedImage[i].GetData(), this->settings_[i].ImageHeader.image.data_size);
821
[51]822 this->shmem_images_[i]->write(this->settings_[i].img_mem, this->settings_[i].img_mem_size);
823 }
824
[53]825 if (this->settings_[i].showdebug)
826 {
827 cv::namedWindow(QString("Flea3Component - Current Reference Frame " + QString::number(i)).toStdString(), CV_WINDOW_NORMAL);
828 cv::imshow(QString("Flea3Component - Current Reference Frame " + QString::number(i)).toStdString(), cv::Mat(convertedImage[i].GetRows(), convertedImage[i].GetCols(), CV_MAKE_TYPE(CV_8U, this->settings_[i].cam_channels), convertedImage[i].GetData(), (int)((double)convertedImage[i].GetReceivedDataSize()/(double)convertedImage[i].GetRows())));
829
830 cv::namedWindow(QString("Flea3Component - Current Corrected Frame " + QString::number(i)).toStdString(), CV_WINDOW_NORMAL);
831 cv::imshow(QString("Flea3Component - Current Corrected Frame " + QString::number(i)).toStdString(), cv::Mat(convertedImage[i].GetRows(), convertedImage[i].GetCols(), CV_MAKE_TYPE(CV_8U, this->settings_[i].cam_channels), correctedImg[i].data, (int)((double)convertedImage[i].GetReceivedDataSize()/(double)convertedImage[i].GetRows())));
832 cv::waitKey(1);
833 }
834
[51]835 /////////////////////
836 // Write images to disk
837 /*if (isRecording())
838 {
839 if (this->settings_[i].save2dbt)
840 {
841 //------------------------------------------------ Save image in the dbt file ------------------------------------------------------------
842 if (this->settings_[i].image_scale == 1.0)
843 {
844 try
845 {
846 this->mDbtImage.writeRecord(time, tr, (char *)convertedImage.GetData(), convertedImage.GetDataSize());
847 }
848 catch (DbiteException & e)
849 {
850 cerr << "error opening dbt file: " << e.what() << endl;
851 }
852 }
853 else
854 {
855 // convert to cv::mat
856 unsigned int rowBytes = (int)((double)convertedImage.GetReceivedDataSize()/(double)convertedImage.GetRows());
857 cv::Mat Img_BGR = cv::Mat(convertedImage.GetRows(), convertedImage.GetCols(), CV_MAKE_TYPE(CV_8U, this->settings_[i].cam_channels), convertedImage.GetData(), rowBytes);
858
859 // Scaled image
860 cv::Mat img_resized;
861
862 cv::resize(Img_BGR, img_resized, cv::Size((int)(Img_BGR.cols * this->settings_[i].image_scale), (int)(Img_BGR.rows * this->settings_[i].image_scale)), 0.0, 0.0, CV_INTER_CUBIC);
863
864 try
865 {
866 this->mDbtImage.writeRecord(time, tr, (char*)img_resized.data, this->mSaveImageSize);
867 }
868 catch (DbiteException & e)
869 {
870 cerr << "error opening dbt file: " << e.what() << endl;
871 }
872 }
873 //----------------------------------------------------------------------------------------------------------------------------------------
874 }
875 else
876 {
877 //------------------------------------------------------------ Save image file ------------------------------------------------------------
878 // Save the image to file
879 stringstream imageNameSs;
880
881 imageNameSs << name().toStdString() << "/" << imagePrefix << imageNameDelimiter << setfill(kFillCharacter) << setw(kNumberWidth) << imageCount << imageExtension;
882 string imageName = this->settings_[i].mOutputDirectory.filePath(imageNameSs.str().c_str()).toStdString();
883
884 //LOG_INFO("Recording frame...");
885 if ((this->settings_[i].image_compact == 0) && (this->settings_[i].image_scale == 1.0))
886 {
887 // Save the image. If a file format is not passed in, then the file
888 // extension is parsed to attempt to determine the file format.
889 error = convertedImage.Save(const_cast<char *>(imageName.c_str()));
890 if (error != FlyCapture2::PGRERROR_OK)
891 {
892 LOG_ERROR(error.GetDescription());
893 ComponentManager::getInstance()->stop(name());
894 }
895 }
896 else if (this->settings_[i].image_scale == 1.0)
897 {
898 // convert to cv::mat
899 unsigned int rowBytes = (int)((double)convertedImage.GetReceivedDataSize()/(double)convertedImage.GetRows());
900 cv::Mat Img_BGR = cv::Mat(convertedImage.GetRows(), convertedImage.GetCols(), CV_MAKE_TYPE(CV_8U, this->settings_[i].cam_channels), convertedImage.GetData(), rowBytes);
901
902 // Save the image. If a file format is not passed in, then the file
903 // extension is parsed to attempt to determine the file format.
904 if (!cv::imwrite(const_cast<char *>(imageName.c_str()), Img_BGR))
905 {
906 LOG_ERROR("Error to save the Flea image!");
907 ComponentManager::getInstance()->stop(name());
908 }
909 }
910 else
911 {
912 // convert to cv::mat
913 unsigned int rowBytes = (int)((double)convertedImage.GetReceivedDataSize()/(double)convertedImage.GetRows());
914 cv::Mat Img_BGR = cv::Mat(convertedImage.GetRows(), convertedImage.GetCols(), CV_MAKE_TYPE(CV_8U, this->settings_[i].cam_channels), convertedImage.GetData(), rowBytes);
915
916 // Scaled image
917 cv::Mat img_resized;
918
919 cv::resize(Img_BGR, img_resized, cv::Size((int)(Img_BGR.cols * this->settings_[i].image_scale), (int)(Img_BGR.rows * this->settings_[i].image_scale)), 0.0, 0.0, CV_INTER_CUBIC);
920
921 // Save the image. If a file format is not passed in, then the file
922 // extension is parsed to attempt to determine the file format.
923 if (!cv::imwrite(const_cast<char *>(imageName.c_str()), img_resized))
924 {
925 LOG_ERROR("Error to save the Flea image!");
926 ComponentManager::getInstance()->stop(name());
927 }
928 }
929
930 try
931 {
932 this->mDbtImage.writeRecord(time, tr, this->settings_[i].mOutputDirectory.filePath(imageName.c_str()).toStdString().c_str(), kMaxFilepathLength);
933 }
934 catch (DbiteException & e)
935 {
936 cerr << "error opening dbt file: " << e.what() << endl;
937 }
938 //--------------------------------------------------------------------------------------------------------------------------------------------
939 }
940 }*/
941
942 //LOG_TRACE("image no. " << imageCount << " acquired successfully");
943 //++imageCount;
944 }
945 }
946
947 for (int i = 0; i < this->nbrCamera_; i++)
948 {
949 //====================================== Camera finalization =================================================
950 LOG_INFO("Finished grabbing images!");
951 // Stop capturing images
952 error = cams[i]->StopCapture();
953 if (error != FlyCapture2::PGRERROR_OK)
954 {
955 LOG_ERROR(error.GetDescription());
956 ComponentManager::getInstance()->stop(name());
957 }
958
959 // Disconnect the camera
960 error = cams[i]->Disconnect();
961 if (error != FlyCapture2::PGRERROR_OK)
962 {
963 LOG_ERROR(error.GetDescription());
964 ComponentManager::getInstance()->stop(name());
965 }
966 //============================================================================================================
967
968 if (this->shmem_images_[i])
969 delete shmem_images_[i];
970
971 this->shmem_images_[i] = NULL;
972
973 this->is_running = false;
974
975 // Destroy the window frame
976 if (this->settings_[i].showdebug)
977 cvDestroyAllWindows();
978 }
979}
Note: See TracBrowser for help on using the repository browser.