Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
showdcam16.cpp
1 
2 #include <Image/Camera.hh>
3 #include <Base/Image/ImageConvert.hh>
4 #include <Base/Image/ImageIO.hh>
5 #include <VideoSource/VideoSource_DCAM.hh>
6 
7 #include <Gui/GuiCV.hh>
8 
9 #ifdef WIN32
10 # include<Base/Common/getopt_W32.h>
11 #else
12 # include <unistd.h>
13 # include <getopt.h>
14 #endif
15 
16 
17 using namespace BIAS;
18 using namespace std;
19 
20 
21 #ifdef BUILD_IMAGE_USHORT
22 #define StorageType unsigned char
23 
24 #include <Base/Debug/TimeMeasure.hh>
25 
26 float rgain=-31.0/32.0, bgain=31.0/32.0;
27 float gain = 0.0;
28 unsigned savecounter=0;
29 
30 
31 void Get8bitsfrom16(Image<unsigned short> &src, Image<unsigned char> &dst,
32  unsigned int shift = 0)
33 {
34  unsigned short *us = src.GetImageData();
35  unsigned char *uc = dst.GetImageData();
36  unsigned int pc = src.GetPixelCount() * src.GetChannelCount();
37  int t;
38  for (unsigned int i=0;i<pc; i++) {
39  t = ((us[i])>>shift);
40  if (t> 255) uc[i] = 255;
41  else uc[i] = (unsigned char)(t & 255);
42  }
43 }
44 
45 void mainloop(VideoSource_DCAM *cam)
46 {
47  GuiCV CamWin;
48  bool done = false;
49  char c;
50  char title[100];
51 
52  struct timeval now;
53  float fps;
54  int i,savedframes;
55  unsigned int shift = 2;
56  long start=0, end;
57  bool color, pause, autosave=false;
58  ostringstream os;
59 
60  TimeMeasure t1,t2;
61 
64  sprintf(title,"MyWindow");
65  CamWin.SetTitle(title);
66  cam->InitImage(pic);
67  //ImageIO::Save("foo",pic);
68  ImageIO::Save("foo", pic);
69 
70  pic1.Init(pic.GetWidth(), pic.GetHeight(),1);
71 
72  pause = false;
73  color = true;
74 
75  i = 1;
76  savedframes = 0;
77 
78  bool save;
79  //pic2.SetDebugLevel(D_CONVERT);
80 
81  while (!done){
82  save = false;
83  gettimeofday(&now, NULL);
84  end = start;
85  start = (now.tv_sec % 1000) * 1000 + (now.tv_usec / 1000);
86  fps = 1.0f / (float)(start-end) * 1000.0f;
87 
88  c = CamWin.CheckForKeyEvent();
89  switch (c) {
90  case 'q':
91  case 27:
92  done = true;
93  break;
94  case 'g':
95  cam->AutoMode(DC1394_FEATURE_GAIN,DC1394_FEATURE_MODE_AUTO);
96  cerr << "\nauto gain on"<<endl;
97  break;
98  case '1':
99  cout <<endl<<"Shutter: "<<cam->GetShutter()<<endl;
100  // cam->SetShutter(0.01);
101  break;
102  case 'w':
103  cam->OnePushAuto(DC1394_FEATURE_WHITE_BALANCE);
104  cerr << "\nwhite balance"<<endl;
105  break;
106  case 'S':
107  case ' ':
108  save = true;
109  break;
110  case 's':
111  autosave=!autosave;
112  if (autosave)
113  cerr << "\nstarted saving sequence at frame "<<i<<" into frame"
114  <<setfill('0')<<setw(5)<<savecounter<<".mip"<<endl;
115  else
116  cerr << "\nstopped saving sequence at frame "<<i<<" into frame"
117  <<setfill('0')<<setw(5)<<savecounter<<".mip"<<endl;
118  break;
119  case 'p':
120  pause = !pause;
121  break;
122  case 'c':
123  color = !color;
124  if (color) cerr << "\nshowing color image" << endl;
125  else cerr << "\nshowing grey image" << endl;
126  break;
127  case 'G':
128  cam->AutoMode(DC1394_FEATURE_GAIN,DC1394_FEATURE_MODE_MANUAL);
129  cerr << "\nauto gain off"<<endl;
130  break;
131  case 'e':
132  if (shift >0) shift--;
133  cout <<endl<<"shift: "<<shift<<endl;
134  break;
135  case 'E':
136  if (shift <4) shift++;
137  cout <<endl<<"shift: "<<shift<<endl;
138  break;
139  case '[':
140  gain -= 1.0/32.0;
141  cam->SetGain(gain);
142  cout<<"Overall gain :"<<gain<<endl;
143  break;
144  case ']':
145  gain += 1.0/32.0;
146  cam->SetGain(gain);
147  cout<<"Overall gain :"<<gain<<endl;
148  break;
149  default:
150  if (c!=-1) cerr <<"unknown key "<<c<<endl;
151  break;
152  }
153 
154 
155  if (!pause) {
156  cam->GrabSingle(pic);
157 
158  unsigned short min, max;
159  pic.GetMinMaxPixelValue(min, max);
160  cout << "min: "<<min<<"\tmax: "<<max<<" "<<endl;
161 
162  Get8bitsfrom16(pic,pic1,shift);
163  // pic1.SetColorModel(pic.GetColorModel());
165 
166 
167  if (save) {
168  os.str("");
169  os << "frame" << setfill('0') << setw(5) << savecounter;
170  //ImageIO::Save(os.str(), pic, true);
171  ImageIO::Save(os.str(),
172  pic,
174  true);
175  // ImageIO::ExportImage(os.str(),pic, ImageIO::FF_ppm);
176  cerr << "\nsaved frame "<<i<<" in "<<os.str()<<endl;
177  os <<"_uc";
178  //ImageIO::Save(os.str(), pic1, true);
179  ImageIO::Save(os.str(),
180  pic1,
182  true);
183  save = false;
184  savecounter++;
185  }
186  if (autosave){
187  os.str("");
188  os << "frame" << setfill('0') << setw(5) << savecounter;
189  //if (ImageIO::Save(os.str(), pic, true)!=0){
190  if (ImageIO::Save(os.str(), pic, ImageIO::FF_auto, true)!=0){
191  BIASERR("error saving frame "<<i<<" at file "<<os.str()<<".mip");
192  }
193  savecounter++;
194  }
195 
196  CamWin.ShowImage(pic1);
197 
198  cout <<"Frame: "<<i<<" Showing fps: "<<fps<<"\r";
199  cout.flush();
200 
201  i++;
202  } // if (!pause)
203  }
204 }
205 
206 
207 
208 
209 
210 
211 
212 int main(int argc, char *argv[])
213 {
214  int res;
215 
216  VideoSource_DCAM *cam;
217 
218  // prepare camera
219  cam = new VideoSource_DCAM;
220 
221  // cam->SetSize(1280,960,2.0);
222  //cam->SetSize(1600,1200,2);
223 // cam->SetSize(1024,768,2);
224  // cam->SetSize(320,250,2);
225  cam->SetBytesPerPixel(2);
226 
227  cam->SetDebugLevel(D_DCAM |D_DCAM_F7);
228  res = cam->OpenDevice() ;
229  cout <<"Openresult: "<<res<<endl;
230 
231  if (res <0) {
232  cerr<<" Can not open camera"<<endl;
233  exit(1);
234  }
235 
236  cam->PreGrab();
237 
238  cout <<" g : Auto Gain ON\n";
239  cout <<" G : Auto Gain OFF\n";
240  cout <<"123 : Gamma mode"<<endl;
241  cout <<" w : OnePushWhitebalance (not for PointGrey DragonFly)\n";
242  cout <<" 7/8: decr/incr blue gain (for white balancing manual)\n";
243  cout <<" 4/5: decr/incr red gain (for white balancing manual)\n";
244  cout <<" c : Color/Grey \n";
245  cout <<"SPC : save an image \n";
246  cout <<" s : save sequence\n";
247  cout <<" p : Stop/Resume\n";
248  cout <<" e : toggle between fast and normal conversion\n";
249  cout <<" q : Quit \n";
250 
251  cam->SetDebugLevel(0);
252 // cam->SetFeature(FEATURE_SHARPNESS,0);
253 
254  mainloop(cam );
255 
256  cam->PostGrab();
257 
258  cam->CloseDevice();
259  delete cam;
260 
261  return 0;
262 }
263 
264 #else
265 int main(int argc, char *argv[])
266 {
267  BIASERR("showdcam16 can only work if BIAS was compiled with BUILD_IMAGE_USHORT");
268 }
269 #endif
270 
virtual int InitImage(BIAS::ImageBase &Image)
virtual void SetTitle(std::string const &Title)
Definition: GuiCV.cpp:73
int GrabSingle(BIAS::Camera< unsigned char > &image)
Select the port (bus-id) to use, only valid before OpenDevice()
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
Bayer_RGGB, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:143
Gui based on OpenCV is a simple windowing environment...
Definition: GuiCV.hh:53
This class VideoSource_DCAM implements access to IEEE1394 (Firewire, iLink) cameras following the DCa...
int SetGain(float g)
set value for gain
void GetMinMaxPixelValue(StorageType &min, StorageType &max, unsigned short int channel=0, unsigned int *mincoo=NULL, unsigned int *maxcoo=NULL) const
returns the minimal and maximal pixel value in channel only Finds minimum and maximum pixel value in ...
Definition: Image.cpp:802
int OpenDevice()
Opens a dc1394 device.
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void SetBytesPerPixel(unsigned char bpp)
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int AutoMode(dc1394feature_t feature, dc1394feature_mode_t mode)
Set a camera feature to auto or to manual mode.
The image template class for specific storage types.
Definition: Image.hh:78
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 OnePushAuto(dc1394feature_t feature)
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
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
int CloseDevice()
closes a camera device and cleans up
int PostGrab()
stops iso transmission of a camera.
int PreGrab()
initializes the iso transmission of a camera.
unsigned long int GetPixelCount() const
returns number of pixels in image
Definition: ImageBase.hh:422
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
float GetShutter()
get the value of the shutter feature