Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opticalflow.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 
25 #ifndef __OPTICALFLOW_H
26 #define __OPTICALFLOW_H
27 
28 #include "camerasensor.h"
29 #include <selforg/controller_misc.h>
30 
31 namespace lpzrobots {
32 
33  /// configuration object for OpticalFlow
34  struct OpticalFlowConf {
35  /// dimensions to return the flow (X means horizonal, Y vertical)
37  /** points to measure optical flow in normalized coordinates [-1,1]
38  however the points are placed sufficiently away from the border to have no
39  boundary effects.*/
40  std::list<Pos> points;
41  /** maximum fraction of the image dimension to consider for
42  a possible flow. Larger values increase the computational demand and move the
43  points more to the center (since we don't want edge effects)*/
44  double maxFlow;
45  /** size (edge length) of the measurement field (block) in pixel
46  (if 0 then 1/12th of width) */
47  int fieldSize;
48  /** verbosity level
49  (0: quite, 1: initialization values, 2: warnings, 3: info, 4: debug) */
50  int verbose;
51  };
52 
53  /** This CameraSensor calculates the optical flow at few points of the image
54  based on a box matching technique.
55  This can be applied directly to the camera image.
56  */
57  class OpticalFlow : public CameraSensor {
58  public:
59 
60  struct Vec2i {
61  Vec2i() : x(0), y(0) {}
62  Vec2i(int x, int y) : x(x), y(y) {}
63  int x;
64  int y;
65  Vec2i operator + (const Vec2i& v) const;
66  Vec2i operator * (int i) const;
67  Vec2i operator / (int i) const;
68  };
69 
70  typedef std::list< std::pair<Vec2i,int> > FlowDelList;
71 
72  /** @see CameraSensor for further parameter explanation.
73  */
75 
76  virtual ~OpticalFlow();
77 
78  /** calculates default positions for optical flow detection.
79  The points are in aranged horizontally in a line at the vertical center.
80  For num 2 the points are at the border,
81  3 points there is additioanlly one is the center and so on.
82  */
83  static std::list<Pos> getDefaultPoints(int num);
84 
85  /// the default config has 2 points in and calculates the flow in X and Y
88  c.dims = XY;
89  c.points = getDefaultPoints(2);
90  c.fieldSize = 24;
91  c.maxFlow = 0.15;
92  c.verbose = 1;
93  return c;
94  }
95 
96  virtual void intern_init();
97 
98  /// Performs the calculations
99  virtual bool sense(const GlobalData& globaldata);
100 
101  virtual int getSensorNumber() const {
102  return num;
103  };
104 
105  virtual int get(sensor* sensors, int length) const;
106 
107 
108  protected:
109  /**
110  compares a small part of two given images
111  and returns the average absolute difference.
112  Field center, size and shift have to be choosen,
113  so that no clipping is required!
114  \param field specifies position(center) of subimage to use for comparison
115  \param size specifies the size of the field edged in pixels
116  \param d_x shift in x direction
117  \param d_y shift in y direction
118  */
119  static double compareSubImg(const unsigned char* const I1,
120  const unsigned char* const I2,
121  const Vec2i& field, int size, int width, int height,
122  int bytesPerPixel, int d_x,int d_y);
123 
124  /** calculates the optimal transformation for one field in RGB
125  * using all three color channels
126  * @param minerror (is to return the minimum error achieved during the matching)
127  */
128  Vec2i calcFieldTransRGB(const Vec2i& field, const osg::Image* current,
129  const osg::Image* last, double& minerror) const;
130 
131  protected:
133  int num;
134  std::list<Vec2i> fields; // fields in image coordinates
136  osg::Image* lasts[4];
137  std::vector<Vec2i> oldFlows;
140  int width;
141  int height;
142  int cnt;
143  double avgerror; // average minimum matching error
144  };
145 
146 
147 }
148 
149 #endif
OpticalFlow(OpticalFlowConf conf=getDefaultConf())
Definition: opticalflow.cpp:42
std::list< std::pair< Vec2i, int > > FlowDelList
Definition: opticalflow.h:70
Definition: opticalflow.h:60
configuration object for OpticalFlow
Definition: opticalflow.h:34
Vec2i calcFieldTransRGB(const Vec2i &field, const osg::Image *current, const osg::Image *last, double &minerror) const
calculates the optimal transformation for one field in RGB using all three color channels ...
Definition: opticalflow.cpp:195
double avgerror
Definition: opticalflow.h:143
int maxShiftX
Definition: opticalflow.h:138
Vec2i operator*(int i) const
Definition: opticalflow.cpp:35
int maxShiftY
Definition: opticalflow.h:139
static OpticalFlowConf getDefaultConf()
the default config has 2 points in and calculates the flow in X and Y
Definition: opticalflow.h:86
double sensor
Definition: types.h:29
std::list< Pos > points
points to measure optical flow in normalized coordinates [-1,1] however the points are placed suffici...
Definition: opticalflow.h:40
double maxFlow
maximum fraction of the image dimension to consider for a possible flow.
Definition: opticalflow.h:44
int y
Definition: opticalflow.h:64
static std::list< Pos > getDefaultPoints(int num)
calculates default positions for optical flow detection.
Definition: opticalflow.cpp:59
int fieldSize
size (edge length) of the measurement field (block) in pixel (if 0 then 1/12th of width) ...
Definition: opticalflow.h:47
Vec2i operator/(int i) const
Definition: opticalflow.cpp:38
sensor * data
Definition: opticalflow.h:135
virtual void intern_init()
overload this function to initialized you data structures.
Definition: opticalflow.cpp:73
virtual ~OpticalFlow()
Definition: opticalflow.cpp:52
std::list< Vec2i > fields
Definition: opticalflow.h:134
Data structure holding all essential global information.
Definition: globaldata.h:57
int num
Definition: opticalflow.h:133
osg::Image * lasts[4]
Definition: opticalflow.h:136
int cnt
Definition: opticalflow.h:142
int width
Definition: opticalflow.h:140
Vec2i(int x, int y)
Definition: opticalflow.h:62
Class to connect a Camera as a sensor to a robot.
Definition: camerasensor.h:43
Definition: sensor.h:46
static double compareSubImg(const unsigned char *const I1, const unsigned char *const I2, const Vec2i &field, int size, int width, int height, int bytesPerPixel, int d_x, int d_y)
compares a small part of two given images and returns the average absolute difference.
Definition: opticalflow.cpp:168
OpticalFlowConf conf
Definition: opticalflow.h:132
int verbose
verbosity level (0: quite, 1: initialization values, 2: warnings, 3: info, 4: debug) ...
Definition: opticalflow.h:50
int height
Definition: opticalflow.h:141
This CameraSensor calculates the optical flow at few points of the image based on a box matching tech...
Definition: opticalflow.h:57
std::vector< Vec2i > oldFlows
Definition: opticalflow.h:137
Sensor::Dimensions dims
dimensions to return the flow (X means horizonal, Y vertical)
Definition: opticalflow.h:36
int x
Definition: opticalflow.h:63
Dimensions
defines which dimensions should be sensed. The meaning is sensor specific.
Definition: sensor.h:46
Vec2i operator+(const Vec2i &v) const
Definition: opticalflow.cpp:32
virtual bool sense(const GlobalData &globaldata)
Performs the calculations.
Definition: opticalflow.cpp:108
int c
Definition: hexapod.cpp:56
Vec2i()
Definition: opticalflow.h:61
virtual int getSensorNumber() const
overload this function and return the number of sensor values
Definition: opticalflow.h:101