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