Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMDCamToShmWx.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 
26 /** @example ExampleMDCamToShmWx.cpp
27  @relates VideoShMFeeder, MDcam
28  @ingroup g_examples
29  @ingroup g_videosource
30  @brief Example for using the MDCam object for multiple DCams and writing to shared memory
31  @author MIP
32 */
33 #include <iostream>
34 #include <wx/wx.h>
35 #include <VideoSource/VideoSource_MDcam.hh>
36 #include <Utils/Param.hh>
37 #include <fstream>
38 #include <VideoSource/VideoShMFeeder.hh>
39 #include <signal.h>
40 #include <Gui/StringConv.hh>
41 #include <Base/Debug/Exception.hh>
42 
43 using namespace BIAS;
44 using namespace std;
45 
46 /** \cond HIDDEN_SYMBOLS */
47 class MyApp : public wxApp
48 {
49  virtual bool OnInit();
50 };
51 
52 class MyFrame: public wxFrame
53 {
54 public:
55 
56  MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
57 
58  void OnQuit(wxCommandEvent& event);
59  void OnWhiteBalance(wxCommandEvent& event);
60  void OnAbout(wxCommandEvent& event);
61  void OnAutoExposure(wxCommandEvent& event);
62  void OnGain(wxScrollEvent& event);
63  void OnShutter(wxScrollEvent& event);
64  void OnTimer(wxTimerEvent& event);
65 
66  DECLARE_EVENT_TABLE()
67 protected:
68  wxButton *QuitButton_, *WhiteBalanceButton_;
69  wxCheckBox *AutoExposure_;
70  wxSlider *SliderGain_;
71  wxSlider *SliderShutter_;
72  wxTimer Timer_;
73 
74  VideoSource_MDcam cams;
75  int numcams;
76  Camera<unsigned char> *GrabImage;
77  vector<string> ShmNames;
78  const string camfile;
79  const string ShmPrefix;
80  int *scanbus;
81  vector<VideoShMFeeder *> ShmFeeder;
82  int framecount;
83  const float max_gain;// = 20.0; // dB
84  const float max_shutter;// = 0.050; // 50 ms = 20 Hz
85 };
86 
87 
88 enum
89 {
90  ID_Quit = 1,
91  ID_About,
92  ID_MANUAL_EXPOSURE,
93  ID_SLIDER_GAIN,
94  ID_SLIDER_SHUTTER,
95  //ID_SLIDER_EXPOSURE,
97  ID_BUTTON_WB,
98  ID_TIMER = 1001
99 };
100 
101 
102 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
103  EVT_MENU(ID_Quit, MyFrame::OnQuit)
104  EVT_MENU(ID_About, MyFrame::OnAbout)
105  EVT_CHECKBOX(ID_MANUAL_EXPOSURE, MyFrame::OnAutoExposure)
106  EVT_COMMAND_SCROLL(ID_SLIDER_GAIN, MyFrame::OnGain)
107  EVT_COMMAND_SCROLL(ID_SLIDER_SHUTTER, MyFrame::OnShutter)
108  EVT_BUTTON(ID_BUTTON_WB, MyFrame::OnWhiteBalance)
109  EVT_BUTTON(ID_BUTTON_QUIT, MyFrame::OnQuit)
110  EVT_TIMER(ID_TIMER, MyFrame::OnTimer)
111 END_EVENT_TABLE()
112 
113 IMPLEMENT_APP(MyApp)
114 
115 
116 bool MyApp::OnInit()
117 {
118  MyFrame *frame =
119  new MyFrame( wxT("MDCam2ShmWx"), wxPoint(50,50), wxSize(450,340) );
120  frame->Show(TRUE);
121  SetTopWindow(frame);
122  return TRUE;
123 }
124 
125 
126 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
127  : wxFrame((wxFrame *)NULL, -1, title, pos, size), camfile("cameras.conf"),
128  ShmPrefix("MDCamShm"), max_gain(30.0), max_shutter(0.03)
129 {
130  wxMenu *menuFile = new wxMenu;
131 
132  menuFile->Append( ID_About, wxT("&About...") );
133  menuFile->AppendSeparator();
134  menuFile->Append( ID_Quit, wxT("E&xit") );
135 
136  wxMenuBar *menuBar = new wxMenuBar;
137  menuBar->Append( menuFile, wxT("&File") );
138 
139  SetMenuBar( menuBar );
140 
141  wxSizer *Sizer = new wxBoxSizer( wxVERTICAL );
142 
143  AutoExposure_ =
144  new wxCheckBox(this, ID_MANUAL_EXPOSURE, AsciiToWx("Auto Exposure"),
145  wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
146  AsciiToWx("auto_exposure"));
147 
148  wxStaticText *GainLabel =
149  new wxStaticText(this, -1, AsciiToWx("Gain"));
150  SliderGain_ =
151  new wxSlider(this, ID_SLIDER_GAIN, 50, 0, 255, wxDefaultPosition,
152  wxDefaultSize, wxSL_HORIZONTAL, wxDefaultValidator,
153  AsciiToWx("slider_gain"));
154 
155  wxStaticText *ShutterLabel =
156  new wxStaticText(this, -1, AsciiToWx("Shutter"));
157  SliderShutter_ =
158  new wxSlider(this, ID_SLIDER_SHUTTER, 255, 0, 255, wxDefaultPosition,
159  wxDefaultSize, wxSL_HORIZONTAL, wxDefaultValidator,
160  AsciiToWx("slider_shutter"));
161 
162  QuitButton_ =
163  new wxButton(this, ID_BUTTON_QUIT, AsciiToWx("Quit"), wxDefaultPosition,
164  wxDefaultSize, 0, wxDefaultValidator,
165  AsciiToWx("button"));
166 
167  WhiteBalanceButton_ =
168  new wxButton(this, ID_BUTTON_WB, AsciiToWx("White Balance"),
169  wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
170  AsciiToWx("white_balance_button"));
171 
172  Sizer->Add(AutoExposure_, 0, wxCENTER|wxALL|wxEXPAND,10);
173  Sizer->Add(GainLabel, 0, wxCENTER|wxTOP|wxRIGHT|wxLEFT, 10);
174  Sizer->Add(SliderGain_, 0, wxCENTER|wxALL|wxEXPAND, 10);
175  Sizer->Add(ShutterLabel, 0, wxCENTER|wxTOP|wxRIGHT|wxLEFT, 10);
176  Sizer->Add(SliderShutter_, 0, wxCENTER|wxALL|wxEXPAND,10);
177  Sizer->Add(WhiteBalanceButton_, 0, wxCENTER|wxALL,10);
178  Sizer->Add(QuitButton_, 0, wxCENTER|wxALL,10);
179 
180  SetSizer( Sizer );
181 
182  CreateStatusBar();
183  SetStatusText( wxT("") );
184 
185  Timer_.SetOwner(this, ID_TIMER);
186 
187  cams.SetFps(30.0);
188  //cams.SetDebugLevel(D_MDCAM_INIT );
189  //cams.SetDebugLevelDcam(D_DCAM_F7 |D_DCAM );
190 
191  numcams = cams.InitCameras(camfile);
192  if (numcams <0) exit(0);
193 
194  GrabImage = new Camera<unsigned char>[numcams];
195  for (int i=0; i<numcams; i++) {
196  cams.GetCamera(i)->InitImage(GrabImage[i]);
197  ostringstream oss;
198  oss<<ShmPrefix<<"-"<<cams.GetCamera(i)->GetID();
199  ShmNames.push_back(oss.str());
200  oss.str("");
201  cout <<"Using: "<<ShmNames[i]<<endl;
202  VideoShMFeeder *s;
203  s = new VideoShMFeeder(ShmNames[i]);
204  ShmFeeder.push_back(s);
205  }
206 
207  cams.PreGrab();
208  framecount = 0;
209 
210  AutoExposure_->SetValue(false);
211  wxCommandEvent cevt;
212  OnAutoExposure(cevt);
213  wxScrollEvent evt;
214  OnGain(evt);
215  OnShutter(evt);
216  Timer_.Start(20, true);
217 }
218 
219 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
220 {
221  cams.PostGrab();
222  cams.ReleaseCameras();
223  for (unsigned int i=0;i<ShmFeeder.size(); i++)
224  delete ShmFeeder[i];
225  Close(TRUE);
226 }
227 
228 void MyFrame::OnWhiteBalance(wxCommandEvent& WXUNUSED(event))
229 {
230  cams.OnePushAuto(DC1394_FEATURE_WHITE_BALANCE);
231  cerr << "\nwhite balance"<<endl;
232 }
233 
234 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
235 {
236  wxMessageBox(wxT("This is a wxWidgets Hello world sample"),
237  wxT("About Hello World"), wxOK | wxICON_INFORMATION, this);
238 }
239 
240 void MyFrame::OnAutoExposure(wxCommandEvent& event)
241 {
242  bool auto_exposure = AutoExposure_->IsChecked();
243  if (auto_exposure){
244  // get shutter and gain from sliders an set to cam
245  int value = SliderGain_->GetValue();
246  float gain = (float)value / 255.0 * max_gain;
247  value = SliderShutter_->GetValue();
248  float shutter = (float)value / 255.0 * max_shutter;
249  if (cams.AutoMode(DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO)!=0){
250  BEXCEPTION("error setting camera");
251  }
252  if (cams.AutoMode(DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_AUTO)!=0){
253  BEXCEPTION("error setting camera");
254  }
255  if (cams.SetGain(gain)!=0){
256  BEXCEPTION("error setting camera");
257  }
258  if (cams.SetShutter(shutter)!=0){
259  BEXCEPTION("error setting camera");
260  }
261  } else {
262  if (cams.AutoMode(DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL)!=0){
263  BEXCEPTION("error setting camera");
264  }
265  if (cams.AutoMode(DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL)!=0){
266  BEXCEPTION("error setting camera");
267  }
268  }
269 }
270 
271 void MyFrame::OnGain(wxScrollEvent& event)
272 {
273  if (!AutoExposure_->IsChecked()){
274  int value = SliderGain_->GetValue();
275  float gain = (float)value / 255.0 * max_gain;
276  if (cams.SetGain(gain)!=0){
277  BEXCEPTION("error setting camera");
278  }
279  //cout << "setting gain: "<<gain<<endl;
280  }
281 }
282 
283 void MyFrame::OnShutter(wxScrollEvent& event)
284 {
285  if (!AutoExposure_->IsChecked()){
286  int value = SliderShutter_->GetValue();
287  float shutter = (float)value / 255.0 * max_shutter;
288  if (cams.SetShutter(shutter)!=0){
289  BEXCEPTION("error setting camera");
290  }
291  //cout << "setting shutter: "<<shutter<<endl;
292  }
293 }
294 
295 void MyFrame::OnTimer(wxTimerEvent& event)
296 {
297  cams.GrabSingle(GrabImage);
298  //cout << "numcams: " << numcams<<endl;
299  for (int i=0; i<numcams; i++) {
300  ShmFeeder[i]->ProcessImage(&(GrabImage[i]));
301  //cout << "cm: bayer: "<<boolalpha<< (GrabImage[i].GetColorModel()==ImageBase::CM_Bayer_RGGB)<<endl;
302  }
303  cout <<framecount++<<endl;
304  Timer_.Start(20, true); // grab new images every 20 ms = resp 50 Hz
305 }
306 /** \endcond */
307 
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
extends the Image by MetaData support (e.g.
Definition: Camera.hh:74
VideoSource_MDcam simplifies acces to more then one DCam. It suports multiple IEEE1394-adapters, extern synchronization via parallel port and networked capture cluster.
class to save Images to a Sharerd memory