Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMDCamToShmWx.cpp

Example for using the MDCam object for multiple DCams and writing to shared memory , MDcam

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExampleMDCamToShmWx.cpp
@relates VideoShMFeeder, MDcam
@ingroup g_examples
@ingroup g_videosource
@brief Example for using the MDCam object for multiple DCams and writing to shared memory
@author MIP
*/
#include <iostream>
#include <wx/wx.h>
#include <VideoSource/VideoSource_MDcam.hh>
#include <Utils/Param.hh>
#include <fstream>
#include <VideoSource/VideoShMFeeder.hh>
#include <signal.h>
#include <Gui/StringConv.hh>
#include <Base/Debug/Exception.hh>
using namespace BIAS;
using namespace std;
/** \cond HIDDEN_SYMBOLS */
class MyApp : public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnWhiteBalance(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnAutoExposure(wxCommandEvent& event);
void OnGain(wxScrollEvent& event);
void OnShutter(wxScrollEvent& event);
void OnTimer(wxTimerEvent& event);
DECLARE_EVENT_TABLE()
protected:
wxButton *QuitButton_, *WhiteBalanceButton_;
wxCheckBox *AutoExposure_;
wxSlider *SliderGain_;
wxSlider *SliderShutter_;
wxTimer Timer_;
int numcams;
Camera<unsigned char> *GrabImage;
vector<string> ShmNames;
const string camfile;
const string ShmPrefix;
int *scanbus;
vector<VideoShMFeeder *> ShmFeeder;
int framecount;
const float max_gain;// = 20.0; // dB
const float max_shutter;// = 0.050; // 50 ms = 20 Hz
};
enum
{
ID_Quit = 1,
ID_MANUAL_EXPOSURE,
ID_SLIDER_GAIN,
ID_SLIDER_SHUTTER,
//ID_SLIDER_EXPOSURE,
ID_BUTTON_WB,
ID_TIMER = 1001
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
EVT_CHECKBOX(ID_MANUAL_EXPOSURE, MyFrame::OnAutoExposure)
EVT_COMMAND_SCROLL(ID_SLIDER_GAIN, MyFrame::OnGain)
EVT_COMMAND_SCROLL(ID_SLIDER_SHUTTER, MyFrame::OnShutter)
EVT_BUTTON(ID_BUTTON_WB, MyFrame::OnWhiteBalance)
EVT_BUTTON(ID_BUTTON_QUIT, MyFrame::OnQuit)
EVT_TIMER(ID_TIMER, MyFrame::OnTimer)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame =
new MyFrame( wxT("MDCam2ShmWx"), wxPoint(50,50), wxSize(450,340) );
frame->Show(TRUE);
SetTopWindow(frame);
return TRUE;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size), camfile("cameras.conf"),
ShmPrefix("MDCamShm"), max_gain(30.0), max_shutter(0.03)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, wxT("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, wxT("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, wxT("&File") );
SetMenuBar( menuBar );
wxSizer *Sizer = new wxBoxSizer( wxVERTICAL );
AutoExposure_ =
new wxCheckBox(this, ID_MANUAL_EXPOSURE, AsciiToWx("Auto Exposure"),
wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
AsciiToWx("auto_exposure"));
wxStaticText *GainLabel =
new wxStaticText(this, -1, AsciiToWx("Gain"));
SliderGain_ =
new wxSlider(this, ID_SLIDER_GAIN, 50, 0, 255, wxDefaultPosition,
wxDefaultSize, wxSL_HORIZONTAL, wxDefaultValidator,
AsciiToWx("slider_gain"));
wxStaticText *ShutterLabel =
new wxStaticText(this, -1, AsciiToWx("Shutter"));
SliderShutter_ =
new wxSlider(this, ID_SLIDER_SHUTTER, 255, 0, 255, wxDefaultPosition,
wxDefaultSize, wxSL_HORIZONTAL, wxDefaultValidator,
AsciiToWx("slider_shutter"));
QuitButton_ =
new wxButton(this, ID_BUTTON_QUIT, AsciiToWx("Quit"), wxDefaultPosition,
wxDefaultSize, 0, wxDefaultValidator,
AsciiToWx("button"));
WhiteBalanceButton_ =
new wxButton(this, ID_BUTTON_WB, AsciiToWx("White Balance"),
wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
AsciiToWx("white_balance_button"));
Sizer->Add(AutoExposure_, 0, wxCENTER|wxALL|wxEXPAND,10);
Sizer->Add(GainLabel, 0, wxCENTER|wxTOP|wxRIGHT|wxLEFT, 10);
Sizer->Add(SliderGain_, 0, wxCENTER|wxALL|wxEXPAND, 10);
Sizer->Add(ShutterLabel, 0, wxCENTER|wxTOP|wxRIGHT|wxLEFT, 10);
Sizer->Add(SliderShutter_, 0, wxCENTER|wxALL|wxEXPAND,10);
Sizer->Add(WhiteBalanceButton_, 0, wxCENTER|wxALL,10);
Sizer->Add(QuitButton_, 0, wxCENTER|wxALL,10);
SetSizer( Sizer );
CreateStatusBar();
SetStatusText( wxT("") );
Timer_.SetOwner(this, ID_TIMER);
cams.SetFps(30.0);
//cams.SetDebugLevel(D_MDCAM_INIT );
//cams.SetDebugLevelDcam(D_DCAM_F7 |D_DCAM );
numcams = cams.InitCameras(camfile);
if (numcams <0) exit(0);
GrabImage = new Camera<unsigned char>[numcams];
for (int i=0; i<numcams; i++) {
cams.GetCamera(i)->InitImage(GrabImage[i]);
ostringstream oss;
oss<<ShmPrefix<<"-"<<cams.GetCamera(i)->GetID();
ShmNames.push_back(oss.str());
oss.str("");
cout <<"Using: "<<ShmNames[i]<<endl;
s = new VideoShMFeeder(ShmNames[i]);
ShmFeeder.push_back(s);
}
cams.PreGrab();
framecount = 0;
AutoExposure_->SetValue(false);
wxCommandEvent cevt;
OnAutoExposure(cevt);
wxScrollEvent evt;
OnGain(evt);
OnShutter(evt);
Timer_.Start(20, true);
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
cams.PostGrab();
cams.ReleaseCameras();
for (unsigned int i=0;i<ShmFeeder.size(); i++)
delete ShmFeeder[i];
Close(TRUE);
}
void MyFrame::OnWhiteBalance(wxCommandEvent& WXUNUSED(event))
{
cams.OnePushAuto(DC1394_FEATURE_WHITE_BALANCE);
cerr << "\nwhite balance"<<endl;
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(wxT("This is a wxWidgets Hello world sample"),
wxT("About Hello World"), wxOK | wxICON_INFORMATION, this);
}
void MyFrame::OnAutoExposure(wxCommandEvent& event)
{
bool auto_exposure = AutoExposure_->IsChecked();
if (auto_exposure){
// get shutter and gain from sliders an set to cam
int value = SliderGain_->GetValue();
float gain = (float)value / 255.0 * max_gain;
value = SliderShutter_->GetValue();
float shutter = (float)value / 255.0 * max_shutter;
if (cams.AutoMode(DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO)!=0){
BEXCEPTION("error setting camera");
}
if (cams.AutoMode(DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_AUTO)!=0){
BEXCEPTION("error setting camera");
}
if (cams.SetGain(gain)!=0){
BEXCEPTION("error setting camera");
}
if (cams.SetShutter(shutter)!=0){
BEXCEPTION("error setting camera");
}
} else {
if (cams.AutoMode(DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL)!=0){
BEXCEPTION("error setting camera");
}
if (cams.AutoMode(DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL)!=0){
BEXCEPTION("error setting camera");
}
}
}
void MyFrame::OnGain(wxScrollEvent& event)
{
if (!AutoExposure_->IsChecked()){
int value = SliderGain_->GetValue();
float gain = (float)value / 255.0 * max_gain;
if (cams.SetGain(gain)!=0){
BEXCEPTION("error setting camera");
}
//cout << "setting gain: "<<gain<<endl;
}
}
void MyFrame::OnShutter(wxScrollEvent& event)
{
if (!AutoExposure_->IsChecked()){
int value = SliderShutter_->GetValue();
float shutter = (float)value / 255.0 * max_shutter;
if (cams.SetShutter(shutter)!=0){
BEXCEPTION("error setting camera");
}
//cout << "setting shutter: "<<shutter<<endl;
}
}
void MyFrame::OnTimer(wxTimerEvent& event)
{
cams.GrabSingle(GrabImage);
//cout << "numcams: " << numcams<<endl;
for (int i=0; i<numcams; i++) {
ShmFeeder[i]->ProcessImage(&(GrabImage[i]));
//cout << "cm: bayer: "<<boolalpha<< (GrabImage[i].GetColorModel()==ImageBase::CM_Bayer_RGGB)<<endl;
}
cout <<framecount++<<endl;
Timer_.Start(20, true); // grab new images every 20 ms = resp 50 Hz
}
/** \endcond */