Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_DiskPMD.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 #ifndef WIN32
25 #include <unistd.h>
26 #else
27 #include <Base/Common/W32Compat.hh>
28 #endif //WIN32
29 
30 #include <string>
31 #include <sstream>
32 #include <iostream>
33 
34 #include "VideoSource_DiskPMD.hh"
35 #include <Base/Image/ImageIO.hh>
36 #include <Image/Camera.hh>
37 
38 #include <Base/Common/BIASpragma.hh>
39 #include <Base/Common/FileHandling.hh>
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 
47 {
48  GenerateNewUIDs_=true;
49 }
50 
53 {
54 }
55 
58 {
59  BIASERR("use OpenDevice(const vector<string> &FileNames)");
60  BIASABORT;
61  return -1;
62 }
63 
64 #ifdef BIAS_HAVE_XML2
65 
67 {
68  vecImg2D_.clear();
69  vecImgDepth_.clear();
70  vecImgModCoeff_.clear();
71  vecMetaData2D_.clear();
72  vecMetaDataDepth_.clear();
73  FileNames_.clear();
74 
75  // open xml file with image sequence
76  BIASDOUT(D_VSO_DISK,"FileName : "<<FileName);
77 
78  if (PMDImageIO::LoadFromXML(FileName, vecImg2D_, vecImgDepth_, vecImgModCoeff_,
79  vecMetaData2D_, vecMetaDataDepth_)!=0)
80  {
81  BIASERR("unable to read "<<FileName);
82  return -1;
83  }
84  if (vecImg2D_.size()>0) {
85  Width_ = vecMetaData2D_[0].width;
86  Height_ = vecMetaData2D_[0].height;
87  ColorChannels_ = vecMetaData2D_[0].channels;
88  ColorMode_ = vecImg2D_[0].GetColorModel();
89  BytesPerPixel_ = vecImg2D_[0].GetDepth();
90  } else {
91  BIASERR("No frames in file "<<FileName);
92  return -1;
93  }
94 
95  for (unsigned int i=0; i<vecMetaData2D_.size(); i++) {
96  stringstream ss;
97  ss << FileHandling::Basename(FileName) << i << FileHandling::Suffix(FileName);
98  FileNames_.push_back(ss.str().c_str());
99  }
100 
101  CompleteInitialized_ = true;
102  ActiveFrame_ = 0;
103 
104  return 0;
105 }
106 #endif
107 
108 int BIAS::VideoSource_DiskPMD::OpenDevice(const vector<string> &FileNames)
109 {
110 
111  // load first image and initialize some internal variables
112  Image<unsigned char> Img2D;
113  Image<float> ImgDepth, ImgModCoeff;
114  PMDImageMetaData MetaData2D, MetaDataDepth;
115  BIASDOUT(D_VSO_DISK,"FileNames[0] : "<<FileNames[0]);
116 
117  if (PMDImageIO::Load(FileNames[0],
118  Img2D, ImgDepth, ImgModCoeff,
119  MetaData2D, MetaDataDepth)!=0) {
120  BIASERR("unable to read "<<FileNames[0]);
121  return -1;
122  }
123 
124  Width_ = MetaData2D.width;
125  Height_ = MetaData2D.height;
126  ColorChannels_ = MetaData2D.channels;
127  ColorMode_ = Img2D.GetColorModel();
128  BytesPerPixel_ = Img2D.GetDepth();
129 
130  FileNames_.clear();
131  for (vector<string>::const_iterator i = FileNames.begin();
132  i != FileNames.end();i++){
133  FileNames_.push_back(*i);
134  }
135 
136  CompleteInitialized_ = true;
137  ActiveFrame_ = 0;
138 
139  return 0;
140 }
141 
142 
144 {
145  BIASDOUT(D_VSO_DISK,"VideoSource_DiskPMD::GrabSingle()");
146 
147  if (!Active_) {
148  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object not active, use PreGrab()");
149  return -1;
150  }
151 
152  if (Grabbing_) {
153  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object is already grabbing()");
154  return -1;
155  }
156 
157  // Add Metadata to the Camera via Projection Object
158  PMDImageMetaData MetaData2D, MetaDataDepth;
159  if (vecImg2D_.size()==0) {
160  if (ActiveFrame_ == FileNames_.size()){
161  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
162  return -1;
163  }
164 
165  if (PMDImageIO::Load(FileNames_[ActiveFrame_], image, DepthImg_, ModCoeffImg_,
166  MetaData2D, MetaDataDepth)<0) {
167  BIASERR("VideoSource_Disk::GrabSingle(): Unable to read image: "
168  << FileNames_[ActiveFrame_]);
169  return -1;
170  }
171  } else {
172  if (ActiveFrame_ == vecImg2D_.size()){
173  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
174  return -1;
175  }
176 
177  image = Camera<unsigned char>(vecImg2D_[ActiveFrame_]);
178  DepthImg_ = Camera<float>(vecImgDepth_[ActiveFrame_]);
179  ModCoeffImg_ = Camera<float>(vecImgModCoeff_[ActiveFrame_]);
180  MetaData2D = vecMetaData2D_[ActiveFrame_];
181  MetaDataDepth = vecMetaDataDepth_[ActiveFrame_];
182 
183  }
184  Grabbing_ = true;
185 
186  KMatrix K2D = PMDImageProc::KFromMeta(MetaData2D);
187  KMatrix Kdepth = PMDImageProc::KFromMeta(MetaDataDepth);
188 
189  Pose Pose2D = PMDImageProc::PoseFromMeta(MetaData2D);
190  Pose PoseDepth = PMDImageProc::PoseFromMeta(MetaDataDepth);
191 
192  Projection Proj2D, ProjDepth;
193 
194  vector<double> UndistCoeff2D(4,0.0);
195  UndistCoeff2D[0] =MetaData2D.UndistCoeff[0];
196  UndistCoeff2D[1] =MetaData2D.UndistCoeff[1];
197  UndistCoeff2D[2] =MetaData2D.UndistCoeff[2];
198  UndistCoeff2D[3] =MetaData2D.UndistCoeff[3];
200  vector<double> UndistCoeffDepth(4,0.0);
201  UndistCoeffDepth[0] =MetaDataDepth.UndistCoeff[0];
202  UndistCoeffDepth[1] =MetaDataDepth.UndistCoeff[1];
203  UndistCoeffDepth[2] =MetaDataDepth.UndistCoeff[2];
204  UndistCoeffDepth[3] =MetaDataDepth.UndistCoeff[3];
205 
206 
207  Proj2D.CreatePerspective(Pose2D, K2D,
208  MetaData2D.width, MetaData2D.height,
209  radDistType,UndistCoeff2D);
210  ProjDepth.CreatePerspective(PoseDepth, Kdepth,
211  MetaDataDepth.width, MetaDataDepth.height,
212  radDistType,UndistCoeffDepth);
213  image.SetProj(Proj2D);
214  image.UpdateMetaData();
216  DepthImg_.SetProj(ProjDepth);
217  DepthImg_.UpdateMetaData();
218  DepthImg_.SetUID(image.GetUID());
219  ModCoeffImg_.SetProj(ProjDepth);
220  ModCoeffImg_.UpdateMetaData();
221  ModCoeffImg_.SetUID(image.GetUID());
222 
223  // set time to _now_
224  if (FrameCounterAsTimeStamp_) image.SetTime(ActiveFrame_,0);
225  else {
226  timeval tv;
227  gettimeofday(&tv, NULL);
228  image.SetTime(tv.tv_sec,tv.tv_usec);
229  }
230  if(ActiveFrame_<FileNames_.size()-1){
231  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
232  FileNames_[ActiveFrame_]);
233  ActiveFrame_++;
234  }
235  else if (Loop_) {
236  ActiveFrame_=0;
237  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
238  }
239  else ActiveFrame_++;
240 
241  Grabbing_ = false;
242  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
243  // check time and sleep while enough time elapsed
244  double secPerFrame=1.0/FramesPerSecond_;
245 
246  //struct timeval now; -- displaced by TimeMeasure
247  //double secs=0.0; -- -"-
248  //while (secs<=secPerFrame) { -- -"-
249  if(KeepFPS_) {
250  timer_.Stop();
251  while (timer_.GetRealTime()<=(1E06*secPerFrame)) {
252  timer_.Start();
253 #ifdef WIN32
254  Sleep(1); //go sleeping for 1 msec
255 #else //not WIN32
256  usleep(1000); //go sleeping for 1 msec
257 #endif //WIN32
258  timer_.Stop();
259 
260  }
261  timer_.Reset();
262  timer_.Start();
263  }
264  //lastTime_=now; -- displaced by TimeMeasure
265 
266  return 0;
267 }
268 
static int LoadFromXML(const std::string &FileName, Image< unsigned char > &Img2D, Image< float > &ImgDepth, Image< float > &ImgModCoeff, PMDImageMetaData &MetaData2D, PMDImageMetaData &MetaDataDepth)
Load a PMD-data 2d/3d-image from PMDtec&#39;s XMK format.
Definition: PMDImageIO.cpp:849
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
Definition: ImageBase.hh:328
virtual int GrabSingle(Camera< unsigned char > &image)
Returns the 2D image, and reads depth and modulation coefficients.
virtual int OpenDevice()
selects the first available device to open (e.g.
const BIAS::UUID & GetUID() const
returns the UUID of the image
Definition: ImageBase.hh:449
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
static std::string Basename(const std::string &fullname)
Get file base name without path from given path and filename.
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
static int Load(const std::string &FileName, uint8 *&Img2D, uint16 *&ImgDepth, uint16 *&ImgModCoeff, PMDImageMetaData &MetaData2D, PMDImageMetaData &MetaDataDepth)
Load a PMD-data 2d/3d-tiff image from disk.
Definition: PMDImageIO.cpp:570
int SetProj(const Projection &Proj)
Definition: Camera.hh:106
unsigned int channels
Definition: PMDImageIO.hh:73
unsigned int width
Definition: PMDImageIO.hh:71
unsigned int height
Definition: PMDImageIO.hh:72
Store extrinsic and intrinsic camera parameters for Rotation is in axis * angle representation image ...
Definition: PMDImageIO.hh:70
void SetUID(const BIAS::UUID &id)
Definition: ImageBase.hh:589
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
int UpdateMetaData()
copy P_ and co.
Definition: Camera.cpp:446
static std::string Suffix(const std::string &fullname)
Get file suffix (including dot!) from given path and filename.
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
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
void CreatePerspective(const BIAS::Pose &pose, const BIAS::KMatrix &K, int width, int height, BIAS_ProjParaPersp_DISTORTION_TYPE radDistType=DISTYPE_DEF, const std::vector< double > &UndistortionCoefficients=std::vector< double >(4, 0.0))
Create a perspective camera and add to projection.
Definition: Projection.cpp:133
static Pose PoseFromMeta(const PMDImageMetaData &MetaData)
static KMatrix KFromMeta(const PMDImageMetaData &MetaData)
static UUID GenerateUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
static function which simply produces a uuid and returns
Definition: UUID.cpp:235
This class simulates a video device by loading images from disk.