Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMixtureOfGaussians.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 #include "ExampleMixtureOfGaussians.hh"
26 #include <Base/Image/ImageIO.hh>
27 #include <Base/Common/FileHandling.hh>
28 
29 
30 using namespace BIAS;
31 using namespace std;
32 
33 bool ExampleMixtureOfGaussians::OnInit()
34 {
35  // Open console for windows
37 
38 
39  // create main frame
41  mainFrame = new ExampleMixtureOfGaussiansFrame(AsciiToWx("Mixture of Gaussians"), wxPoint(50,50),
42  wxSize(400,340));
43 
44  // necessary for high resolution on small monitors (hackish...)
45  mainFrame->SetSizeHints(640,480,1800,1600);
46 
47  // initialize cameras and try to open them
48  std::vector<string> files,filesFL;
49  if(argc > 1){
50  Param::ParseListFile(WxToAscii(argv[1]),files);
51  mainFrame->InitCamera(&files);
52  if(argc > 2){
53  Param::ParseListFile(WxToAscii(argv[2]),filesFL);
54  mainFrame->InitCameraFL(&filesFL);
55  }
56  }
57  else
58  mainFrame->InitCamera();
59 
60  mainFrame->CreateMenu();
61 
62  // create main windows
63  mainFrame->Show(true);
64  SetTopWindow(mainFrame);
65 
66  return true;
67 }
68 
69 IMPLEMENT_APP(ExampleMixtureOfGaussians)
70 
71 
72 
73 BEGIN_EVENT_TABLE(ExampleMixtureOfGaussiansFrame, wxFrame)
74  EVT_MENU (ID_Quit, ExampleMixtureOfGaussiansFrame::OnQuit)
75  EVT_MENU (ID_Grab, ExampleMixtureOfGaussiansFrame::OnGrab)
76  EVT_MENU (ID_GrabContinuous, ExampleMixtureOfGaussiansFrame::OnGrabContinuous)
77  EVT_MENU (ID_Show1, ExampleMixtureOfGaussiansFrame::OnShowImage1)
78  EVT_MENU (ID_Show2, ExampleMixtureOfGaussiansFrame::OnShowImage2)
79  EVT_MENU (ID_Show3, ExampleMixtureOfGaussiansFrame::OnShowImage3)
80  EVT_MENU (ID_Show4, ExampleMixtureOfGaussiansFrame::OnShowImage4)
81  EVT_MENU (ID_Show5, ExampleMixtureOfGaussiansFrame::OnShowImage5)
82  EVT_MENU (ID_Show6, ExampleMixtureOfGaussiansFrame::OnShowImage6)
83  EVT_MENU (ID_Show7, ExampleMixtureOfGaussiansFrame::OnShowImage7)
84  EVT_MENU (ID_Show8, ExampleMixtureOfGaussiansFrame::OnShowImage8)
85  EVT_MENU (ID_Show9, ExampleMixtureOfGaussiansFrame::OnShowImage9)
86  EVT_MENU (ID_Show_FL, ExampleMixtureOfGaussiansFrame::OnShowFL)
87  EVT_MENU (ID_Controller, ExampleMixtureOfGaussiansFrame::OnShowController)
88  EVT_TIMER (ID_Timer, ExampleMixtureOfGaussiansFrame::OnTimer)
89 
90  EVT_KEY_DOWN (ExampleMixtureOfGaussiansFrame::OnKey)
91 END_EVENT_TABLE()
92 
93 
94 ExampleMixtureOfGaussiansFrame::
95 ExampleMixtureOfGaussiansFrame(const wxString& title,
96  const wxPoint& pos, const wxSize& size)
97  : wxFrame(NULL, -1, title, pos, size)
98 {
99 
100  // all parameters for MOG here, change if desired
101  mog_ = new MixtureOfGaussians<unsigned char>(2, 5, .2f,MOG_NRGB);
102  mog_->InitWeights(100000, 10, 2.9f );
103  mog_->SetSmoothing(false);
104  mogFL_ = new MixtureOfGaussians<float>(5, 2, .2f,MOG_RGB);
105  mogFL_->InitWeights(1000000, 0, 4.9f );
106  mogFL_->SetSmoothing(false);
107  CameraFL_= NULL;
108  Camera_= NULL;
109  bShowImageFL_=false;
110  roi_.UL[0] = 0;
111  roi_.UL[1] = 0;
112  roi_.LR[0] = 00;
113  roi_.LR[1] = 00;
114  downsample_ = false;
115 
116  // create status bar and add status text
117  CreateStatusBar(3);
118  wxString text;
119  text.Printf(wxT("size: %4dx%4d"),0, 0);
120  SetStatusText(text, 2);
121 
122  // init image canvas
123  IC_ = new ImageCanvas(this,GetStatusBar(), 0,-1,wxDefaultPosition,
124  wxSize(1024,768));
125 
126  // add image canvas to main window
127  wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
128  vbox->Add(IC_,1, wxEXPAND |wxALL |wxFIXED_MINSIZE , 5);
129  vbox->SetMinSize(640,480);
130  SetSizer(vbox);
131  SetAutoLayout(true);
132  Layout();
133 
134  // init and start timer
135  Timer_.SetOwner(this, ID_Timer);
136  Timer_.Start(50,true);
137  StopWatch_.Start();
138  ShowImage_ = &CamImg_;
139  dCurImage_ = 0;
140 
141  guardimagemutex = new pthread_mutex_t;
142  guarddiffimagemutex = new pthread_mutex_t;
143  pthread_mutex_init(guardimagemutex, NULL);
144  pthread_mutex_init(guarddiffimagemutex, NULL);
145  barrier = new pthread_barrier_t;
146  pthread_barrier_init(barrier, NULL, 2);
147 
148 }
149 
150 void ExampleMixtureOfGaussiansFrame::InitCamera(std::vector<std::string>* fileNames) {
151  if(fileNames != NULL && fileNames->size() > 0){
152  Camera_ = new VideoSource_Disk;
153  // std::vector<string> files;
154  // Param::ParseListFile("/data/projects/Dyn3D/DiplomFalkoKellner/MOG-Pictures-Fisheye/MOGin.lst",files);
155  // Param::ParseListFile("/data/projects/Dyn3D/MixedReality/SIGGRAPH/Scenes2/Scene06/lists/Scene06-Tex-undist-sync.lst",files);
156 
157  Camera_->OpenDevice(*fileNames);
158  ((VideoSource_Disk*)Camera_)->SetLoopMode(true);
159  }
160  else{
161  Camera_ = new VideoSource_DCAM();
162  dynamic_cast<VideoSource_DCAM*>(Camera_)->SetFirewireB(false);
163  if(Camera_->OpenDevice() != 0){
164  delete Camera_;
165  BIASERR("No DCAM present!");
166  return;
167  }
168  }
169 
170  Camera_->InitImage(CamImgOrg_);
171  CamImgOrgRGB_.Init(CamImgOrg_.GetWidth(), CamImgOrg_.GetHeight(),3);
172  Camera_->PreGrab();
173 
174  pthread_create(&grabthread, NULL, grabImages_, this);
175  pthread_create(&processthread, NULL, processImages_, this);
176 
177 }
178 
179 void ExampleMixtureOfGaussiansFrame::InitCameraFL(std::vector<std::string>* fileNames) {
180  if(fileNames != NULL && fileNames->size() > 0){
181  CameraFL_ = new VideoSource_Disk;
182  CameraFL_->OpenDevice(*fileNames);
183  ((VideoSource_Disk*)CameraFL_)->SetLoopMode(true);
184  }
185  else{
186  BIASERR("No way!");
187  }
188 
189  CameraFL_->PreGrab();
190 }
191 
193  // create file menu
194  wxMenu *menuFile = new wxMenu;
195  menuFile->Append(ID_Quit, wxT("E&xit") );
196 
197  // create menu for showing different cameras
198  wxMenu *menuShow = new wxMenu;
199  menuShow->Append(ID_Show1, wxT("Original Image") );
200  menuShow->Append(ID_Show2, wxT("Normalized Image") );
201  menuShow->Append(ID_Show3, wxT("Weight Image") );
202  menuShow->Append(ID_Show4, wxT("Difference Image") );
203  menuShow->Append(ID_Show5, wxT("Visualization") );
204  menuShow->AppendSeparator();
205  if(CameraFL_ != NULL){
206  pCheckItemFL_=
207  menuShow->AppendCheckItem(ID_Show_FL ,
208  wxT("&Show float images"),
209  wxT(""));
210  pCheckItemFL_->Check(false);
211  }
212  menuShow->Append(ID_Controller, wxT("Controller") );
213 
214  // add menues to menu bar
215  wxMenuBar *menuBar = new wxMenuBar;
216  menuBar->Append(menuFile, wxT("&File"));
217  menuBar->Append(menuShow, wxT("&Show"));
218  SetMenuBar(menuBar);
219 
220  // fit all components to size of window
221  Fit();
222 }
223 
224 void ExampleMixtureOfGaussiansFrame::OnQuit(wxCommandEvent& event) {
225  Timer_.Stop();
226  Close(true);
227 }
228 
229 void ExampleMixtureOfGaussiansFrame::OnGrab(wxCommandEvent& event) {}
231 void ExampleMixtureOfGaussiansFrame::OnShowImage1(wxCommandEvent& event) {
232  dCurImage_ = 0;
233 }
234 void ExampleMixtureOfGaussiansFrame::OnShowImage2(wxCommandEvent& event) {
235  dCurImage_ = 1;
236 }
237 void ExampleMixtureOfGaussiansFrame::OnShowImage3(wxCommandEvent& event) {
238  dCurImage_ = 2;
239 }
240 void ExampleMixtureOfGaussiansFrame::OnShowImage4(wxCommandEvent& event) {
241  dCurImage_ = 3;
242 }
243 void ExampleMixtureOfGaussiansFrame::OnShowImage5(wxCommandEvent& event) {
244  dCurImage_ = 4;
245 }
246 
247 void ExampleMixtureOfGaussiansFrame::OnShowFL(wxCommandEvent& event) {
248  bShowImageFL_ = event.IsChecked();
249 }
250 
251 void ExampleMixtureOfGaussiansFrame::OnShowImage6(wxCommandEvent& event) {}
252 void ExampleMixtureOfGaussiansFrame::OnShowImage7(wxCommandEvent& event) {}
253 void ExampleMixtureOfGaussiansFrame::OnShowImage8(wxCommandEvent& event) {}
254 void ExampleMixtureOfGaussiansFrame::OnShowImage9(wxCommandEvent& event) {}
256 
258  ExampleMixtureOfGaussiansFrame *obj = static_cast<ExampleMixtureOfGaussiansFrame*>(thisvisu);
259  Image<unsigned char> bufferImage(obj->CamImgOrg_.GetWidth(), obj->CamImgOrg_.GetHeight(), 3);
260  do {
261  obj->Camera_->GrabSingle(obj->CamImgOrg_);
263  //obj->CamImgOrg_.SetColorModel(ImageBase::CM_Bayer_RGGB);
264  ImageConvert::ToRGB(obj->CamImgOrg_, bufferImage);
265  } else {
266  bufferImage = obj->CamImgOrg_;
267  }
268  pthread_mutex_lock(obj->guardimagemutex);
269  obj->CamImgOrgRGB_.CopyIn_NoInit(bufferImage.GetImageData());
270  pthread_mutex_unlock(obj->guardimagemutex);
271  pthread_barrier_wait(obj->barrier);
272  } while (!obj->stopRequest);
273  return NULL;
274 }
275 
277  ExampleMixtureOfGaussiansFrame *obj = static_cast<ExampleMixtureOfGaussiansFrame*>(thisvisu);
278  Image<unsigned char> bufferImage(obj->CamImgOrg_.GetWidth(), obj->CamImgOrg_.GetHeight(), 3);;
279  do {
280  pthread_barrier_wait(obj->barrier);
281  pthread_mutex_lock(obj->guardimagemutex);
282  bufferImage.CopyIn_NoInit(obj->CamImgOrgRGB_.GetImageData());
283  pthread_mutex_unlock(obj->guardimagemutex);
284  pthread_mutex_lock(obj->guarddiffimagemutex);
285  bufferImage.SetROI(5,5,bufferImage.GetWidth()-5,bufferImage.GetHeight()-5);
286  obj->mog_->Apply(bufferImage);
287  pthread_mutex_unlock(obj->guarddiffimagemutex);
288  } while (!obj->stopRequest);
289  return NULL;
290 }
291 
292 
293 void ExampleMixtureOfGaussiansFrame::OnTimer(wxTimerEvent& event) {
294  // update frames per second and restart timer and counters
295  Timer_.Stop();
296  StopWatch_.Stop();
297  double fps = 1000000.0 / StopWatch_.GetRealTime();
298  StopWatch_.Reset();
299  StopWatch_.Start();
300  FrameCounter_++;
301  wxString text;
302  text.Printf(wxT("%05d %2.1f fps "),FrameCounter_, fps);
303  SetStatusText(text, 2);
304 
305  pthread_mutex_lock(guardimagemutex);
306  CamImg_ = CamImgOrgRGB_;
307  pthread_mutex_unlock(guardimagemutex);
308 
309  unsigned int w,h;
310  // get width and height from image
311  w = CamImg_.GetWidth();
312  h = CamImg_.GetHeight();
313 
314  text.Printf(wxT("size: %4dx%4d"),w, h);
315  SetStatusText(text, 1);
316 
317  // set new height or width if changed
318  if (w != Width_ || h != Height_)
319  {
320  Width_ = w;
321  Height_ = h;
322  IC_->SetSize(w+50,h+80);
323  SetMinSize(wxSize(w+50,h+80));
324  Fit();
325  }
326 
328 
329  //FLOAT
330  if(CameraFL_ != NULL){
331  CameraFL_->GrabSingle(CamImgOrgFL_);
332  if (downsample_) {
333  if (CamImgRescaledFL_.IsEmpty())
334  CamImgRescaledFL_.Init(CamImgOrgFL_.GetWidth()/2,CamImgOrgFL_.GetHeight()/2,1);
335  Rescale<float,float> pRescaleFilter;
336  pRescaleFilter.Downsample (CamImgOrgFL_,CamImgFL_,2);
337 
338  }
339  else
340  CamImgFL_ = CamImgOrgFL_;
341 
342  w = CamImgFL_.GetWidth();
343  h = CamImgFL_.GetHeight();
344  CamImgFL_.SetROI(15,15,w-15,h-15);
345 
346  mogFL_->Apply(CamImgFL_);
347  }
348 
349  static int n=0;
350  n++;
351  if (n>100) {
352  mog_->AllowCreationOfNewDistributions(false);
353  mog_->SetUpdateFactor(0.0f);
354  mog_->EnableCleanup(true, true, false);
355  }
356  switch (dCurImage_) {
357 
358  case 0:
359  if(bShowImageFL_) {
360  CamImgFL_.ScaleShiftBetween(0,255);
362  ShowImage_ = &tmpUC;
363  }
364  else ShowImage_ = &CamImg_;
365  break;
366  case 1:
367  if(bShowImageFL_) {
368  mogFL_->GetNormalizedImage(tmp);
369  }else{
370  mog_->GetNormalizedImage(tmp);
371  }
372  tmp.ScaleShiftBetween(0,255);
373  BIAS::ImageConvert::ConvertST(tmp, CamImgNormalizedChar_, ImageBase::ST_unsignedchar);
374  ShowImage_ = &CamImgNormalizedChar_;
375  break;
376  case 2:
377  if(bShowImageFL_)
378  mogFL_->GetWeightImage(tmp);
379  else
380 // mog_->GetWeightImage(tmp);
381  mog_->GetMatchValueImage(tmp);
382  ImageIO::Save("bla-"+FileHandling::LeadingZeroString(n,5)+".mip", tmp);
383  tmp.ScaleShiftBetween(0,255);
385  ShowImage_ = &WeightImgChar_;
386  break;
387  case 3:
388  if(bShowImageFL_)
389  mogFL_->GetDifferenceImage(DifferenceImgChar_);
390  else
391  mog_->GetDifferenceImage(DifferenceImgChar_);
392  ShowImage_ = &DifferenceImgChar_;
393  break;
394  case 4:
395  if(bShowImageFL_)
396  mogFL_->GetDifferenceImageWithVisuals(DifferenceImgCharRGB_);
397  else
398  mog_->GetDifferenceImageWithVisuals(DifferenceImgCharRGB_);
399  ShowImage_ = &DifferenceImgCharRGB_;
400 // ImageIO::Save("MOG-visu"+BIAS::LeadingZeroString(n,3)+".mip", DifferenceImgCharRGB_);
401  break;
402  default:
403  ShowImage_ = &CamImg_;
404  break;
405  }
406 
407  // show actual camera image on main frame
408  IC_->Show(*ShowImage_, "Camera Image");
409 
410  Timer_.Start(30, true);
411 }
412 
413 void ExampleMixtureOfGaussiansFrame::OnKey(wxKeyEvent& event) {
414 
415 }
void BIASGui_EXPORT ConsoleRedirectIO(std::string name=std::string(""))
Extra Console for WIN32 WinMain gui applications.
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
void CopyIn_NoInit(void *data)
Take some data and fill it into the Image.
Definition: ImageBase.cpp:827
int ScaleShiftBetween(double Min, double Max)
scales and shifts image so afterwards every pixel has a value between Min and Max ...
Definition: Image.cpp:1118
display image in wx application, provides zoom and investigation functionality
Definition: ImageCanvas.hh:38
This class VideoSource_DCAM implements access to IEEE1394 (Firewire, iLink) cameras following the DCa...
Down-, Upsampling routines and Resize.
Definition: Rescale.hh:71
int Downsample(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
generic downsample function.
Definition: Rescale.cpp:111
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int InitWeights(const int maxWeight, const int minWeight, const float errorMargin)
init weights for background/object distinction
virtual int OpenDevice(std::string Prefix, std::string Postfix, int DigitCount, int PictureCount, int StartCount=0)
this opens all images given by prefix, postfix, Digitcount and PictureCount e.g.
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
static std::string LeadingZeroString(const int &n, const unsigned int &digits=DEFAULT_LEADING_ZEROS)
Create a string with leading zeroes from number.
unsigned int GetHeight() const
Definition: ImageBase.hh:319
static int ParseListFile(const std::string &ListFileName, std::vector< std::string > &LinesInFile)
Extracts lines from passed file.
Definition: Param.cpp:1853
BIAS::MixtureOfGaussians< unsigned char > * mog_
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
int SetROI(unsigned int UpperLeftX, unsigned int UpperLeftY, unsigned int LowerRightX, unsigned int LowerRightY)
deprecated, use SetROICorners()
Definition: ImageBase.cpp:1033
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
virtual int GrabSingle(BIAS::Camera< unsigned char > &image)
void CreateMenu()
Creates the menu of the main window.
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
int Apply(BIAS::Image< StorageType > &in)
update calculations with new image the image has to be formatted correctly, e.g for MoG on HSL images...
void InitCameraFL(std::vector< std::string > *fileNames=NULL)
void InitCamera(std::vector< std::string > *fileNames=NULL)
Initialize cameras with param file.
This class simulates a video device by loading images from disk.