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
imageprocessors.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 #ifndef __IMAGEPROCESSORS
25 #define __IMAGEPROCESSORS
26 
27 #include "imageprocessor.h"
28 
29 #include <selforg/stl_adds.h>
30 
31 #define MIN3(x,y,z) x<y ? (x<z ? x : z) : (y<z ? y : z)
32 #define MAX3(x,y,z) x>y ? (x>z ? x : z) : (y>z ? y : z)
33 
34 namespace lpzrobots {
35 
36  /** Standard image processor - convenience class for 1 to 1 image processing.
37  The last image of the stack is the source (output of last processor).
38  Simpler to implement than ImageProcessor.
39  @param show whether do display the resulting image on the screen
40  @param scale how to scale the display
41  @see ImageProcessor
42  */
44  StdImageProcessor(bool show, float scale) {
45  _dest.show = show;
46  _dest.scale = scale;
47  };
48  virtual ~StdImageProcessor() {
49  };
50 
51  /// overload this function and initialise the dest.img and the dest.name
52  virtual void initDestImage(Camera::CameraImage& dest, const Camera::CameraImage& src) = 0;
53 
54  /// overload this function and do processing here
55  virtual void process(const osg::Image* src, osg::Image* dest) = 0;
56 
57  /* substituded generic interface */
59  assert(imgs.size()>0);
60  _src = imgs.back();
61  // printf("source picture: %s, %i x %i\n",src.name.c_str(), src.img->s(), src.img->t());
62  _dest.img = new osg::Image;
63  initDestImage(_dest, _src);
64  return _dest;
65  }
66 
67  virtual void process(){
69  _dest.img->dirty();
70  }
71 
72  protected:
75  };
76 
77 
78 
79  /// black and white image @see StdImageProcessor
81  enum ChannelMask {Red = 1, Green = 2, Blue = 4, Hue = 1, Saturation = 2, Value = 4};
82 
83  /// @param channelmask which channels to consider, @see BWImageProcessor::ChannelMask
84  BWImageProcessor(bool show, float scale, char channelmask = 7)
85  : StdImageProcessor(show,scale), channelmask(channelmask) {}
86 
87  virtual ~BWImageProcessor() {}
88 
89  virtual void initDestImage(Camera::CameraImage& dest, const Camera::CameraImage& src){
90  dest.img->allocateImage(src.img->s(), src.img->t(), 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
91  dest.name = "bw(" + src.name + ")";
92  red = (channelmask & 1);
93  green = (channelmask & 2);
94  blue = (channelmask & 4);
96  printf("BWImageProcessor: Select: Red %i, Green %i, Blue %i : numchannels %i\n",
97  red, green, blue,numchannels);
98  if(numchannels==0) numchannels=1; // avoid division by 0
99  }
100 
101  virtual void process(const osg::Image* src, osg::Image* dest){
102  assert(src && src->getPixelFormat()==GL_RGB && src->getDataType()==GL_UNSIGNED_BYTE);
103  for(int r=0; r < src->t(); ++r) {
104  const unsigned char* sdata = src->data(0, r);
105  unsigned char* ddata = dest->data(0, r);
106  for(int c=0; c < src->s(); ++c) {
107  (*ddata) = (*(sdata)*red + *(sdata+1)*green + *(sdata+2)*blue)/numchannels;
108  sdata+=3;
109  ddata++;
110  }
111  }
112  }
113  bool red, green, blue;
116  };
117 
118  /** converts the image to a HSV coded image @see StdImageProcessor.
119  If this image is shown the h,s,v is displayed by r,g,b!
120  The h (hue) values are given by HSVImgProc::Colors
121  */
122 
123  struct HSVImgProc : public StdImageProcessor {
124  enum Colors {Red=0, Yellow=30, Green=60, Cyan=90,
125  Blue=120, Magenta=150, Red2=180, Gray=255, Span=30};
126 
127  HSVImgProc(bool show, float scale)
128  : StdImageProcessor(show,scale) {
129  }
130 
131  virtual ~HSVImgProc() {}
132 
133  virtual void initDestImage(Camera::CameraImage& dest, const Camera::CameraImage& src){
134  dest.img->allocateImage(src.img->s(), src.img->t(), 1, GL_RGB, GL_UNSIGNED_BYTE);
135  dest.name = "hsv(" + src.name + ")";
136  }
137 
138  virtual void process(const osg::Image* src, osg::Image* dest){
139  assert(src && src->getPixelFormat()==GL_RGB && src->getDataType()==GL_UNSIGNED_BYTE);
140  for(int r=0; r < src->t(); ++r) {
141  const unsigned char* sdata = src->data(0, r);
142  unsigned char* ddata = dest->data(0, r);
143  for(int c=0; c < src->s(); ++c) {
144  RGBtoHSV(*(sdata),*(sdata+1),*(sdata+2),
145  (*ddata), *(ddata+1), *(ddata+2));
146  sdata+=3;
147  ddata+=3;
148  }
149  }
150  }
151 
152  /** converts RGB to HSV color model;
153  r,g,b values are from 0 to 255;
154  h = [0,180]+255, s = [0,255], v = [0,255];
155  h is the standard hue value/2 (since 360 cannot be represented)
156  and 255 if undefined (gray)
157  */
158  void RGBtoHSV( unsigned char r, unsigned char g, unsigned char b,
159  unsigned char& h, unsigned char& s, unsigned char& v ) {
160  unsigned char min, max;
161  float delta;
162  float hue;
163  min = MIN3( r, g, b );
164  max = MAX3( r, g, b );
165  v = max; // v
166  delta = max - min;
167  if( max != 0 ){
168  s = (unsigned char)(255.0*delta / max); // s
169  }
170  if( max == 0 || delta == 0){
171  // r = g = b // s = 0, h is undefined
172  s = 0;
173  h = 255;
174  return;
175  }
176 
177  if( r == max )
178  hue = float( g - b ) / delta; // between yellow & magenta
179  else if( g == max )
180  hue = 2.0 + float( b - r ) / delta; // between cyan & yellow
181  else
182  hue = 4.0 + float( r - g ) / delta; // between magenta & cyan
183  hue *=30; // this is 60 in full range
184  if( hue < 0 )
185  hue += 180;
186  h = int(hue);
187  }
188  };
189 
190 
191  /** filters for a specific color (requires HSV, so use HSVImgProc before)
192  @param minhue minimal hue value to pass through @see HSVImgProc::Colors
193  @param maxhue maximal hue value to pass through @see HSVImgProc::Colors
194  @param satThreshold minimal saturation required to be considered as a color
195  @param valThreshold minimal "value" required to be considered as a color
196  @see HSVImgProc
197  @see StdImageProcessor
198  */
200  ColorFilterImgProc(bool show, float scale, int minhue, int maxhue,
201  int sat_threshold=100, int val_threshold=50)
202  : StdImageProcessor(show,scale),
203  minhue(minhue), maxhue(maxhue), sat_threshold(sat_threshold), val_threshold(val_threshold) {
204  }
205 
206  virtual ~ColorFilterImgProc() {}
207 
208  virtual void initDestImage(Camera::CameraImage& dest, const Camera::CameraImage& src){
209  dest.img->allocateImage(src.img->s(), src.img->t(), 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
210  // dest.img->allocateImage(16, 1, 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
211  dest.name = "spots(" + src.name + ")";
212  }
213 
214  virtual void process(const osg::Image* src, osg::Image* dest){
215  // actually we need HSV but there is no coding for it
216  assert(src && src->getPixelFormat()==GL_RGB && src->getDataType()==GL_UNSIGNED_BYTE);
217  for(int r=0; r < src->t(); ++r) {
218  const unsigned char* sdata = src->data(0, r);
219  unsigned char* ddata = dest->data(0, r);
220  for(int c=0; c < src->s(); ++c) {
221  if(*(sdata) >= minhue && *(sdata) < maxhue
222  && *(sdata+1) > sat_threshold && *(sdata+2) > val_threshold){
223  (*ddata) = *(sdata+2);
224  } else{
225  (*ddata) = 0;
226  }
227  sdata+=3;
228  ddata++;
229  }
230  }
231  }
232  int minhue;
233  int maxhue;
236  };
237 
238 
239 
240  /** creates a lightsensitive sensorline. It requires a black and white source,
241  e.g. provided by BWImageProcessor, ColorFilterImgProc
242  @param num number of segments of the sensor line
243  @param factor factor for average pixel value (rescaling)
244  @see StdImageProcessor
245  */
246  struct LineImgProc : public StdImageProcessor {
247  LineImgProc(bool show, float scale, int num, double factor = 20.0)
248  : StdImageProcessor(show,scale), num(num), factor(factor) {
249  }
250 
251  virtual ~LineImgProc() {}
252 
253  virtual void initDestImage(Camera::CameraImage& dest, const Camera::CameraImage& src){
254  dest.img->allocateImage(num, 1, 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
255  dest.name = "line(" + src.name + ")";
256  }
257 
258  virtual void process(const osg::Image* src, osg::Image* dest){
259  // actually we need HSV but there is no coding for it
260  assert(src && src->getPixelFormat()==GL_LUMINANCE && src->getDataType()==GL_UNSIGNED_BYTE);
261 
262  int w = src->s();
263  int h = src->t();
264  int size = w/num; // size of one segment
265  int numpixel_per_segm = size*h;
266  int segmentvalue;
267  unsigned char* destdata = dest->data();
268  for(int k=0; k<num; k++){
269  int sum = 0;
270  for(int j=0; j<h; j++){
271  const unsigned char* pixel = src->data(k*size, j);
272  for(int i=0; i< size; i++){
273  sum += *pixel;
274  pixel++;
275  }
276  }
277  segmentvalue = int((double)sum*factor/(double)numpixel_per_segm);
278  destdata[k] = std::min(segmentvalue,255);
279  }
280  }
281  int num;
282  double factor;
283  };
284 
285  /** time average of image @see StdImageProcessor.
286  */
287 
288  struct AvgImgProc : public StdImageProcessor {
289 
290  /// @param time length of averageing (time=1: no averaging)
291  AvgImgProc(bool show, float scale, int time)
292  : StdImageProcessor(show,scale), time(time) {
293  if(time<1) time =1;
294  factor = 1.0/(float)time;
295  }
296 
297  virtual ~AvgImgProc() {}
298 
299  virtual void initDestImage(Camera::CameraImage& dest, const Camera::CameraImage& src){
300  assert(src.img && src.img->getDataType()==GL_UNSIGNED_BYTE);
301  dest.img->allocateImage(src.img->s(), src.img->t(), 1, src.img->getPixelFormat(),
302  GL_UNSIGNED_BYTE);
303  dest.name = "avg(" + std::itos(time) + "," + src.name + ")";
304  }
305 
306  virtual void process(const osg::Image* src, osg::Image* dest){
307  for(int r=0; r < src->t(); ++r) {
308  const unsigned char* sdata = src->data(0, r);
309  unsigned char* ddata = dest->data(0, r);
310  for(unsigned int c=0; c < src->getRowSizeInBytes(); ++c) {
311  *ddata = (unsigned char)(((float)*sdata)*factor + ((float)*ddata)*(1-factor));
312  sdata++;
313  ddata++;
314  }
315  }
316  }
317 
318  int time;
319  float factor;
320  };
321 
322 
323 // /** Testing code
324 // */
325 // struct TestLineImgProc : public StdImageProcessor {
326 // TestLineImgProc(bool show, float scale, int num)
327 // : StdImageProcessor(show,scale), num(num) {
328 // }
329 
330 // virtual ~TestLineImgProc() {}
331 
332 // virtual void initDestImage(Camera::CameraImage& dest, const Camera::CameraImage& src){
333 // dest.img->allocateImage(src.img->s(), src.img->t(), 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
334 // dest.name = "testline(" + src.name + ")";
335 // }
336 
337 // virtual void process(const osg::Image* src, osg::Image* dest){
338 // // actually we need HSV but there is no coding for it
339 // assert(src && src->getPixelFormat()==GL_LUMINANCE && src->getDataType()==GL_UNSIGNED_BYTE);
340 
341 // int w = src->s();
342 // int h = src->t();
343 // int size = w/num; // size of one segment
344 // int numpixel_per_segm = size*h;
345 // for(int k=0; k<num; k++){
346 // int sum = 0;
347 // for(int j=0; j<h; j++){
348 // const unsigned char* pixel = src->data(k*size, j);
349 // unsigned char* destdata = dest->data(k*size, j);
350 // for(int i=0; i< size; i++){
351 // *destdata= (k*111+*pixel)%256;
352 // pixel++;
353 // destdata++;
354 // }
355 // }
356 // }
357 // }
358 // int num;
359 // };
360 
361 
362 
363 
364 }
365 
366 #endif
bool red
Definition: imageprocessors.h:113
virtual ~AvgImgProc()
Definition: imageprocessors.h:297
std::vector< CameraImage > CameraImages
Definition: camera.h:91
Definition: imageprocessors.h:81
BWImageProcessor(bool show, float scale, char channelmask=7)
Definition: imageprocessors.h:84
HSVImgProc(bool show, float scale)
Definition: imageprocessors.h:127
virtual void process(const osg::Image *src, osg::Image *dest)
overload this function and do processing here
Definition: imageprocessors.h:138
bool blue
Definition: imageprocessors.h:113
virtual void initDestImage(Camera::CameraImage &dest, const Camera::CameraImage &src)
overload this function and initialise the dest.img and the dest.name
Definition: imageprocessors.h:133
Definition: imageprocessors.h:124
StdImageProcessor(bool show, float scale)
Definition: imageprocessors.h:44
Definition: imageprocessors.h:81
Definition: imageprocessors.h:124
Colors
Definition: imageprocessors.h:124
Definition: imageprocessors.h:81
Definition: imageprocessors.h:125
converts the image to a HSV coded image
Definition: imageprocessors.h:123
filters for a specific color (requires HSV, so use HSVImgProc before)
Definition: imageprocessors.h:199
Definition: imageprocessors.h:124
virtual void process(const osg::Image *src, osg::Image *dest)
overload this function and do processing here
Definition: imageprocessors.h:258
virtual void initDestImage(Camera::CameraImage &dest, const Camera::CameraImage &src)
overload this function and initialise the dest.img and the dest.name
Definition: imageprocessors.h:89
Definition: imageprocessors.h:124
void RGBtoHSV(unsigned char r, unsigned char g, unsigned char b, unsigned char &h, unsigned char &s, unsigned char &v)
converts RGB to HSV color model; r,g,b values are from 0 to 255; h = [0,180]+255, s = [0...
Definition: imageprocessors.h:158
structure to store the image data and information for display
Definition: camera.h:80
virtual ~StdImageProcessor()
Definition: imageprocessors.h:48
virtual void initDestImage(Camera::CameraImage &dest, const Camera::CameraImage &src)
overload this function and initialise the dest.img and the dest.name
Definition: imageprocessors.h:299
Definition: imageprocessors.h:81
Definition: imageprocessors.h:81
virtual void process(const osg::Image *src, osg::Image *dest)
overload this function and do processing here
Definition: imageprocessors.h:306
virtual void initDestImage(Camera::CameraImage &dest, const Camera::CameraImage &src)
overload this function and initialise the dest.img and the dest.name
Definition: imageprocessors.h:208
virtual void process(const osg::Image *src, osg::Image *dest)
overload this function and do processing here
Definition: imageprocessors.h:101
int num
Definition: imageprocessors.h:281
float scale
scaling for display
Definition: camera.h:87
creates a lightsensitive sensorline.
Definition: imageprocessors.h:246
virtual void process()
perform the image calculation here
Definition: imageprocessors.h:67
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
Definition: imageprocessors.h:125
char numchannels
Definition: imageprocessors.h:114
int time
Definition: imageprocessors.h:318
Definition: imageprocessors.h:125
virtual void process(const osg::Image *src, osg::Image *dest)
overload this function and do processing here
Definition: imageprocessors.h:214
double g(double z)
neuron transfer function
Definition: regularisation.h:35
time average of image
Definition: imageprocessors.h:288
Definition: imageprocessors.h:125
Camera::CameraImage _dest
Definition: imageprocessors.h:73
int val_threshold
Definition: imageprocessors.h:235
virtual ~LineImgProc()
Definition: imageprocessors.h:251
virtual void initDestImage(Camera::CameraImage &dest, const Camera::CameraImage &src)=0
overload this function and initialise the dest.img and the dest.name
string itos(int i)
integer to string with default formating
Definition: stl_adds.cpp:30
#define MAX3(x, y, z)
Definition: imageprocessors.h:32
ChannelMask
Definition: imageprocessors.h:81
#define MIN3(x, y, z)
Definition: imageprocessors.h:31
Camera::CameraImage _src
Definition: imageprocessors.h:74
Base class for image processing units.
Definition: imageprocessor.h:39
std::string name
name of the image
Definition: camera.h:88
char channelmask
Definition: imageprocessors.h:115
black and white image
Definition: imageprocessors.h:80
virtual Camera::CameraImage init(const Camera::CameraImages &imgs)
initialization with all images so far.
Definition: imageprocessors.h:58
Definition: imageprocessors.h:81
AvgImgProc(bool show, float scale, int time)
Definition: imageprocessors.h:291
virtual void initDestImage(Camera::CameraImage &dest, const Camera::CameraImage &src)
overload this function and initialise the dest.img and the dest.name
Definition: imageprocessors.h:253
bool green
Definition: imageprocessors.h:113
Standard image processor - convenience class for 1 to 1 image processing.
Definition: imageprocessors.h:43
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
int maxhue
Definition: imageprocessors.h:233
osg::Image * img
Definition: camera.h:85
int minhue
Definition: imageprocessors.h:232
Definition: imageprocessors.h:125
bool show
whether to show the image on the screen
Definition: camera.h:86
double factor
Definition: imageprocessors.h:282
ColorFilterImgProc(bool show, float scale, int minhue, int maxhue, int sat_threshold=100, int val_threshold=50)
Definition: imageprocessors.h:200
virtual ~BWImageProcessor()
Definition: imageprocessors.h:87
virtual ~HSVImgProc()
Definition: imageprocessors.h:131
LineImgProc(bool show, float scale, int num, double factor=20.0)
Definition: imageprocessors.h:247
float factor
Definition: imageprocessors.h:319
int sat_threshold
Definition: imageprocessors.h:234
int c
Definition: hexapod.cpp:56
virtual ~ColorFilterImgProc()
Definition: imageprocessors.h:206