Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Controller_Kinect.cpp
1 #ifdef WIN32
2 // workaround for wx 2.6.x redefiniton of _CRT_SECURE_NO_DEPRECATE
3 // fixed in wx 2.7.2
4 # pragma warning(disable: 4005)
5 #endif
6 
7 #include "VideoSource_Controller_Kinect.hh"
8 
9 #ifdef WIN32
10 # include <Base/Common/getopt_W32.h>
11 #endif
12 
13 #include <iostream>
14 #include <Base/Image/ImageIO.hh>
15 #include <Image/Camera.hh>
16 #include <Gui/StringConv.hh>
17 #include <wx/string.h>
18 
19 using namespace std;
20 using namespace BIAS;
21 
22 VideoSource_Controller_Kinect::
23 VideoSource_Controller_Kinect(VideoSource *cam, wxWindow *parent, const std::string &title)
24  : VideoSource_Controller_Base(parent, -1, AsciiToWx(title)
25  ,wxDefaultPosition,wxSize(365,310),wxSTAY_ON_TOP |wxCAPTION)
26 {
27  update_ms_ = 100;
28  curTilt_ = 0;
29 
30  Cam_ = dynamic_cast<BIAS::VideoSource_Kinect_Base*>(cam);
31 
32 
33  this->SetSizeHints( wxDefaultSize, wxDefaultSize );
34 
35  wxGridSizer* gSizer1;
36  gSizer1 = new wxGridSizer( 2, 2, 0, 0 );
37 
38  wxBoxSizer* bSizer1;
39  bSizer1 = new wxBoxSizer( wxVERTICAL );
40 
41  wxString m_radioBox1Choices[] = { wxT("RGB Color"), wxT("Infrared"), wxT("Switch every Frame") };
42  int m_radioBox1NChoices = sizeof( m_radioBox1Choices ) / sizeof( wxString );
43  m_radioBox1 = new wxRadioBox( this, wxID_ANY, wxT("Video Mode"), wxDefaultPosition, wxDefaultSize, m_radioBox1NChoices, m_radioBox1Choices, 1, wxRA_SPECIFY_COLS );
44  m_radioBox1->SetSelection( 0 );
45  bSizer1->Add( m_radioBox1, 0, wxALL, 5 );
46 
47  gSizer1->Add( bSizer1, 1, wxEXPAND, 5 );
48 
49  wxBoxSizer* bSizer4;
50  bSizer4 = new wxBoxSizer( wxVERTICAL );
51 
52  m_grid1 = new wxGrid( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
53 
54  // Grid
55  m_grid1->CreateGrid( 4, 1 );
56  m_grid1->EnableEditing( false );
57  m_grid1->EnableGridLines( true );
58  m_grid1->EnableDragGridSize( false );
59  m_grid1->SetMargins( 0, 0 );
60 
61  // Columns
62  m_grid1->EnableDragColMove( false );
63  m_grid1->EnableDragColSize( false );
64  m_grid1->SetColLabelSize( 30 );
65  m_grid1->SetColLabelValue( 0, wxT("Value") );
66  m_grid1->SetColLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE );
67 
68  // Rows
69  m_grid1->EnableDragRowSize( false );
70  m_grid1->SetRowLabelSize( 80 );
71  m_grid1->SetRowLabelValue( 0, wxT("tilt") );
72  m_grid1->SetRowLabelValue( 1, wxT("x") );
73  m_grid1->SetRowLabelValue( 2, wxT("y") );
74  m_grid1->SetRowLabelValue( 3, wxT("z") );
75  m_grid1->SetRowLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE );
76 
77  // Label Appearance
78  m_grid1->SetLabelBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNHIGHLIGHT ) );
79 
80  // Cell Defaults
81  m_grid1->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_TOP );
82  bSizer4->Add( m_grid1, 0, wxALL, 5 );
83 
84  gSizer1->Add( bSizer4, 1, wxEXPAND, 5 );
85 
86  wxBoxSizer* bSizer41;
87  bSizer41 = new wxBoxSizer( wxVERTICAL );
88 
89  m_button1 = new wxButton( this, wxID_ANY, wxT("Load Calibration xml"), wxDefaultPosition, wxDefaultSize, 0 );
90  bSizer41->Add( m_button1, 0, wxALL, 5 );
91 
92  m_button5 = new wxButton( this, wxID_ANY, wxT("Toggle Depth Stream"), wxDefaultPosition, wxDefaultSize, 0 );
93  bSizer41->Add( m_button5, 0, wxALL, 5 );
94 
95  m_button6 = new wxButton( this, wxID_ANY, wxT("Hide"), wxDefaultPosition, wxDefaultSize, 0 );
96  bSizer41->Add( m_button6, 0, wxALL, 5 );
97 
98  gSizer1->Add( bSizer41, 1, wxEXPAND, 5 );
99 
100  wxBoxSizer* bSizer3;
101  bSizer3 = new wxBoxSizer( wxVERTICAL );
102 
103  m_button2 = new wxButton( this, wxID_ANY, wxT("Up +5"), wxDefaultPosition, wxDefaultSize, 0 );
104  bSizer3->Add( m_button2, 0, wxALL, 5 );
105 
106  m_button3 = new wxButton( this, wxID_ANY, wxT("Level"), wxDefaultPosition, wxDefaultSize, 0 );
107  bSizer3->Add( m_button3, 0, wxALL, 5 );
108 
109  m_button4 = new wxButton( this, wxID_ANY, wxT("Down -5"), wxDefaultPosition, wxDefaultSize, 0 );
110  bSizer3->Add( m_button4, 0, wxALL, 5 );
111 
112  gSizer1->Add( bSizer3, 1, wxEXPAND, 5 );
113 
114  this->SetSizer( gSizer1 );
115  this->Layout();
116 
117  timer_ = new wxTimer(this, ID_Timer);
118 
119  // Connect Events
120  m_radioBox1->Connect( wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnSelectVideoMode ), NULL, this );
121  m_button1->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnLoadCalib ), NULL, this );
122  m_button5->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnToggleDepth ), NULL, this );
123  m_button6->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnHide ), NULL, this );
124  m_button2->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnUp ), NULL, this );
125  m_button3->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnLevel ), NULL, this );
126  m_button4->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnDown ), NULL, this );
127  this->Connect( ID_Timer, wxEVT_TIMER, wxTimerEventHandler( VideoSource_Controller_Kinect::OnTimer ) );
128 
129  timer_->Start(update_ms_, true);
130 }
131 
132 
133 
136  timer_->Stop();
137  // Disconnect Events
138  m_radioBox1->Disconnect( wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnSelectVideoMode ), NULL, this );
139  m_button1->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnLoadCalib ), NULL, this );
140  m_button5->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnToggleDepth ), NULL, this );
141  m_button6->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnHide ), NULL, this );
142  m_button2->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnUp ), NULL, this );
143  m_button3->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnLevel ), NULL, this );
144  m_button4->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VideoSource_Controller_Kinect::OnDown ), NULL, this );
145  this->Disconnect( wxEVT_TIMER, wxTimerEventHandler( VideoSource_Controller_Kinect::OnTimer ) );
146 }
147 
150  Cam_ = dynamic_cast<BIAS::VideoSource_Kinect_Base*>(cam);
151 }
152 
153 
155 OnHide(wxCommandEvent& event){
156  Show(false);
157  timer_->Stop();
158 }
159 
161 OnClose(wxCloseEvent& event) {
162  timer_->Stop();
163  Show(false);
164 }
165 
167 OnLoadCalib(wxCommandEvent& event) {
168 
169  wxFileDialog fileDialog(
170  this, // parent
171  wxT("Load internal calibration file for IR camera (BIAS Projection xml)"), // message
172  wxEmptyString, // default directory
173  wxEmptyString, // default file
174  wxT("Projection files (proj;xml)|*.proj;*.xml|All files (*.*)|*.*"), // wildcard
175  wxFD_OPEN); // style
176 
177  if (fileDialog.ShowModal() == wxID_OK) {
178  wxString wxStr = fileDialog.GetPath();
179  Projection p;
180  p.Load(WxToAscii(wxStr));
182  if (ppp == NULL) {
183  wxMessageDialog messageDialog(this, wxT("Sorry, no Projection Parameters found!"), wxT("ShowCamWx - Kinect Calibration file"), wxICON_ERROR | wxOK);
184  messageDialog.ShowModal();
185  }
186  double fx, fy, px, py;
187  ppp->GetFocalLength(fx);
188  fy = ppp->GetAspectratio() * fx;
189  ppp->GetPrincipal(px,py);
190  Cam_->CreateDepthLookupTable(px, py, fx, fy);
191  }
192 }
193 
195 OnSelectVideoMode( wxCommandEvent& event ) {
196  int id = event.GetInt();
197  switch (id) {
198  case 0: // rgb
200  break;
201  case 1: // rgb
202  Cam_->SetVideoModeIR();
203  break;
204  case 2: // rgb
206  break;
207  }
208 }
209 
211 OnUp( wxCommandEvent& event ) {
212  if (curTilt_ != -64)
213  Cam_->SetTilt(curTilt_+5.0);
214 }
216 OnLevel( wxCommandEvent& event ) {
217  Cam_->SetTilt(0.0);
218 }
220 OnDown( wxCommandEvent& event ) {
221  if (curTilt_ != -64)
222  Cam_->SetTilt(curTilt_-5.0);
223 }
225 OnToggleDepth( wxCommandEvent& event ) {
227 }
229 OnTimer( wxTimerEvent& event ) {
230  timer_->Stop();
231  double x,y,z;
232  if (Cam_->GetAccelerometer(curTilt_, x,y,z) != 0) {
233  // if cam does not support, do not reactivate timer
234  return;
235  }
236 
237  wxString tilts;
238  wxString xs;
239  wxString ys;
240  wxString zs;
241  if (curTilt_ == -64) {
242  tilts = AsciiToWx("moving");
243  } else {
244  tilts << curTilt_;
245  }
246  xs << x;
247  ys << y;
248  zs << z;
249 
250  m_grid1->SetCellValue(0,0,tilts);
251  m_grid1->SetCellValue(1,0,xs);
252  m_grid1->SetCellValue(2,0,ys);
253  m_grid1->SetCellValue(3,0,zs);
254 
255  timer_->Start(update_ms_, true);
256 }
257 
Defines a common interface to different devices.
virtual int CreateDepthLookupTable(double px, double py, double fx, double fy)=0
virtual int SetVideoModeIR()=0
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
virtual void OnToggleDepth(wxCommandEvent &event)
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual int GetPrincipal(double &PrincipalX, double &PrincipalY) const
Get principal point (in pixels relative to top left corner).
virtual int SetSwitchVideoModeAfterGrab()=0
virtual int GetAccelerometer(double &tilt, double &x, double &y, double &z)=0
virtual void OnLevel(wxCommandEvent &event)
virtual int ToggleDepthStream()=0
virtual int SetVideoModeColor()=0
virtual int SetTilt(double degrees)=0
virtual void OnDown(wxCommandEvent &event)
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
virtual double GetAspectratio() const
Return aspectratio (i.e.
void GetFocalLength(double &f) const
Get the current camera focal length.
virtual void OnSelectVideoMode(wxCommandEvent &event)
virtual void OnLoadCalib(wxCommandEvent &event)
virtual void OnHide(wxCommandEvent &event)
Use Kinect just like the ToF Cameras.
virtual void OnUp(wxCommandEvent &event)