Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasextractcheckerboardWX.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 #ifdef WIN32
26 #include <Base/Common/BIASpragmaStart.hh>
27 #endif
28 
29 #include <Gui/ImageCanvasCheckerboardExtract.hh>
30 #include <Base/Image/Image.hh>
31 #include <Base/Image/ImageIO.hh>
32 #include <Base/Image/ImageConvert.hh>
33 #include <FeatureDetector/CheckerboardDetector.hh>
34 #include <iostream>
35 #include <fstream>
36 #include <wx/wx.h>
37 #include <Utils/Param.hh>
38 
39 #ifndef WIN32
40 #include <unistd.h>
41 #else //WIN32
42 #include "Base/Common/getopt_W32.h"
43 #endif //WIN32
44 
45 using namespace std;
46 using namespace BIAS;
47 
48 #define PARAM_FILE_NAME "checkerboard.param"
49 
50 /**
51  * @file
52  * @ingroup g_tools
53  * @brief The tool tries to find checkerboards in the input images
54  * and writes these to the Matlab file 'Filename.m', additionally it offers the posssibility to select them manually,
55  * see biasextractcheckerboardWX.cpp
56  * @author ischiller 09/07
57 */
58 
59 /** \internal */
60 enum {
61  // standard ids
62  ID_About= wxID_ABOUT,
63  ID_Exit = wxID_EXIT,
64  ID_Next = wxID_HIGHEST+2051,
65  ID_Prev,
66  ID_Save,
67  ID_Help
68 };
69 
70 /** \internal */
71 class CheckerBoardExtractApp: public wxApp
72 {
73  virtual bool OnInit();
74 };
75 
76 /** \internal */
77 class CheckerBoardExtractFrame:public wxFrame, public BIAS::ICECCallbackInterface, public BIAS::Param
78 {
79 public:
80 
81  CheckerBoardExtractFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
82  virtual ~CheckerBoardExtractFrame();
83 
84  void Usage();
85  int Init(int argc, char* argv[]);
86  void WriteMatlab();
87  void CheckerboardSelected(std::vector<BIAS::HomgPoint2D> & corners, bool bOnlyFourPointsGiven);
88  void HandleImage();
89 
90  void OnNext(wxCommandEvent& event);
91  void OnPrev(wxCommandEvent& event);
92  void OnSave(wxCommandEvent& event);
93  void OnHelp(wxCommandEvent& event);
94  void ShowText(const string text, int slot);
95 
96 protected:
97  bool bImageDone_;
98  string MatlabFilename_;
99  int xCorners_;
100  int yCorners_;
101  bool bWriteDebug_;
102  std::vector<std::string> fileNames_;
103  vector<vector<BIAS::HomgPoint2D> > allCoords_;
105  unsigned int imageNumber_;
106  wxFlexGridSizer *Sizer_;
107 private:
108  DECLARE_EVENT_TABLE()
109 };
110 
111 ///////////////////////////////////////////////////////
112 //declaration
113 DECLARE_APP(CheckerBoardExtractApp)
114 
115 // implementation
116 IMPLEMENT_APP(CheckerBoardExtractApp )
117 
118 bool CheckerBoardExtractApp::OnInit()
119 {
120  CheckerBoardExtractFrame *frame_;
121  frame_ = new CheckerBoardExtractFrame(wxT("CheckerBoardExtract"), wxPoint(50,50),
122  wxSize(700,550) );
123 
124  // read params from file
125  int ret =0;
126  ret = frame_->Init(argc, WxToAsciiArray(argv, argc));
127  if(ret <0){
128  frame_->Usage();
129  exit(-1);
130  }
131  frame_->Show(TRUE);
132  frame_->HandleImage();
133  SetTopWindow(frame_);
134  return TRUE;
135 }
136 
137 ///////////////////////////////////////////////////////
138 CheckerBoardExtractFrame::
139 CheckerBoardExtractFrame(const wxString& title,const wxPoint& pos,const wxSize& size):
140  wxFrame((wxFrame *)NULL, -1, title, pos, size)
141 {
142  // Sizer and ImageCanvas
143  Sizer_ = new wxFlexGridSizer(1,2,0,0);
144  Sizer_->AddGrowableCol(0);
145  Sizer_->AddGrowableRow(0);
146 
147  ic_=new ImageCanvasCheckerboardExtract(this, NULL, 1,-1,
148  wxDefaultPosition, wxSize(640,480));
149 
150  wxSize buttonSize(65,30);
151  // create tool bar
152  wxToolBar* toolBar = CreateToolBar();
153  wxButton *pBNext= new wxButton(toolBar, ID_Next, wxT("Next"),wxDefaultPosition,buttonSize);
154  wxButton *pBPrev= new wxButton(toolBar, ID_Prev, wxT("Previous"),wxDefaultPosition,buttonSize);
155  wxButton *pBSave= new wxButton(toolBar, ID_Save, wxT("Save"),wxDefaultPosition,buttonSize);
156  wxButton *pBHelp= new wxButton(toolBar, ID_Help, wxT("Help"),wxDefaultPosition,buttonSize);
157 
158  toolBar->AddControl(pBPrev);
159  toolBar->AddControl(pBNext);
160  toolBar->AddControl(pBSave);
161  toolBar->AddControl(pBHelp);
162 
163  toolBar->Realize();
164  //finally add to sizer
165  Sizer_->Add(ic_,0,wxALL|wxEXPAND, 5);
166  SetSizer(Sizer_);
167  Layout();
168  SetMinSize(wxSize(700,550));
169  Fit();
170  CreateStatusBar(2);
171 }
172 
173 CheckerBoardExtractFrame::
174 ~CheckerBoardExtractFrame(){
175  if(ic_ != NULL)
176  delete ic_;
177 
178  if(Sizer_ != NULL)
179  delete Sizer_;
180 }
181 
182 void CheckerBoardExtractFrame::
183 OnNext(wxCommandEvent& event){
184  if(imageNumber_ < fileNames_.size()-1 && bImageDone_){
185  imageNumber_++;
186  HandleImage();
187  }
188  else if(imageNumber_ == fileNames_.size()-1){
189  string message ="Done! Now save the corners.";
190  wxMessageBox(AsciiToWx(message),
191  wxT("Detector"), wxOK , this);
192  ShowText(message,0);
193  }
194  else
195  ShowText("Select Corners!",0);
196 }
197 
198 void CheckerBoardExtractFrame::
199 OnPrev(wxCommandEvent& event){
200  if(imageNumber_ >=1 && bImageDone_){
201  imageNumber_--;
202  HandleImage();
203  }
204  else
205  ShowText("Select Corners!",0);
206 }
207 
208 void CheckerBoardExtractFrame::
209 OnSave(wxCommandEvent& event){
210  WriteMatlab();
211 }
212 
213 void CheckerBoardExtractFrame::
214 OnHelp(wxCommandEvent& event){
215  string message;
216  message = "This is the MIP checkerboard detector utility.\n It can be used in combination with Scaramuzza's OcamCalib.\n\nCorner Selection:\nIf the corners are not selected automatically\nselect them ROWISE, starting in the top left corner.\nAfter selecting all corners in one image press \"Next\".\n\nFinally save the corners, pressing \"Save\".";
217 
218  wxMessageBox(AsciiToWx(message),
219  wxT("Detector"), wxOK , this);
220 }
221 
222 
223 void CheckerBoardExtractFrame::
224 Usage()
225 {
226  cout <<"Usage: biasextractcheckerboardWX -f Filename.m -x xnum -y ynum -d image1 image2 ..."<<endl
227  <<endl;
228  cout <<" The tool tries to find checkerboards in the input images" << endl
229  <<" finds corners also in fisheye distorted images and " << endl
230  <<" writes these to the Matlab file 'Filename.m'." << endl
231  <<" xnum and ynum are the number of the checkerboards inner corners."
232  << endl
233  <<" -d writes debug images." << endl
234  <<" Do not use .mip images since Matlab is unable to read them." << endl
235  <<endl<<endl;
236 }
237 
238 
239 int CheckerBoardExtractFrame::
240 Init(int argc, char* argv[])
241 {
242  MatlabFilename_ = "ExtractedCorners.m";
243  xCorners_ = 7;
244  yCorners_ = 4;
245  bWriteDebug_ = false;
246  imageNumber_=0;
247 
248  int *xc = AddParamInt("xCorners",
249  "Number of Corners in x direction", 7);
250  int *yc = AddParamInt("yCorners",
251  "Number of Corners in y direction", 4);
252  string *imageList = AddParamString("ImageList","List with images","");
253  // update parameters from file with arguments from command line
254  if (argc > 1){
255  UpdateParameter(argc, argv, PARAM_FILE_NAME);
256  }
257  else
258  return -1;
259  Param::ParseListFile(*imageList,fileNames_);
260  allCoords_.resize(fileNames_.size());
261  xCorners_ = *xc;
262  yCorners_ = *yc;
263  ic_->SetPatternParameters( xCorners_, yCorners_);
264  imageNumber_=0;
265  return 0;
266 }
267 
268 void CheckerBoardExtractFrame::
269 HandleImage(){
270  BIAS::ImageBase image;
272 
273  if(BIAS::ImageIO::Load(fileNames_[imageNumber_],image)==0){
274 
275  if(image.GetStorageType() != ImageBase::ST_unsignedchar){
276  ImageConvert::ConvertST(image, imageUC, ImageBase::ST_unsignedchar);
277  }
278  else
279  imageUC = image;
280 
281  if(imageUC.GetColorModel() !=ImageBase::CM_Grey){
283  BIAS::ImageConvert::ToRGB(imageUC, dbImage2);
284  BIAS::ImageConvert::ToGrey(dbImage2,imageUC);
285  }
286 
287  ic_->Show(imageUC,"Select");
288  bImageDone_ = false;
289  ic_->SetPatternParameters (xCorners_,yCorners_);
290  ic_->SelectCheckerboardCorners(*this,true,false);
291  }
292 }
293 
294 void CheckerBoardExtractFrame::
295 CheckerboardSelected(std::vector<BIAS::HomgPoint2D> & corners, bool bOnlyFourPointsGiven){
296  if(bOnlyFourPointsGiven){
297  ShowText("Only four points, this is invalid!",0);
298  return;
299  }
300  // delete old corners
301  allCoords_[imageNumber_].clear();
302 
303  for(unsigned int k=0;k< corners.size();k++){
304  allCoords_[imageNumber_].push_back(corners[k]);
305  }
306  ShowText("Checkerboard selected!",0);
307  bImageDone_ = true;
308 }
309 
310 
311 void CheckerBoardExtractFrame::
312 WriteMatlab(){
313 
314  wxString fileName;
315  wxFileDialog dialog(this,wxT("Save Corners"),wxT(""),wxT(""),
316  wxT("Matlab (*.m)|*.m"),
317  wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
318  int ret = dialog.ShowModal();
319  if(ret == wxID_OK){
320  fileName = dialog.GetPath();
321 
322  ofstream ofs(WxToAscii(fileName));
323 
324  ofs << "numpoints = [" << xCorners_ << " " << yCorners_ << "];" << endl;
325 
326  ofs << "imagenames = {";
327  for (unsigned int i=0; i<fileNames_.size(); i++) {
328  ofs << "'" << fileNames_[i] << "' ";
329  }
330  ofs << "};" << endl;
331 
332  ofs << "xpos = [";
333  for (unsigned int i=0; i<allCoords_.size(); i++) {
334  for (unsigned int j=0; j<allCoords_[i].size(); j++) {
335  ofs << (allCoords_[i][j])[0] << " ";
336  }
337  ofs << ";" << endl;
338  }
339  ofs << "];" << endl;
340 
341  ofs << "ypos = [";
342  for (unsigned int i=0; i<allCoords_.size(); i++) {
343  for (unsigned int j=0; j<allCoords_[i].size(); j++) {
344  ofs << (allCoords_[i][j])[1] << " ";
345  }
346  ofs << ";" << endl;
347  }
348  ofs << "];" << endl;
349 
350  ofs.close();
351  }
352 
353 }
354 
355 void CheckerBoardExtractFrame::
356 ShowText(const string text, int slot){
357  if(GetStatusBar() != NULL)
358  GetStatusBar()->SetStatusText(AsciiToWx(text),slot);
359 }
360 
361 BEGIN_EVENT_TABLE(CheckerBoardExtractFrame, wxFrame)
362  EVT_BUTTON (ID_Next, CheckerBoardExtractFrame::OnNext)
363  EVT_BUTTON (ID_Prev, CheckerBoardExtractFrame::OnPrev)
364  EVT_BUTTON (ID_Save, CheckerBoardExtractFrame::OnSave)
365  EVT_BUTTON (ID_Help, CheckerBoardExtractFrame::OnHelp)
366 END_EVENT_TABLE()
367 
368 #ifdef WIN32
369 #include <Base/Common/BIASpragmaEnd.hh>
370 #endif
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
provides functionality for selection of checkerboard patterns
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
This class Param provides generic support for parameters.
Definition: Param.hh:231
callback interface for ImageCanvasCheckerBoardExtract
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
Class for converting an array of wxStrings to an array of non-const ASCII strings.
Definition: StringConv.hh:60
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey