Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ShowCamWxThreads.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifdef WIN32
26 #include <Base\Common\W32Compat.hh>
27 #endif
28 #include "ShowCamWxThreads.hh"
29 #include <Filter/Rescale.hh>
30 #include <Base/Common/FileHandling.hh>
31 #include <wx/wx.h>
32 
33 #include <Base/Common/W32Compat.hh>
34 #ifdef BIAS_HAVE_DCAM
35 #include <VideoSource/VideoSource_DCAM_BumbleBee.hh>
36 #endif
37 #ifdef BIAS_HAVE_SWISSRANGER
38 #include <VideoSource/VideoSource_SwissRanger.hh>
39 #endif
40 #include <VideoSource/VideoSource_Kinect_Base.hh>
41 #ifdef BIAS_HAVE_PMD_PMDTec
42 #include <VideoSource/VideoSource_PMD.hh>
43 #endif
44 #ifdef BIAS_HAVE_PMD_PMDZess
45 #include <VideoSource/VideoSource_PMDZess.hh>
46 #endif
47 #ifdef BIAS_HAVE_UEYE
48 #include <VideoSource/VideoSource_uEye.hh>
49 #endif
50 #ifdef BIAS_HAVE_DSHOW
51 #include <VideoSource/VideoSource_DSHOW.hh>
52 #endif
53 #ifdef BIAS_HAVE_KINECT2
54 #include <VideoSource/VideoSource_Kinect2.hh>
55 #endif
56 #ifdef BIAS_HAVE_V4L
57 #include <VideoSource/VideoSource_V4L.hh>
58 #endif
59 
60 using namespace BIAS;
61 using namespace std;
62 
63 
64 #define BUFFER_CAPACITY_MIN 0.8
65 #define BUFFER_GRAB_SLEEP 500
66 #define BUFFER_SAVE_SLEEP 2500
67 
68 /********************* ImageSaveThread **********************************
69  **************************************************************************/
70 
72 ImageSaveThread(std::string cameraName,
73  const int& downsample,
74  wxMutex* BufferAccessMutex,
75  bool& writeToBuffer,
76  unsigned int& readIndex,
77  unsigned int& writeIndex,
79  BIAS::Camera<unsigned char>* imgCenter,
81  BIAS::Camera<float>* imgDepth,
82  BIAS::Camera<float>* imgAmp,
83  BIAS::Camera<float>* imgInt,
84  std::vector<Camera<unsigned char>* >* imageBuffer,
85  std::vector<Camera<unsigned char>* >* imageBufferCenter,
86  std::vector<Camera<unsigned char>* >* imageBufferRight,
87  std::vector<Camera<float>* >* imageBufferPmdDepth,
88  std::vector<Camera<float>* >* imageBufferPmdAmp,
89  std::vector<Camera<float>* >* imageBufferPmdInt,
90  bool saveToStream)
91  : wxThread(wxTHREAD_DETACHED)
92 {
93 
94  cameraName_ = cameraName;
95  DownSample_ = downsample;
96  BufferAccess_ = BufferAccessMutex;
97 
98  //indicates whether we are writing to th ebuffer or not
99  WriteToBuffer_= &writeToBuffer;
100 
101  //indexs in buffers
102  ReadIndex_ = &readIndex;
103  WriteIndex_ = &writeIndex;
104 
105  Img_ = img;
106  ImgCenter_ = imgCenter;
107  ImgRight_ = imgRight;
108  ImgDepth_ = imgDepth;
109  ImgAmp_ = imgAmp;
110  ImgInt_ = imgInt;
111 
112  ImageBuffer_ = imageBuffer;
113  ImageBufferCenter_ = imageBufferCenter;
114  ImageBufferRight_ = imageBufferRight;
115 
116  ImageBufferPMDDepth_ = imageBufferPmdDepth;
117  ImageBufferPMDAmp_ = imageBufferPmdAmp;
118  ImageBufferPMDInt_ = imageBufferPmdInt;
119 
120  ImageCounter_ = 0;
121  saveDirectory_ = "";
122  stream_ = saveToStream;
123  if (stream_) {
124  for (unsigned int i=0;i<10;i++) {
125  ips_.push_back(new ImagePackage());
126  bStreamOpen_.push_back(false);
127  }
128  }
129 }
130 
131 
132 /**
133  @brief
134  @author ischiller
135 */
136 void* ImageSaveThread::
137 Entry() {
138  while(!TestDestroy()){
139  if(*WriteToBuffer_){
140  // save images from buffer
141  BufferAccess_->Lock();
142  //cout<<"l"<<flush;
143  if((*WriteIndex_ != ((*ReadIndex_ + 1) % ImageBuffer_->size()))){
144  //set new readindex
145  *ReadIndex_ = (*ReadIndex_ + 1) % ImageBuffer_->size();
146  //cout<<"u"<<flush;
147  BufferAccess_->Unlock();
148 
149  if((*ImageBuffer_)[*ReadIndex_] != NULL)
150  SaveImage_((*ImageBuffer_)[*ReadIndex_],"");
151  if((*ImageBufferCenter_)[*ReadIndex_] != NULL)
152  SaveImage_((*ImageBufferCenter_)[*ReadIndex_],"Center");
153  if((*ImageBufferRight_)[*ReadIndex_] != NULL)
154  SaveImage_((*ImageBufferRight_)[*ReadIndex_],"Right");
155  if((*ImageBufferPMDDepth_)[*ReadIndex_] != NULL)
156  SaveImage_((*ImageBufferPMDDepth_)[*ReadIndex_],"Depth");
157  if((*ImageBufferPMDAmp_)[*ReadIndex_] != NULL)
158  SaveImage_((*ImageBufferPMDAmp_)[*ReadIndex_],"Amp");
159  if((*ImageBufferPMDInt_)[*ReadIndex_] != NULL)
160  SaveImage_((*ImageBufferPMDInt_)[*ReadIndex_],"Int");
161 
162  ImageCounter_++;
163  }
164  else{
165  // wait for images in buffer
166  //cout<<"sleepSAVE "<<flush;
167  BufferAccess_->Unlock();
168 #ifndef WIN32
169  usleep(BUFFER_SAVE_SLEEP);
170 #else
171  Sleep(BUFFER_SAVE_SLEEP/1000);
172 #endif
173  }
174  }else{
175  // save single image
176  if(Img_ != NULL)
177  SaveImage_(Img_,"");
178  if(ImgCenter_ != NULL)
179  SaveImage_(ImgCenter_,"Center");
180  if(ImgRight_ != NULL)
181  SaveImage_(ImgRight_,"Right");
182  if(ImgDepth_ != NULL)
183  SaveImage_(ImgDepth_,"Depth");
184  if(ImgAmp_ != NULL)
185  SaveImage_(ImgAmp_,"Amp");
186  if(ImgInt_ != NULL)
187  SaveImage_(ImgInt_,"Int");
188 
189  ImageCounter_++;
190  }
191  }
192  FinishSaving();
193  return NULL;
194 }
195 
196 /**
197  @brief
198  @author ischiller
199 */
201  BufferAccess_->Lock();
202  //cout<<"f"<<flush;
203  while((*WriteIndex_ != ((*ReadIndex_ + 1) % ImageBuffer_->size()))){
204  cout<<"finishing:"<<endl<<flush;
205  *ReadIndex_ = (*ReadIndex_ + 1) % ImageBuffer_->size();
206 
207  if((*ImageBuffer_)[*ReadIndex_] != NULL)
208  SaveImage_((*ImageBuffer_)[*ReadIndex_],"");
209  if((*ImageBufferCenter_)[*ReadIndex_] != NULL)
210  SaveImage_((*ImageBufferCenter_)[*ReadIndex_],"Center");
211  if((*ImageBufferRight_)[*ReadIndex_] != NULL)
212  SaveImage_((*ImageBufferRight_)[*ReadIndex_],"Right");
213  if((*ImageBufferPMDDepth_)[*ReadIndex_] != NULL)
214  SaveImage_((*ImageBufferPMDDepth_)[*ReadIndex_],"Depth");
215  if((*ImageBufferPMDAmp_)[*ReadIndex_] != NULL)
216  SaveImage_((*ImageBufferPMDAmp_)[*ReadIndex_],"Amp");
217  if((*ImageBufferPMDInt_)[*ReadIndex_] != NULL)
218  SaveImage_((*ImageBufferPMDAmp_)[*ReadIndex_],"Int");
219 
220  ImageCounter_++;
221  }
222  //cout<<"k"<<flush;
223  BufferAccess_->Unlock();
224  if (stream_) {
225  for (unsigned int i=0; i<10;i++) {
226  if (bStreamOpen_[i]) {
227  bStreamOpen_[i]=false;
228  ips_[i]->Close();
229  }
230  }
231  }
232 }
233 
234 /**
235  @brief
236  @author ischiller
237 */
238 void ImageSaveThread::
239 SaveImage_(Camera <unsigned char>* im, string name) {
240  stringstream text;
241  text<<"-"<<FileHandling::LeadingZeroString(ImageCounter_,5)<<".mip";
242  string FilenameOut = cameraName_ + name + text.str();
243  //cout<<"Write:"<<FilenameOut<<endl;
244  int idx = -1;
245  if (stream_) {
246  idx = mapNameToStreamIdx_(name);
247  if (idx!=-1) {
248  if (!bStreamOpen_[idx]) {
249  string fname = saveDirectory_ + cameraName_ + name;
250  ips_[idx]->Create(fname);
251  bStreamOpen_[idx] = true;
252  }
253  }
254  }
255  if(DownSample_ != 0){
256  Camera<unsigned char> rescaledImage;
258  rescale.Downsample(*im, rescaledImage,
259  (int)(im->GetWidth() / DownSample_),
260  (int)(im->GetHeight() / DownSample_));
261  rescaledImage.UpdateMetaData();
262 
263  if (stream_) {
264  if (ips_[idx]->AddImage(rescaledImage, FilenameOut)!=0) {
265  BIASERR("Failed saving image to stream");
266  }
267  } else {
268  //BIAS::ImageIO::Save(saveDirectory_ + FilenameOut, rescaledImage, true);
269  BIAS::ImageIO::Save(saveDirectory_ + FilenameOut,
270  rescaledImage,
272  true);
273  }
274 
275  }else{
276  im->UpdateMetaData();
277  if (stream_) {
278  if (ips_[idx]->AddImage(*im, FilenameOut)!=0) {
279  BIASERR("Failed saving image to stream");
280  }
281  } else {
282  //BIAS::ImageIO::Save(saveDirectory_ + FilenameOut, *im, true);
283  BIAS::ImageIO::Save(saveDirectory_ + FilenameOut,
285  true);
286  }
287  }
288 }
289 
290 /**
291  @brief
292  @author ischiller
293 */
294 void ImageSaveThread::
295 SaveImage_(Camera <float>* imF, string name){
296  stringstream text;
297  text<<"-"<<FileHandling::LeadingZeroString(ImageCounter_,5)<<".mip";
298  string FilenameOut = cameraName_ + name + text.str();
299  int idx = -1;
300  if (stream_) {
301  idx = mapNameToStreamIdx_(name);
302  if (idx!=-1) {
303  if (!bStreamOpen_[idx]) {
304  string fname = saveDirectory_ + cameraName_ + name;
305  ips_[idx]->Create(fname);
306  bStreamOpen_[idx] = true;
307  }
308  }
309  }
310  if(DownSample_ != 0){
311  Camera<float> rescaledImage;
312  Rescale<float, float> rescale;
313  rescale.Downsample(*imF, rescaledImage,
314  (int)(imF->GetWidth() / DownSample_),
315  (int)(imF->GetHeight() / DownSample_));
316  rescaledImage.UpdateMetaData();
317 
318  if (stream_) {
319  if (ips_[idx]->AddImage(rescaledImage, FilenameOut)!=0) {
320  BIASERR("Failed saving image to stream");
321  }
322  } else {
323  //BIAS::ImageIO::Save(saveDirectory_ + FilenameOut, rescaledImage, true);
324  BIAS::ImageIO::Save(saveDirectory_ + FilenameOut,
325  rescaledImage,
327  true);
328  }
329 
330  }else{
331  imF->UpdateMetaData();
332  if (stream_) {
333  if (ips_[idx]->AddImage(*imF, FilenameOut)!=0) {
334  BIASERR("Failed saving image to stream");
335  }
336  } else {
337  //BIAS::ImageIO::Save(saveDirectory_ + FilenameOut, *imF, true);
338  BIAS::ImageIO::Save(saveDirectory_ + FilenameOut,
339  *imF,
341  true);
342  }
343  }
344 }
345 
346 int ImageSaveThread::mapNameToStreamIdx_(string name) {
347  if (name.length() == 0)
348  return 0;
349  if (name.find("Center")!=string::npos)
350  return 1;
351  if (name.find("Right")!=string::npos)
352  return 2;
353  if (name.find("Amp")!=string::npos)
354  return 3;
355  if (name.find("Depth")!=string::npos)
356  return 4;
357  if (name.find("Int")!=string::npos)
358  return 5;
359  return -1;
360 }
361 
362 /************************* GrabSingleThread *******************************
363  **************************************************************************/
364 
367  wxCondition *ClientCondition,
368  wxCondition *ServerCondition,
369  wxMutex *mutexClientCondition,
370  wxMutex *mutexServerCondition,
371  unsigned int threadNumber,
372  wxMutex *BufferAccessMutex,
373  bool& writeToBuffer,
374  unsigned int& readIndex,
375  unsigned int& writeIndex,
376 
378  BIAS::Camera<unsigned char> *imgCenter,
379  BIAS::Camera<unsigned char> *imgRight,
380  BIAS::Camera<float> *imgDepth,
381  BIAS::Camera<float> *imgAmp,
382  BIAS::Camera<float> *imgInt,
384 
385  std::vector<Camera<unsigned char>* > *imageBuffer,
386  std::vector<Camera<unsigned char>* > *imageBufferCenter,
387  std::vector<Camera<unsigned char>* > *imageBufferRight,
388  std::vector<Camera<float>* >* imageBufferPMDDepth,
389  std::vector<Camera<float>* >* imageBufferPMDAmp,
390  std::vector<Camera<float>* >* imageBufferPMDInt)
391 
392  :wxThread(wxTHREAD_DETACHED)
393 {
394  //the images
395  Img_ = img;
396  ImgCenter_ = imgCenter;
397  ImgRight_ = imgRight;
398  ImgDepth_ = imgDepth;
399  ImgAmp_ = imgAmp;
400  ImgInt_ = imgInt;
401  AllImages_ = imgAll;
402  threadNumber_ = threadNumber;
403 
404  //mutex for conditional
405  mutexClientCondition_= mutexClientCondition;
406  mutexServerCondition_= mutexServerCondition;
407 
408  //the conditionals
409  ClientCondition_ = ClientCondition;
410  ServerCondition_ = ServerCondition;
411 
412  //mutex for threaded buffer access
413  BufferAccess_ = BufferAccessMutex;
414 
415  // The image buffers
416  ImageBuffer_ = imageBuffer;
417  ImageBufferCenter_ = imageBufferCenter;
418  ImageBufferRight_ = imageBufferRight;
419  ImageBufferPMDDepth_ = imageBufferPMDDepth;
420  ImageBufferPMDAmp_ = imageBufferPMDAmp;
421  ImageBufferPMDInt_ = imageBufferPMDInt;
422 
423  //shall we write in the buffer?
424  WriteToBuffer_ = &writeToBuffer;
425  //read an dwrite indexes
426  ReadIndex_ = &readIndex;
427  WriteIndex_ = &writeIndex;
428  //the bnuffersize
429  BufferSize_ = ImageBuffer_->size();
430 
431  //the camera
432  Cam_ = cam;
433 
434 }
435 
436 /**
437  @brief
438  @author ischiller
439 */
440 void* GrabSingleThread::
442 {
443 //cout << "initing grab thread" << endl;
444 #ifdef BIAS_HAVE_DCAM
445  VideoSource_DCAM_BumbleBee *xb3cam = NULL;
446  xb3cam = dynamic_cast<VideoSource_DCAM_BumbleBee*>(Cam_);
447  VideoSource_DCAM *dcam = NULL;
448  dcam = dynamic_cast<VideoSource_DCAM*>(Cam_);
449 #endif
450 #ifdef BIAS_HAVE_SWISSRANGER
451  VideoSource_SwissRanger *srcam = NULL;
452  srcam = dynamic_cast<VideoSource_SwissRanger*>(Cam_);
453 #endif
454 #ifdef BIAS_HAVE_UEYE
455  VideoSource_uEye *ueyecam = NULL;
456  ueyecam = dynamic_cast<VideoSource_uEye*>(Cam_);
457 #endif
458 #ifdef BIAS_HAVE_PMD_PMDTec
459  VideoSource_PMD *pmdcam = NULL;
460  pmdcam = dynamic_cast<VideoSource_PMD*>(Cam_);
461 #endif
462 #ifdef BIAS_HAVE_PMD_PMDZess
463 //zess
464  VideoSource_PMDZess *usbpmdcam = NULL;
465  usbpmdcam = dynamic_cast<VideoSource_PMDZess*>(Cam_);
466 #endif
467 #ifdef BIAS_HAVE_V4L
468  VideoSource_V4L * webcam=NULL;
469  webcam = dynamic_cast<VideoSource_V4L*>(Cam_);
470 #endif
471 #ifdef BIAS_HAVE_DSHOW
472  VideoSource_DSHOW * webcam=NULL;
473  webcam = dynamic_cast<VideoSource_DSHOW*>(Cam_);
474 #endif
475  VideoSource_Kinect_Base * kinect=NULL;
476  kinect = dynamic_cast<VideoSource_Kinect_Base*>(Cam_);
477 
478 #ifdef BIAS_HAVE_DCAM
479  //normal
480  if(dcam !=NULL){
481  camtype_ = DCAM;
482  }
483  //BumbleBee
484  if(xb3cam !=NULL){
485  camtype_ = XB3;
486  }
487 #endif
488 
489 #ifdef BIAS_HAVE_SWISSRANGER
490  if(srcam != NULL){
491  camtype_ = SR;
492  }
493 #endif
494 #ifdef BIAS_HAVE_UEYE
495  if(ueyecam !=NULL){
496  camtype_ = UEYE;
497  }
498 #endif
499 #ifdef BIAS_HAVE_PMD_PMDTec
500 //pmdtec
501  if(pmdcam !=NULL){
502  camtype_ = PMDTEC;
503  }
504 #endif
505 #ifdef BIAS_HAVE_PMD_PMDZess
506 //zess
507  if(usbpmdcam !=NULL){
508  camtype_ = ZESS;
509  }
510 #endif
511 #ifdef BIAS_HAVE_DSHOW
512  if (webcam != NULL) {
513  camtype_ = DSHOW;
514  }
515 #endif
516 #ifdef BIAS_HAVE_V4L
517  if (webcam != NULL) {
518  camtype_ = V4L;
519  }
520 #endif
521 #ifdef BIAS_HAVE_KINECT
522  if (kinect != NULL) {
523  camtype_ = KINECT;
524  }
525 #endif
526 
527 #ifdef BIAS_HAVE_OPENNI
528  if (kinect != NULL) {
529  camtype_ = KINECT;
530  }
531 #endif
532 
533 #ifdef BIAS_HAVE_KINECT2
534  VideoSource_Kinect2 *kinect2 = NULL;
535  kinect2 = dynamic_cast<VideoSource_Kinect2*>(Cam_);
536  if (kinect2 != NULL) {
537  camtype_ = FREENECT2;
538  }
539 #endif
540 
541  // lock this clients mutex
542  mutexClientCondition_->Lock();
543 
544  while(!TestDestroy()){
545  if(Cam_ != NULL){
546  //cout<<"Grabbing"<<endl;
547  if(*WriteToBuffer_){
548  double bufferPercent = 0.0;
549  if(*WriteIndex_ >= *ReadIndex_){
550  bufferPercent = (double)((*WriteIndex_ + 1) -
551  (*ReadIndex_ + 1) ) / (double)BufferSize_;
552  }else{
553  bufferPercent = (double)(((*WriteIndex_ + 1)+BufferSize_)-
554  (*ReadIndex_ + 1)) / (double)BufferSize_;
555  }
556 
557  while(*WriteToBuffer_ && bufferPercent > BUFFER_CAPACITY_MIN){
558 
559  if(*WriteIndex_ >= *ReadIndex_){
560  bufferPercent = (double)((*WriteIndex_ + 1) -
561  (*ReadIndex_ + 1) ) / (double)BufferSize_;
562  }else{
563  bufferPercent = (double)(((*WriteIndex_ + 1)+BufferSize_)-
564  (*ReadIndex_ + 1)) / (double)BufferSize_;
565  }
566  //std::cout << "Thread:"<<threadNumber_<<" Pufferfreiraum: " << bufferPercent << std::endl;
567 #ifndef WIN32
568  usleep(BUFFER_GRAB_SLEEP);
569 #else
570  Sleep(BUFFER_GRAB_SLEEP/1000);
571 #endif
572  }
573  }
574 
575  timeval t1,t2;
576  gettimeofday(&t1,NULL);
577 
578  switch (camtype_) {
579  #ifdef BIAS_HAVE_DCAM
580  case DCAM:
581  //cout << "grab dcam" << endl;
582  dcam->GrabSingle(*Img_);
583  break;
584  case XB3:
585  //cout << "grab xb3" << endl;
586  xb3cam->GrabSingle(*AllImages_,*Img_,*ImgCenter_,*ImgRight_);
587  break;
588  #endif
589  #ifdef BIAS_HAVE_SWISSRANGER
590  case SR:
591  srcam->GrabSingle(*Img_);
592  srcam->GrabSingleDepth(*ImgDepth_);
593  srcam->GrabSingleModCoeff(*ImgAmp_);
594  break;
595  #endif
596  case KINECT:
597  kinect->GrabSingle(*Img_);
598  kinect->GrabSingleDepth(*ImgDepth_);
599  break;
600  #ifdef BIAS_HAVE_UEYE
601  case UEYE:
602  ueyecam->GrabSingle(*Img_);
603  break;
604  #endif
605  #ifdef BIAS_HAVE_PMD_PMDTec
606  case PMDTEC:
607  pmdcam->GrabSingle(*Img_);
608  // cout << Img_->GetWidth() << " " <<Img_->GetHeight() << endl;
609  pmdcam->GrabSingleDepth(*ImgDepth_);
610  pmdcam->GrabSingleModCoeff(*ImgAmp_);
611  pmdcam->GrabSingleIntensity(*ImgInt_);
612  break;
613  #endif
614  #ifdef BIAS_HAVE_PMD_PMDZess
615  case ZESS:
616  usbpmdcam->GrabSingle(*Img_);
617  usbpmdcam->GrabSingleDepth(*ImgDepth_);
618  usbpmdcam->GrabSingleModCoeff(*ImgAmp_);
619  break;
620  #endif
621  #ifdef BIAS_HAVE_DSHOW
622  case DSHOW:
623  webcam->GrabSingle(*Img_);
624  break;
625  #endif
626  #ifdef BIAS_HAVE_V4L
627  case V4L:
628  webcam->GrabSingle(*Img_);
629  break;
630  #endif
631  #ifdef BIAS_HAVE_KINECT2
632  case FREENECT2:
633  kinect2->GrabSingle(*Img_);
634  kinect2->GrabSingleDepth(*ImgDepth_);
635  kinect2->GrabSingleIR(*ImgInt_);
636  break;
637  #endif
638 
639  case INVALID:
640  default:
641  BIASWARN("Camera type not supported.");
642  break;
643  }
644 
645  //obtain second time stamp
646  gettimeofday(&t2,NULL);
647  long duration = ((t2.tv_sec-t1.tv_sec)*1000000) + (t2.tv_usec-t1.tv_usec);
648  //long sec=t1.tv_sec + duration/1000000;
649  //long usec=t1.tv_usec + duration%1000000;
650  int sec=t1.tv_sec + duration/1000000;
651  int usec=t1.tv_usec + duration%1000000;
652  if (usec > 1000000) {
653  sec++;
654  usec -= 1000000;
655  }
656  //set time in image
657  Img_->SetTime(sec, usec);
658 
659  switch (camtype_) {
660  #ifdef BIAS_HAVE_DCAM
661  case XB3:
662  AllImages_->SetTime(sec, usec);
663  ImgCenter_->SetTime(sec, usec);
664  ImgRight_->SetTime(sec, usec);
665  break;
666  #endif
667  #ifdef BIAS_HAVE_SWISSRANGER
668  case SR:
669  ImgAmp_->SetTime(sec, usec);
670  case KINECT:
671  ImgDepth_->SetTime(sec, usec);
672  break;
673  #endif
674  #ifdef BIAS_HAVE_PMD_PMDTec
675  case PMDTEC:
676  ImgDepth_->SetTime(sec, usec);
677  ImgAmp_->SetTime(sec, usec);
678  ImgInt_->SetTime(sec, usec);
679  break;
680  #endif
681  #ifdef BIAS_HAVE_PMD_PMDZess
682  case ZESS:
683  ImgDepth_->SetTime(sec, usec);
684  ImgAmp_->SetTime(sec, usec);
685  break;
686  #endif
687  #ifdef BIAS_HAVE_KINECT2
688  case FREENECT2:
689  Img_->SetTime(sec, usec);
690  ImgDepth_->SetTime(sec, usec);
691  ImgInt_->SetTime(sec, usec);
692  break;
693  #endif
694  case INVALID:
695  default:
696  // nothing
697  break;
698  }
699  }
700  // write images to buffer
701  if(*WriteToBuffer_){
702  //cout<<"L"<<flush;
703  BufferAccess_->Lock();
704 
705  if((*WriteIndex_ + 1) % ImageBuffer_->size() != *ReadIndex_ ){
706  //cout<<"U"<<flush;
707  BufferAccess_->Unlock();
708  //std::cout << "Write Image to buffer at pos: " << *WriteIndex_ << std::endl;
709  if(Img_ != NULL){
710  if((*ImageBuffer_)[*WriteIndex_] == NULL)
711  (*ImageBuffer_)[*WriteIndex_] = new BIAS::Camera<unsigned char>;
712  *(*ImageBuffer_)[*WriteIndex_] = *Img_;
713  }
714  if(ImgCenter_ != NULL){
715  if((*ImageBufferCenter_)[*WriteIndex_] == NULL)
716  (*ImageBufferCenter_)[*WriteIndex_] = new BIAS::Camera<unsigned char>;
717  *(*ImageBufferCenter_)[*WriteIndex_] = *ImgCenter_;
718  }
719  if(ImgRight_ != NULL){
720  if((*ImageBufferRight_)[*WriteIndex_] == NULL)
721  (*ImageBufferRight_)[*WriteIndex_] = new BIAS::Camera<unsigned char>;
722  *(*ImageBufferRight_)[*WriteIndex_] = *ImgRight_;
723  }
724  if(ImgDepth_ != NULL){
725  if((*ImageBufferPMDDepth_)[*WriteIndex_] == NULL)
726  (*ImageBufferPMDDepth_)[*WriteIndex_] = new BIAS::Camera<float>;
727  *(*ImageBufferPMDDepth_)[*WriteIndex_] = *ImgDepth_;
728  }
729  if(ImgAmp_ != NULL){
730  if((*ImageBufferPMDAmp_)[*WriteIndex_] == NULL)
731  (*ImageBufferPMDAmp_)[*WriteIndex_] = new BIAS::Camera<float>;
732  *(*ImageBufferPMDAmp_)[*WriteIndex_] = *ImgAmp_;
733  }
734  if(ImgInt_ != NULL){
735  if((*ImageBufferPMDInt_)[*WriteIndex_] == NULL)
736  (*ImageBufferPMDInt_)[*WriteIndex_] = new BIAS::Camera<float>;
737  *(*ImageBufferPMDInt_)[*WriteIndex_] = *ImgInt_;
738  }
739 
740  //cout<<"L"<<flush;
741  BufferAccess_->Lock();
742  *WriteIndex_ = (*WriteIndex_ + 1) % ImageBuffer_->size();
743  //cout<<"U"<<flush;
744  BufferAccess_->Unlock();
745  } else {
746  //cout<<"U"<<flush;
747  BufferAccess_->Unlock();
748  /*std::cout << "Puffer schreiben ueberholt den auslesenden Thread, Frames gehen verloren.\n"
749  << "Thread: " << this->GetId() << " wird kurz schlafen gelegt."
750  << std::endl;*/
751  //cout<<"Thread:"<<threadNumber_<<" overtaking!"<<endl;
752 #ifndef WIN32
753  usleep(BUFFER_GRAB_SLEEP);
754 #else
755  Sleep(BUFFER_GRAB_SLEEP/1000);
756 #endif
757  }
758 
759  }
760 
761 #ifdef BIAS_DEBUG
762  //cout <<threadNumber_;
763 #endif
764  //i am done
765  //lock server mutex here
766  mutexServerCondition_->Lock();
767  ServerCondition_->Signal();
768  mutexServerCondition_->Unlock();
769  ClientCondition_->Wait(); //calls unlock(mutex), waits for signal(condition), calls lock(mutex)
770  }
771  // on exit, unlock client condition in case grab thread is recreated
772  mutexClientCondition_->Unlock();
773  return NULL;
774 }
775 
776 
777 /************************* GrabMasterThread *******************************
778  **************************************************************************/
779 
781 GrabMasterThread(std::vector<BIAS::VideoSource*> *Cameras,
782  std::vector<wxCondition*> *ClientCondition,
783  std::vector<wxCondition*> *ServerCondition,
784  std::vector<wxMutex*> *mutexClientCondition,
785  std::vector<wxMutex*> *mutexServerCondition)
786 
787  :wxThread(wxTHREAD_DETACHED)
788 {
789 
790  //mutex for conditional
791  mutexClientCondition_= mutexClientCondition;
792  mutexServerCondition_= mutexServerCondition;
793 
794  //the conditionals
795  ClientCondition_ = ClientCondition;
796  ServerCondition_ = ServerCondition;
797 
798  Cameras_ = Cameras;
799 
800  FrameCounter_ = 0;
801  StopWatch_.Start();
802 
803 
804 }
805 
806 /**
807  @brief
808  @author fkellner
809 */
810 void* GrabMasterThread::
812 {
813  // lock all server conditions
814  for ( unsigned int i = 0; i < ServerCondition_->size(); i++)
815  (*mutexServerCondition_)[i]->Lock();
816 
817  while(!TestDestroy()){
818  StopWatch_.Stop();
819  fps_ = 1000000.0 / StopWatch_.GetRealTime();
820  StopWatch_.Reset();
821  StopWatch_.Start();
822  FrameCounter_++;
823  //cout << "real fps " << fps_ << " number: " << FrameCounter_ << endl;
824 
825 
826 #ifdef BIAS_DEBUG
827  //cout << "*" <<flush;
828 #endif
829 
830  //send signal to grabthreads to continue
831  for ( unsigned int i = 0; i < ServerCondition_->size(); i++) {
832  if ((*Cameras_)[i] != NULL) {
833  //wait for serverCondition to be signaled from each active camera (unlock, wait, lock)
834  (*ServerCondition_)[i]->Wait();
835  }
836  }
837 #ifdef BIAS_DEBUG
838  //cout << "+" <<flush;
839 #endif
840 
841  // set flags to false for all active cameras
842  for ( unsigned int i = 0; i < ServerCondition_->size(); i++) {
843  //tell waiting grab threads to continue
844  if ((*Cameras_)[i] != NULL) {
845  (*mutexClientCondition_)[i]->Lock();
846  (*ClientCondition_)[i]->Signal();
847  (*mutexClientCondition_)[i]->Unlock();
848  }
849  }
850 
851  }
852  return NULL;
853 }
GrabSingleThread(BIAS::VideoSource *cam, wxCondition *ClientCondition, wxCondition *ServerCondition, wxMutex *mutexClientCondition, wxMutex *mutexServerCondition, unsigned int threadNumber, wxMutex *BufferAccessMutex, bool &writeToBuffer, unsigned int &readIndex, unsigned int &writeIndex, BIAS::Camera< unsigned char > *img, BIAS::Camera< unsigned char > *imgCenter, BIAS::Camera< unsigned char > *imgRight, BIAS::Camera< float > *imgDepth, BIAS::Camera< float > *imgAmp, BIAS::Camera< float > *imgInt, BIAS::Camera< unsigned char > *imgAll, std::vector< Camera< unsigned char > * > *imageBuffer, std::vector< Camera< unsigned char > * > *imageBufferCenter, std::vector< Camera< unsigned char > * > *imageBufferRight, std::vector< Camera< float > * > *imageBufferPMDDepth, std::vector< Camera< float > * > *imageBufferPMDAmp, std::vector< Camera< float > * > *imageBufferPMDInt)
int GrabSingle(BIAS::Camera< unsigned char > &image)
virtual int GrabSingleDepth(BIAS::Camera< float > &image)
grab the depth image (slow).
represents Zess PMD camera driver interface TODO: include support for normal images (2DImg_)...
Defines a common interface to different devices.
int GrabSingleModCoeff(Camera< float > &image)
Returns the amplitude image.
Use the Kinect2 class to grab images from the Microsoft Kinect2.
int GrabSingle(BIAS::Camera< unsigned char > &image)
Select the port (bus-id) to use, only valid before OpenDevice()
virtual int GrabSingle(BIAS::Camera< unsigned char > &allImages, BIAS::Camera< unsigned char > &imageLeft, BIAS::Camera< unsigned char > &imageMiddle, BIAS::Camera< unsigned char > &imageRight)
Grab single images.
virtual int GrabSingleIR(BIAS::Camera< float > &image)
grab the raw ir data dump (very fast).
virtual int GrabSingle(BIAS::Camera< unsigned char > &image)=0
grab single color image
virtual int GrabSingle(BIAS::Camera< unsigned char > &image)
grab single color image. Note: This image has already been jpeg compressed
Support for SwissRanger usb cam.
This class VideoSource_DCAM implements access to IEEE1394 (Firewire, iLink) cameras following the DCa...
int Downsample(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
generic downsample function.
Definition: Rescale.cpp:111
unsigned int GetWidth() const
Definition: ImageBase.hh:312
virtual int GrabSingle(Camera< unsigned char > &image)
Returns the 2D image, and reads depth and modulation coefficients.
int GrabSingleModCoeff(Camera< float > &image)
Returns the amplitude image.
This class extends VideoSource for the use of DirectShow devices you need Microsoft Windows SDK versi...
virtual int GrabSingleDepth(BIAS::Camera< float > &image)=0
depth according to formula by Stephane Magnenat, see http://openkinect.org/wiki/Imaging_Information, todo: needs validation!
Support for CamCube usb cam.
int GrabSingleDepth(Camera< float > &image)
Returns the depth image.
static std::string LeadingZeroString(const int &n, const unsigned int &digits=DEFAULT_LEADING_ZEROS)
Create a string with leading zeroes from number.
unsigned int GetHeight() const
Definition: ImageBase.hh:319
An Image Package is multiple .mip images in one file.
Definition: ImagePackage.hh:20
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
int UpdateMetaData()
copy P_ and co.
Definition: Camera.cpp:446
int GrabSingleIntensity(Camera< float > &image)
Returns the amplitude image.
virtual ExitCode Entry()
int GrabSingleDepth(Camera< float > &image)
Returns the depth image.
virtual int GrabSingle(Camera< unsigned char > &image)
Returns the 2D image, and reads depth and modulation coefficients.
GrabMasterThread(std::vector< BIAS::VideoSource * > *Cameras, std::vector< wxCondition * > *ClientCondition, std::vector< wxCondition * > *ServerCondition, std::vector< wxMutex * > *mutexClientCondition, std::vector< wxMutex * > *mutexServerCondition)
void SetTime(unsigned long long int sec, unsigned long long int usec)
Set time and ensure correct format (seconds and useconds after &#39;sec&#39;)
Definition: Camera.hh:124
This class extends VideoSource for the use of IDS uEye devices.
int GrabSingle(BIAS::Camera< unsigned char > &image)
This class extends VideoSource for the use of Video4Linux supported devices like framegrabber, USB cameras.
virtual int GrabSingle(Camera< unsigned char > &image)
Returns the 2D image, and reads depth and modulation coefficients.
int GrabSingleDepth(Camera< float > &image)
Returns the depth image.
Use Kinect just like the ToF Cameras.
int GrabSingleModCoeff(Camera< float > &image)
Returns the amplitude image.
ImageSaveThread(std::string cameraName, const int &downsample, wxMutex *BufferAccessMutex, bool &writeToBuffer, unsigned int &readIndex, unsigned int &writeIndex, BIAS::Camera< unsigned char > *img, BIAS::Camera< unsigned char > *imgCenter, BIAS::Camera< unsigned char > *imgRight, BIAS::Camera< float > *imgDepth, BIAS::Camera< float > *imgAmp, BIAS::Camera< float > *imgInt, std::vector< Camera< unsigned char > * > *imageBuffer, std::vector< Camera< unsigned char > * > *imageBufferCenter, std::vector< Camera< unsigned char > * > *imageBufferRight, std::vector< Camera< float > * > *imageBufferPmdDepth, std::vector< Camera< float > * > *imageBufferPmdAmp, std::vector< Camera< float > * > *imageBufferPmdInt, bool saveToStream=false)