Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
showdv2cam.cpp
1 #include <Image/Camera.hh>
2 #include <Base/Image/ImageConvert.hh>
3 #include <Base/Image/ImageIO.hh>
4 #include <VideoSource/VideoSource_DV2.hh>
5 #include <Gui/GuiCV.hh>
6 #include <unistd.h>
7 #include <getopt.h>
8 
9 using namespace BIAS;
10 using namespace std;
11 
12 #define StorageType unsigned char
13 
14 #include <Base/Debug/TimeMeasure.hh>
15 
16 void mainloop(VideoSource_DV2 *cam)
17 {
18  GuiCV CamWin;
19  bool done = false;
20  char c;
21  char title[100];
22 
23  struct timeval now;
24  float fps;
25  int i,savedframes, savecounter=0;
26 
27  long start=0, end;
28  bool color, pause, autosave=false;
29  ostringstream os;
30 
31  TimeMeasure t1,t2;
32 
33  Camera<unsigned char> pic,pic2;
34 
35  sprintf(title,"MyWindow");
36  CamWin.SetTitle(title);
37  cam->InitImage(pic);
38  pic2.Init(720, pic.GetHeight(),3);
39 
40  pause = false;
41  color = true;
42 
43  i = 1;
44  savedframes = 0;
45 
46 
47  bool save;
48  //pic2.SetDebugLevel(D_CONVERT);
49 
50  while (!done){
51  save = false;
52  gettimeofday(&now, NULL);
53  end = start;
54  start = (now.tv_sec % 1000) * 1000 + (now.tv_usec / 1000);
55  fps = 1.0 / (float)(start-end) * 1000.0 ;
56 
57  c = CamWin.CheckForKeyEvent();
58  switch (c) {
59  case 'q':
60  done = true;
61  break;
62  case 's':
63  save = true;
64  break;
65  case 'S':
66  autosave=!autosave;
67  savecounter=0;
68  if (autosave) cerr << "\nstarted saving sequence at frame "<<i<<endl;
69  else cerr << "\nstopped saving sequence at frame "<<i<<endl;
70  break;
71  case ' ':
72  pause = !pause;
73  break;
74  case 'c':
75  color = !color;
76  if (color) cerr << "\nshowing color image" << endl;
77  else cerr << "\nshowing grey image" << endl;
78  break;
79  default:
80  if (c!=-1) cerr <<"unknown key "<<c<<endl;
81  break;
82  }
83 
84 
85  if (!pause) {
86  // cout <<"starting grab"<<endl;
87  cam->GrabSingle(pic);
88  // cout <<"got image"<<endl;
89 
90  ImageConvert::ToRGB(pic, pic2);
91 
92  //if (i==10)
93  // pic2.ExportImage("Cam.pnm",MIP_pnm);
94  if (save) {
95  //ImageIO::Save("image",pic2);
96  ImageIO::Save("image",pic2);
97  // ImageIO::ExportImage("image.pnm",pic2,ImageIO::FF_ppm);
98  save = false;
99  }
100  if (autosave){
101  os.str("");
102  os << "frame" << setfill('0') << setw(5) << savecounter;
103  if (ImageIO::ExportImage(os.str(),pic2, ImageIO::FF_pgm)!=0){
104  BIASERR("error saving frame "<<i<<" at file "<<os.str()<<".pgm");
105  }
106  savecounter++;
107  }
108 
109  CamWin.ShowImage(pic2);
110 
111  cout <<"Frame: "<<i<<" Showing fps: "<<fps<<"\r";
112  cout.flush();
113 
114  i++;
115  } // if (!pause)
116  }
117 }
118 
119 
120 int main(int argc, char *argv[])
121 {
122  int res;
123 
124 
125  VideoSource_DV2 *cam;
126 
127  // prepare camera
128  cam = new VideoSource_DV2;
129 
130  cam->SetDebugLevel(D_DV);
131 
132  // cam->SetDebugLevel(D_DCAM);
133  res = cam->OpenDevice() ;
134  cout <<"Openresult: "<<res<<endl;
135 
136  if (res <0) {
137  cerr<<" Can not open camera"<<endl;
138  exit(1);
139  }
140 
141  //cam->ShowCapabilities();
142  cam->PreGrab();
143 
144  cout <<" s : save an image \n";
145  cout <<" S : save sequence\n";
146  cout <<"SPC : Stop/Resume\n";
147  cout <<" e : toggle between slow and fast conversion\n";
148  cout <<" q : Quit \n";
149 
150 
151  mainloop(cam );
152 
153  cam->PostGrab();
154 
155  cam->CloseDevice();
156  delete cam;
157 
158  return 0;
159 }
virtual int InitImage(BIAS::ImageBase &Image)
virtual void SetTitle(std::string const &Title)
Definition: GuiCV.cpp:73
int PostGrab()
Stop anything started in PreGrab()
int OpenDevice()
selects the first available device to open (e.g.
Gui based on OpenCV is a simple windowing environment...
Definition: GuiCV.hh:53
int GrabSingle(BIAS::Camera< unsigned char > &image)
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
unsigned int GetHeight() const
Definition: ImageBase.hh:319
This class extends VideoSource for the use of IEEE1394-connected DV-camcorders. The low-level driver ...
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
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
virtual int ShowImage(ImageBase &image, float min=0.0, float max=255.0)
shows/updates the image shown in window.
Definition: GuiBase.cpp:137
virtual char CheckForKeyEvent()
checks if any key is pressed and returns it returns -1 if no key is pressed
Definition: GuiCV.cpp:96
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111