Example for using the MDCam object for multiple DCams and writing to shared memory , MDcam
#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;
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;
int framecount;
const float max_gain;
const float max_shutter;
};
enum
{
ID_MANUAL_EXPOSURE,
ID_SLIDER_GAIN,
ID_SLIDER_SHUTTER,
ID_BUTTON_WB,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
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)
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,
wxStaticText *GainLabel =
new wxStaticText(
this, -1,
AsciiToWx(
"Gain"));
SliderGain_ =
new wxSlider(this, ID_SLIDER_GAIN, 50, 0, 255, wxDefaultPosition,
wxDefaultSize, wxSL_HORIZONTAL, wxDefaultValidator,
wxStaticText *ShutterLabel =
new wxStaticText(
this, -1,
AsciiToWx(
"Shutter"));
SliderShutter_ =
new wxSlider(this, ID_SLIDER_SHUTTER, 255, 0, 255, wxDefaultPosition,
wxDefaultSize, wxSL_HORIZONTAL, wxDefaultValidator,
QuitButton_ =
new wxButton(
this, ID_BUTTON_QUIT,
AsciiToWx(
"Quit"), wxDefaultPosition,
wxDefaultSize, 0, wxDefaultValidator,
WhiteBalanceButton_ =
new wxButton(
this, ID_BUTTON_WB,
AsciiToWx(
"White Balance"),
wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
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);
numcams = cams.InitCameras(camfile);
if (numcams <0) exit(0);
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;
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){
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");
}
}
}
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");
}
}
}
void MyFrame::OnTimer(wxTimerEvent& event)
{
cams.GrabSingle(GrabImage);
for (int i=0; i<numcams; i++) {
ShmFeeder[i]->ProcessImage(&(GrabImage[i]));
}
cout <<framecount++<<endl;
Timer_.Start(20, true);
}