Basic Image AlgorithmS Library 2.8.0

ExampleMDCamToShmWx.cpp

00001 /*
00002 This file is part of the BIAS library (Basic ImageAlgorithmS).
00003 
00004 Copyright (C) 2003-2009    (see file CONTACT for details)
00005   Multimediale Systeme der Informationsverarbeitung
00006   Institut fuer Informatik
00007   Christian-Albrechts-Universitaet Kiel
00008 
00009 
00010 BIAS is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU Lesser General Public License as published by
00012 the Free Software Foundation; either version 2.1 of the License, or
00013 (at your option) any later version.
00014 
00015 BIAS is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License
00021 along with BIAS; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 
00026 /** @example ExampleMDCamToShmWx.cpp
00027     @relates VideoShMFeeder, MDcam
00028     @ingroup g_examples
00029     @ingroup g_videosource
00030     @brief  Example for using the MDCam object for multiple DCams and writing to shared memory
00031     @author MIP
00032 */
00033 #include <iostream>
00034 #include <wx/wx.h>
00035 #include <VideoSource/VideoSource_MDcam.hh>
00036 #include <Utils/Param.hh>
00037 #include <fstream>
00038 #include <VideoSource/VideoShMFeeder.hh>
00039 #include <signal.h>
00040 #include <Gui/StringConv.hh>
00041 #include <Base/Debug/Exception.hh>
00042 
00043 using namespace BIAS;
00044 using namespace std;
00045 
00046 /** \cond HIDDEN_SYMBOLS */
00047 class MyApp : public wxApp
00048 {
00049   virtual bool OnInit();
00050 };
00051 
00052 class MyFrame: public wxFrame
00053 {
00054 public:
00055   
00056   MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
00057   
00058   void OnQuit(wxCommandEvent& event);
00059   void OnWhiteBalance(wxCommandEvent& event);
00060   void OnAbout(wxCommandEvent& event);
00061   void OnAutoExposure(wxCommandEvent& event);
00062   void OnGain(wxScrollEvent& event);
00063   void OnShutter(wxScrollEvent& event);
00064   void OnTimer(wxTimerEvent& event);
00065 
00066   DECLARE_EVENT_TABLE()
00067 protected:
00068   wxButton *QuitButton_, *WhiteBalanceButton_;
00069   wxCheckBox *AutoExposure_;
00070   wxSlider *SliderGain_;
00071   wxSlider *SliderShutter_; 
00072   wxTimer      Timer_;
00073 
00074   VideoSource_MDcam cams;
00075   int numcams;
00076   Camera<unsigned char> *GrabImage;
00077   vector<string> ShmNames;
00078   const string camfile; 
00079   const string ShmPrefix;
00080   int *scanbus;
00081   vector<VideoShMFeeder *> ShmFeeder;
00082   int framecount;
00083   const float max_gain;// = 20.0; // dB
00084   const float max_shutter;// = 0.050; // 50 ms = 20 Hz
00085 };
00086 
00087 
00088 enum
00089 {
00090   ID_Quit = 1,
00091   ID_About,
00092   ID_MANUAL_EXPOSURE,
00093   ID_SLIDER_GAIN,
00094   ID_SLIDER_SHUTTER,
00095   //ID_SLIDER_EXPOSURE,
00096   ID_BUTTON_QUIT,
00097   ID_BUTTON_WB,
00098   ID_TIMER = 1001
00099 };
00100 
00101 
00102 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
00103   EVT_MENU(ID_Quit, MyFrame::OnQuit)
00104   EVT_MENU(ID_About, MyFrame::OnAbout)
00105   EVT_CHECKBOX(ID_MANUAL_EXPOSURE, MyFrame::OnAutoExposure)
00106   EVT_COMMAND_SCROLL(ID_SLIDER_GAIN, MyFrame::OnGain)
00107   EVT_COMMAND_SCROLL(ID_SLIDER_SHUTTER, MyFrame::OnShutter)
00108   EVT_BUTTON(ID_BUTTON_WB, MyFrame::OnWhiteBalance)
00109   EVT_BUTTON(ID_BUTTON_QUIT, MyFrame::OnQuit)
00110   EVT_TIMER(ID_TIMER, MyFrame::OnTimer)
00111 END_EVENT_TABLE()
00112 
00113 IMPLEMENT_APP(MyApp)
00114 
00115 
00116 bool MyApp::OnInit()
00117 {
00118   MyFrame *frame = 
00119       new MyFrame( wxT("MDCam2ShmWx"), wxPoint(50,50), wxSize(450,340) );
00120   frame->Show(TRUE);
00121   SetTopWindow(frame);
00122   return TRUE;
00123 }
00124 
00125 
00126 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
00127   : wxFrame((wxFrame *)NULL, -1, title, pos, size), camfile("cameras.conf"),
00128     ShmPrefix("MDCamShm"), max_gain(30.0), max_shutter(0.03)
00129 {
00130   wxMenu *menuFile = new wxMenu;
00131 
00132   menuFile->Append( ID_About, wxT("&About...") );
00133   menuFile->AppendSeparator();
00134   menuFile->Append( ID_Quit, wxT("E&xit") );
00135 
00136   wxMenuBar *menuBar = new wxMenuBar;
00137   menuBar->Append( menuFile, wxT("&File") );
00138 
00139   SetMenuBar( menuBar );
00140 
00141   wxSizer *Sizer = new wxBoxSizer( wxVERTICAL );
00142 
00143   AutoExposure_ = 
00144     new wxCheckBox(this, ID_MANUAL_EXPOSURE, AsciiToWx("Auto Exposure"), 
00145                    wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, 
00146                    AsciiToWx("auto_exposure"));
00147 
00148   wxStaticText *GainLabel = 
00149     new wxStaticText(this, -1, AsciiToWx("Gain"));
00150   SliderGain_ = 
00151     new wxSlider(this, ID_SLIDER_GAIN, 50, 0, 255, wxDefaultPosition, 
00152                  wxDefaultSize,  wxSL_HORIZONTAL, wxDefaultValidator, 
00153                  AsciiToWx("slider_gain"));
00154   
00155   wxStaticText *ShutterLabel = 
00156     new wxStaticText(this, -1, AsciiToWx("Shutter"));
00157   SliderShutter_ = 
00158     new wxSlider(this, ID_SLIDER_SHUTTER, 255, 0, 255, wxDefaultPosition, 
00159                  wxDefaultSize,  wxSL_HORIZONTAL, wxDefaultValidator, 
00160                  AsciiToWx("slider_shutter"));
00161 
00162   QuitButton_ = 
00163     new wxButton(this, ID_BUTTON_QUIT, AsciiToWx("Quit"), wxDefaultPosition,
00164                  wxDefaultSize, 0, wxDefaultValidator, 
00165                  AsciiToWx("button"));
00166 
00167   WhiteBalanceButton_ = 
00168     new wxButton(this, ID_BUTTON_WB, AsciiToWx("White Balance"), 
00169                  wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, 
00170                  AsciiToWx("white_balance_button"));
00171 
00172   Sizer->Add(AutoExposure_, 0, wxCENTER|wxALL|wxEXPAND,10);
00173   Sizer->Add(GainLabel, 0, wxCENTER|wxTOP|wxRIGHT|wxLEFT, 10);
00174   Sizer->Add(SliderGain_, 0, wxCENTER|wxALL|wxEXPAND, 10);
00175   Sizer->Add(ShutterLabel, 0, wxCENTER|wxTOP|wxRIGHT|wxLEFT, 10);
00176   Sizer->Add(SliderShutter_, 0, wxCENTER|wxALL|wxEXPAND,10);
00177   Sizer->Add(WhiteBalanceButton_, 0, wxCENTER|wxALL,10);
00178   Sizer->Add(QuitButton_, 0, wxCENTER|wxALL,10);
00179 
00180   SetSizer( Sizer );
00181 
00182   CreateStatusBar();
00183   SetStatusText( wxT("") );
00184 
00185   Timer_.SetOwner(this, ID_TIMER);
00186 
00187   cams.SetFps(30.0);
00188   //cams.SetDebugLevel(D_MDCAM_INIT );
00189   //cams.SetDebugLevelDcam(D_DCAM_F7 |D_DCAM );
00190   
00191   numcams = cams.InitCameras(camfile);
00192   if (numcams <0) exit(0);
00193   
00194   GrabImage = new Camera<unsigned char>[numcams];
00195   for (int i=0; i<numcams; i++) {
00196     cams.GetCamera(i)->InitImage(GrabImage[i]);
00197     ostringstream oss;
00198     oss<<ShmPrefix<<"-"<<cams.GetCamera(i)->GetID();
00199     ShmNames.push_back(oss.str());
00200     oss.str("");
00201     cout <<"Using: "<<ShmNames[i]<<endl;
00202     VideoShMFeeder *s;
00203     s = new VideoShMFeeder(ShmNames[i]);
00204     ShmFeeder.push_back(s);
00205   }
00206 
00207   cams.PreGrab();
00208   framecount = 0;
00209 
00210   AutoExposure_->SetValue(false);
00211   wxCommandEvent cevt;
00212   OnAutoExposure(cevt);
00213   wxScrollEvent evt;
00214   OnGain(evt);
00215   OnShutter(evt);
00216   Timer_.Start(20, true);
00217 }
00218 
00219 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
00220 {
00221   cams.PostGrab();
00222   cams.ReleaseCameras();
00223   for (unsigned int i=0;i<ShmFeeder.size(); i++)
00224     delete ShmFeeder[i];
00225   Close(TRUE);
00226 }
00227 
00228 void MyFrame::OnWhiteBalance(wxCommandEvent& WXUNUSED(event))
00229 {
00230   cams.OnePushAuto(DC1394_FEATURE_WHITE_BALANCE);
00231   cerr << "\nwhite balance"<<endl;
00232 }
00233 
00234 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
00235 {
00236   wxMessageBox(wxT("This is a wxWidgets Hello world sample"),
00237                wxT("About Hello World"), wxOK | wxICON_INFORMATION, this);
00238 }
00239 
00240 void MyFrame::OnAutoExposure(wxCommandEvent& event)
00241 {
00242   bool auto_exposure = AutoExposure_->IsChecked();
00243   if (auto_exposure){
00244     // get shutter and gain from sliders an set to cam
00245     int value = SliderGain_->GetValue();
00246     float gain = (float)value / 255.0 * max_gain;
00247     value = SliderShutter_->GetValue();
00248     float shutter = (float)value / 255.0 * max_shutter;
00249     if (cams.AutoMode(DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO)!=0){
00250       BEXCEPTION("error setting camera");
00251     }
00252     if (cams.AutoMode(DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_AUTO)!=0){
00253       BEXCEPTION("error setting camera");
00254     }
00255     if (cams.SetGain(gain)!=0){
00256       BEXCEPTION("error setting camera");
00257     }
00258     if (cams.SetShutter(shutter)!=0){
00259       BEXCEPTION("error setting camera");
00260     }
00261   } else {
00262     if (cams.AutoMode(DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL)!=0){
00263       BEXCEPTION("error setting camera");
00264     }
00265     if (cams.AutoMode(DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL)!=0){
00266       BEXCEPTION("error setting camera");
00267     }
00268   }
00269 }
00270 
00271 void MyFrame::OnGain(wxScrollEvent& event)
00272 {
00273   if (!AutoExposure_->IsChecked()){
00274     int value = SliderGain_->GetValue();
00275     float gain = (float)value / 255.0 * max_gain;
00276     if (cams.SetGain(gain)!=0){
00277       BEXCEPTION("error setting camera");
00278     }
00279     //cout << "setting gain: "<<gain<<endl;
00280   }
00281 }
00282 
00283 void MyFrame::OnShutter(wxScrollEvent& event)
00284 {
00285   if (!AutoExposure_->IsChecked()){
00286     int value = SliderShutter_->GetValue();
00287     float shutter = (float)value / 255.0 * max_shutter;
00288     if (cams.SetShutter(shutter)!=0){
00289       BEXCEPTION("error setting camera");
00290     }
00291     //cout << "setting shutter: "<<shutter<<endl;
00292   }
00293 }
00294 
00295 void MyFrame::OnTimer(wxTimerEvent& event)
00296 {
00297   cams.GrabSingle(GrabImage);
00298   //cout << "numcams: " << numcams<<endl;
00299   for (int i=0; i<numcams; i++) {
00300     ShmFeeder[i]->ProcessImage(&(GrabImage[i]));
00301     //cout << "cm: bayer: "<<boolalpha<< (GrabImage[i].GetColorModel()==ImageBase::CM_Bayer_RGGB)<<endl;
00302   } 
00303   cout <<framecount++<<endl;
00304   Timer_.Start(20, true); // grab new images every 20 ms = resp 50 Hz
00305 }
00306 /** \endcond */
00307 
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends