ACT-CV - Machine Vision for Cognitive Modeling
main.cpp
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
196 #include <iostream>
197 #include <vector>
198 #include <algorithm>
199 #include <fstream>
200 #include <sstream>
201 
202 #include <config.h>
203 
204 #if HAVE_ICE
205 # include "IceComm/ActCvImpl.h"
206 #endif
207 
208 #include <color/RGBtoHSV.h>
209 #include <highgui.h>
210 
211 #include <mhexcptn.h>
212 #include <garbagecoll.h>
213 #include <GCString.h>
214 #include <statistiken.h>
215 
216 #include "Robot.h"
217 #include "oflow/OpticalFlow.h"
218 
219 #include "tests/TestFrameSource.h"
220 
221 #include "vision/RobotFrameSource.h"
223 #include "vision/ImageFrameSource.h"
224 #include "vision/QtFrameSource.h"
225 
226 #include "tcpserver/tcpserver.h"
227 
228 #include "FrameLoop.h"
229 #include "Options.h"
230 #include "config.h"
231 
232 #include <DynamicsHelpers.h>
233 
234 #include <stdlib.h>
235 #include <time.h>
236 
237 using namespace std;
238 
239 ostream& operator << (ostream &o, const struct HSV &hsv) {
240  o << hsv.h << " "
241  << static_cast<int>(hsv.s) << " "
242  << static_cast<int>(hsv.v);
243  return o;
244 }
245 
246 void PrintImageInfo(const IplImage *img) {
247  cout << "size: " << img->width << "x" << img->height << endl;
248  cout << "channels: " << img->nChannels << endl;
249  cout << "depth: " << img->depth << endl;
250 }
251 
252 vector<CvPoint> GetBigCorrPoints(const IplImage *img, double threshold=.999) {
253  vector<CvPoint> result;
254 
255  for (int x=0;x<img->width;x++) {
256  for (int y=0;y<img->height;y++) {
257  if (cvGetReal2D(img, y, x)>threshold) {
258  result.push_back(cvPoint(x,y));
259  }
260  }
261  }
262  return result;
263 }
264 
265 CvPoint GetRandomTarget(const IplImage *img) {
266  vector<CvPoint> points = GetBigCorrPoints(img);
267  if (points.size()==0) return cvPoint(0,0);
268 
269  return points[rand()%points.size()];
270 }
271 
272 
273 
274 void PrintBigCorrs(const IplImage *img) {
275  vector<CvPoint> points = GetBigCorrPoints(img);
276  for (unsigned int i=0;i<points.size();i++) {
277  cout << points[i] << endl;
278  }
279 }
280 
281 
282 #define MAX_FRAME_COUNT -1
283 
284 
285 void Usage(Options *options) {
286  cout << PACKAGE_STRING << endl
287  << "Usage: " << endl;
288 
289  for (unsigned int i=0;i<options->size();i++) {
290  options->at(i)->Describe(cout);
291  }
292 }
293 
297 struct BMFontInfo {
298  int code;
299  int bmId;
300  int pos[2];
301  int size[2];
302 };
303 
304 istream& operator >> (istream &in, BMFontInfo &bmfi) {
305  in >> bmfi.code
306  >> bmfi.bmId
307  >> bmfi.pos[0] >> bmfi.pos[1]
308  >> bmfi.size[0] >> bmfi.size[1];
309  return in;
310 }
311 
312 int main(int argc, char** argv) {
313  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
314  srand((unsigned) time(nullptr));
315 
316  cout << "number of threads: " << cvGetNumThreads() << endl;
317 
318  bool showWnd = true;
319 
320  SourceOption srcOpt;
321  AlgOption algOpt;
322  FpsOption fpsOpt;
323  ServerOption servOpt;
324 
325  Options options;
326  options.push_back(&srcOpt);
327  options.push_back(&algOpt);
328  options.push_back(&fpsOpt);
329  options.push_back(&servOpt);
330 
331  for (int i=1; i<argc; i++) {
332  options.Parse(argv[i]);
333  }
334 
335  ActCvObserver *visionObserver=0;
336 
337  if (servOpt.useServer) {
338 #if HAVE_ICE
339  IceServer* iceServer = IceServer::GetInstance();
340  cout << "IceServer started" << endl;
341  visionObserver = IceServer::GetInstance()->GetVisionObserver();
342  for (;;) {
343  // nothing
344  }
345 #else
346  cerr << "IceServer is not available. Please recompile" << endl;
347  return 1;
348 #endif
349  }
350 
351  if (srcOpt.source == SourceOption::s_none) {
352  Usage(&options);
353  return -1;
354  }
355 
357  switch (algOpt.algorithm) {
358  case a_match:
359  //frameLoop->AddObserver(new MatchObs("imgs/objects.txt"));
360  frameLoop->AddObserver(new MatchContourObs());
361  frameLoop->AddObserver(new FrameObsWindow("input video"));
362  break;
363  case a_oflow:
364  {
365  GCPointer<OFlowObs> flow = new OFlowObs();
366  frameLoop->AddObserver(&*flow);
367  if (visionObserver) flow->AddObserver(visionObserver);
368  }
369  break;
370  case a_lines:
371  frameLoop->AddObserver(new LineObs());
372  break;
373  case a_text:
374  frameLoop->AddObserver(new TextObs());
375  break;
376  case a_color:
377  //frameLoop->AddObserver(new ColorObs("hand.png"));
378  frameLoop->AddObserver(new DiffObs());
379  break;
380  default:
381 #if 0
382  break;
383 #else
384  Usage(&options);
385  return -1;
386 #endif
387  }
388 
389  if (fpsOpt.calcFps) {
390  frameLoop->AddObserver(new FpsFrameObs());
391  }
392 
393  cout << "Press any key to exit" << endl;
394 
395  switch (srcOpt.source) {
397  {
398 #if 1
401  frameLoop->LoopUntilKeyPress(&*qtFS);
402 #else
403  Robot robot;
404  RobotFrameSource robotFS(&robot);
405  robotFS.SetSize(0,50,
406  robot.GetScreenWidth()/2-1,robot.GetScreenHeight()/2-1);
407  frameLoop->LoopUntilKeyPress(&robotFS);
408 #endif
409  break;
410  }
412  {
413  // assume last option is the url
415  = QtFrameSource::QtSourceFactory(argv[argc-1]);
416  frameLoop->LoopUntilKeyPress(&*qtFS);
417  break;
418  }
419  case SourceOption::s_cam:
420  {
423  frameLoop->LoopUntilKeyPress(&*camFS);
424  break;
425  }
427  {
428  // assume last option is the file name
430  = OpenCVFrameSource::FileSourceFactory(argv[argc-1]);
431  frameLoop->LoopUntilKeyPress(&*fileFS);
432  break;
433  }
435  {
436  // assume last option is the file name
438  = ImageFrameSource::ImageSourceFactory(argv[argc-1]);
439  frameLoop->LoopUntilKeyPress(&*fileFS);
440  break;
441  }
443  {
444  TestFrameSource testFS(cvSize(400,300));
445  frameLoop->LoopUntilKeyPress(&testFS);
446  break;
447  }
449  {
450  //TestLineFrameSource testFS(cvSize(400,200));
451  TestLineFSStaticErr testFS(cvSize(400,200));
452  frameLoop->LoopUntilKeyPress(&testFS);
453  break;
454  }
455  }
456 
457 
458  MHDBGMSG(__FILE__,__LINE__,"Success - Bye");
459  return 0;
460 }


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)