1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* @file BallKeyPointExtractor.h
*
* Sorted list evaluated non-overlaping patches
*/

#ifndef _BallKeyPointExtractor_H_
#define _BallKeyPointExtractor_H_

#include <ModuleFramework/Module.h>

#include "Representations/Infrastructure/FieldInfo.h"
#include "Representations/Perception/MultiChannelIntegralImage.h"
#include "Representations/Perception/FieldPercept.h"
#include "Representations/Perception/CameraMatrix.h"
#include "Representations/Perception/BodyContour.h"
// tools
#include "BestPatchList.h"
#include "Tools/DoubleCamHelpers.h"
#include "Tools/CameraGeometry.h"
//#include <algorithm>

// debug
#include "Representations/Debug/Stopwatch.h"
#include "Tools/Debug/DebugRequest.h"
#include "Tools/Debug/DebugImageDrawings.h"
#include "Tools/Debug/DebugParameterList.h"
#include "Tools/Debug/DebugModify.h"
#include "Tools/Debug/DebugDrawings.h"

BEGIN_DECLARE_MODULE(BallKeyPointExtractor)
  PROVIDE(DebugRequest)
  PROVIDE(DebugImageDrawings)
  PROVIDE(DebugImageDrawingsTop)
  PROVIDE(DebugParameterList)
  PROVIDE(DebugDrawings)

  REQUIRE(FieldInfo) // needed for ball radius

  REQUIRE(Image)
  REQUIRE(ImageTop)

  REQUIRE(GameColorIntegralImage)
  REQUIRE(GameColorIntegralImageTop)

  REQUIRE(BallDetectorIntegralImage)
  REQUIRE(BallDetectorIntegralImageTop)

  REQUIRE(BodyContour)
  REQUIRE(BodyContourTop)

  REQUIRE(CameraMatrix)
  REQUIRE(CameraMatrixTop)

  REQUIRE(FieldPercept)
  REQUIRE(FieldPerceptTop)
END_DECLARE_MODULE(BallKeyPointExtractor)

class BallKeyPointExtractor : public BallKeyPointExtractorBase
{
public:
  virtual void execute(){} // dummy, do not use

  struct Parameter {
    double borderRadiusFactorClose;
    double borderRadiusFactorFar;

    double maxInnerGreenDensitiy;
  };

  BallKeyPointExtractor() : cameraID(CameraInfo::Bottom)<--- Member variable 'BallKeyPointExtractor::params' is not initialized in the constructor.<--- Member variable 'BallKeyPointExtractor::values' is not initialized in the constructor.
  {
    DEBUG_REQUEST_REGISTER("Vision:BallKeyPointExtractor:draw_value","", false);
  }

public:

  void calculateKeyPoints(BestPatchList& best) const {
    calculateKeyPoints(getGameColorIntegralImage(), best);
  }

  void calculateKeyPointsBetter(BestPatchList& best) const {
    //calculateKeyPoints(getBallDetectorIntegralImage(), best);
    calculateKeyPointsFast(getBallDetectorIntegralImage(), best);
    //calculateKeyPointsFull(getBallDetectorIntegralImage(), best);
  }

  // scan the integral image for white key points
  template<class ImageType>
  void calculateKeyPoints(const ImageType& integralImage, BestPatchList& best) const;

  template<class ImageType>
  void calculateKeyPointsFast(const ImageType& integralImage, BestPatchList& best) const;

  template<class ImageType>
  void calculateKeyPointsFull(const ImageType& integralImage, BestPatchList& best) const;

  BestPatchList::Patch refineKeyPoint(const BestPatchList::Patch& patch) const;

  void setParameter(const Parameter& params) {
    this->params = params;
  }

  void setCameraId(CameraInfo::CameraID id) {
    this->cameraID = id;
  }

private:

  void evaluatePatch(const GameColorIntegralImage& integralImage, BestPatchList& best, const Vector2i& point, const int size, const int border) const
  {
    int inner = integralImage.getSumForRect(point.x, point.y, point.x+size, point.y+size, 0);
    double greenBelow = integralImage.getDensityForRect(point.x, point.y+size, point.x+size, point.y+size+border, 1);

    if (inner*2 > size*size && greenBelow > 0.3)
    {
      int outer = integralImage.getSumForRect(point.x-border, point.y+size, point.x+size+border, point.y+size+border, 0);
      double value = (double)(inner - (outer - inner))/((double)(size+border)*(size+border));

      // scale the patch up to the image coordinates
      best.add( 
        (point.x-border)*integralImage.FACTOR, 
        (point.y-border)*integralImage.FACTOR, 
        (point.x+size+border)*integralImage.FACTOR, 
        (point.y+size+border)*integralImage.FACTOR, 
        value);
    }
  }

  void evaluatePatch(const BallDetectorIntegralImage& integralImage, BestPatchList& best, const Vector2i& point, const int size, const int border) const
  {
    int inner = integralImage.getSumForRect(point.x, point.y, point.x+size, point.y+size, 0);
    double greenBelow = integralImage.getDensityForRect(point.x, point.y+size, point.x+size, point.y+size+border, 1);
    double greeInner = integralImage.getDensityForRect(point.x, point.y, point.x+size, point.y+size, 1);

    if (inner*2 > size*size && greenBelow > 0.3 && greeInner <= params.maxInnerGreenDensitiy)
    {
      int outer = integralImage.getSumForRect(point.x-border, point.y+size, point.x+size+border, point.y+size+border, 0);
      double value = (double)(inner - (outer - inner))/((double)(size+border)*(size+border));

      // scale the patch up to the image coordinates
      best.add( 
        (point.x-border)*integralImage.FACTOR, 
        (point.y-border)*integralImage.FACTOR, 
        (point.x+size+border)*integralImage.FACTOR, 
        (point.y+size+border)*integralImage.FACTOR, 
        value);
    }
    /*
    int inner = integralImage.getSumForRect(point.x, point.y, point.x+size, point.y+size, 0);
    //double greenBelow = integralImage.getDensityForRect(point.x, point.y+size, point.x+size, point.y+size+border, 1);

    double greeInner = integralImage.getDensityForRect(point.x, point.y, point.x+size, point.y+size, 1);

    if (inner*2 > size*size && greeInner < 0.5) // && greenBelow > 0.3)
    {
      //int outer = integralImage.getSumForRect(point.x-border, point.y+size, point.x+size+border, point.y+size+border, 0);
      //double value = (double)(inner - (outer - inner))/((double)(size+border)*(size+border));
      double value = ((double)inner)/((double)(size)*(size));// (double)(inner - (outer - inner)) / ((double)(size + border)*(size + border));

      // scale the patch up to the image coordinates
      best.add( 
        (point.x-border)*integralImage.FACTOR, 
        (point.y-border)*integralImage.FACTOR, 
        (point.x+size+border)*integralImage.FACTOR, 
        (point.y+size+border)*integralImage.FACTOR, 
        value);
    }*/
  }

private:
  Parameter params;
  CameraInfo::CameraID cameraID;

  mutable double values[640/4][480/4][2];

  // double cam stuff
  DOUBLE_CAM_REQUIRE(BallKeyPointExtractor, Image);
  DOUBLE_CAM_REQUIRE(BallKeyPointExtractor, CameraMatrix);
  DOUBLE_CAM_REQUIRE(BallKeyPointExtractor, FieldPercept);
  DOUBLE_CAM_REQUIRE(BallKeyPointExtractor, BodyContour);

  DOUBLE_CAM_REQUIRE(BallKeyPointExtractor, GameColorIntegralImage);
  DOUBLE_CAM_REQUIRE(BallKeyPointExtractor, BallDetectorIntegralImage);
};


template<class ImageType>
void BallKeyPointExtractor::calculateKeyPoints(const ImageType& integralImage, BestPatchList& best) const
{
  //
  // STEP I: find the maximal height minY to be scanned in the image
  //
  if(!getFieldPercept().valid) {
    return;
  }

  // find the top point of the polygon
  int minY = getFieldPercept().getMinY();

  // double check: polygon is empty
  if(minY == (int)getImage().height() || minY < 0) {
    return;
  }

  // todo needs a better place
  const int32_t FACTOR = integralImage.FACTOR;

  Vector2i point;
  
  for(point.y = minY/FACTOR; point.y < (int)integralImage.getHeight(); ++point.y)
  {
    double estimatedRadius = CameraGeometry::estimatedBallRadius(
      getCameraMatrix(), getImage().cameraInfo, getFieldInfo().ballRadius,
      point.x*FACTOR, point.y*FACTOR);
    
    double radius = std::max( 6.0, estimatedRadius);
    int size   = (int)(radius*2.0/FACTOR+0.5);
    int border = (int)(radius*params.borderRadiusFactorClose/FACTOR+0.5);

    // HACK: different parameters depending on size
    if(size < 40/FACTOR) {
      border = (int)(radius*params.borderRadiusFactorFar/FACTOR+0.5);
    }
    border = std::max( 2, border);

    // smalest ball size == 3 => ball size == FACTOR*3 == 12
    if (point.y <= border || point.y+size+border >= (int)integralImage.getHeight()) {
      continue;
    }
    
    for(point.x = border; point.x+size+border < (int)integralImage.getWidth(); ++point.x)
    {
      evaluatePatch(integralImage, best, point, size, border);
    }
  }
}


template<class ImageType>
void BallKeyPointExtractor::calculateKeyPointsFast(const ImageType& integralImage, BestPatchList& best) const
{
  DEBUG_REQUEST("Vision:BallKeyPointExtractor:draw_value",
    CANVAS(((cameraID == CameraInfo::Top)?"ImageTop":"ImageBottom"));
  );

  //
  // STEP I: find the maximal height minY to be scanned in the image
  //
  if(!getFieldPercept().valid) {
    return;
  }

  // find the top point of the polygon
  int minY = getFieldPercept().getMinY();

  // double check: polygon is empty
  if(minY == (int)getImage().height() || minY < 0) {
    return;
  }

  // todo needs a better place
  const int32_t FACTOR = integralImage.FACTOR;

  Vector2i point;

  const int height = (int)integralImage.getHeight();
  const int width = (int)integralImage.getWidth();
  
  for(point.y = minY/FACTOR; point.y < height; ++point.y)
  {
    double estimatedRadius = CameraGeometry::estimatedBallRadius(
      getCameraMatrix(), getImage().cameraInfo, getFieldInfo().ballRadius,
      getImage().width()/2, point.y*FACTOR);
    
    // Note: we have a minimal allowed radius
    int radius = (int)(estimatedRadius / FACTOR + 0.5);
    // the minimal alowed radius
    if(radius < 2) {
      radius = 2;
    }
    
    // smalest ball size == 3 => ball size == FACTOR*3 == 12
    if (point.y < radius || point.y + radius >= height) {
      continue;
    }
    
    for(point.x = radius; point.x + radius < width; ++point.x)
    {
      //evaluatePatch(integralImage, best, point, size, border);

      if(cameraID == CameraInfo::Bottom && //point.y+radius >(int)(integralImage.getHeight()/2) &&
         getBodyContour().isOccupied(point.x*integralImage.FACTOR, (point.y+radius)*integralImage.FACTOR)) {
        continue;
      }
      
      const int innerOffset = radius/2;
      const int area = 4*radius*radius;

      int inner = integralImage.getSumForRect(point.x-radius, point.y-radius, point.x+radius, point.y+radius, 0);
      double greenInner = integralImage.getDensityForRect(point.x-innerOffset, point.y-innerOffset, point.x+innerOffset, point.y+innerOffset, 1);
      
      if (inner*2 > area && greenInner <= params.maxInnerGreenDensitiy)
      {
        double value = ((double)inner)/((double)(area));
        best.add( 
            (point.x-radius)*integralImage.FACTOR, 
            (point.y-radius)*integralImage.FACTOR, 
            (point.x+radius)*integralImage.FACTOR, 
            (point.y+radius)*integralImage.FACTOR, 
            value);
      }

      DEBUG_REQUEST("Vision:BallKeyPointExtractor:draw_value",
          double value = ((double)inner)/((double)(area));

          value = Math::clamp(value / 200.0, 0.0,1.0);
          PEN(Color(1.0,1.0-value,1.0-value,0.8),0.1);

          FILLBOX((point.x)*integralImage.FACTOR - integralImage.FACTOR/2, 
                  (point.y)*integralImage.FACTOR - integralImage.FACTOR/2, 
                  (point.x)*integralImage.FACTOR + integralImage.FACTOR/2, 
                  (point.y)*integralImage.FACTOR + integralImage.FACTOR/2);
      );
    }
  }
}


template<class ImageType>
void BallKeyPointExtractor::calculateKeyPointsFull(const ImageType& integralImage, BestPatchList& best) const
{
  DEBUG_REQUEST("Vision:BallKeyPointExtractor:draw_value",
    CANVAS(((cameraID == CameraInfo::Top)?"ImageTop":"ImageBottom"));
  );

  //
  // STEP I: find the maximal height minY to be scanned in the image
  //
  if(!getFieldPercept().valid) {
    return;
  }

  // find the top point of the polygon
  int minY = getFieldPercept().getMinY();

  // double check: polygon is empty
  if(minY == (int)getImage().height() || minY < 0) {
    return;
  }

  // todo needs a better place
  const int32_t FACTOR = integralImage.FACTOR;

  Vector2i point;

  // TODO: this has to be made more general
  for(int y = 1; y+1 < 480/4; ++y) {
    for(int x = 1; x+1 < 640/4; ++x) {
      values[x][y][0] = 0.0;
    }
  }
  
  // TODO: faster reset?
  //std::fill_n(&values[0][0][0], (480/4)*(640/4)*2, 0);
  
  for(point.y = minY/FACTOR; point.y < (int)integralImage.getHeight(); ++point.y)
  {
    double estimatedRadius = CameraGeometry::estimatedBallRadius(
      getCameraMatrix(), getImage().cameraInfo, getFieldInfo().ballRadius,
      getImage().width()/2, point.y*FACTOR);
    
    
    int radius = (int)(estimatedRadius / FACTOR + 0.5);

    // smalest ball size == 3 => ball size == FACTOR*3 == 12
    if (point.y < radius || point.y + radius >= (int)integralImage.getHeight()) {
      continue;
    }

    for(point.x = radius; point.x+radius < (int)integralImage.getWidth(); ++point.x)
    {
      //evaluatePatch(integralImage, best, point, size, border);

      if(cameraID == CameraInfo::Bottom && getBodyContour().isOccupied(point.x*integralImage.FACTOR, (point.y+radius)*integralImage.FACTOR)) {
        values[point.x][point.y][0] = 0.0;
        values[point.x][point.y][1] = radius;
        continue;
      }

      int inner = integralImage.getSumForRect(point.x-radius, point.y-radius, point.x+radius, point.y+radius, 0);
      double greeInner = integralImage.getDensityForRect(point.x-radius, point.y-radius, point.x+radius, point.y+radius, 1);
      const int size = radius*2;

      if (inner*2 > size*size && greeInner <= params.maxInnerGreenDensitiy)
      {
        double value = ((double)inner)/((double)(size)*(size));
        values[point.x][point.y][0] = value;
        values[point.x][point.y][1] = radius;
      }
      else 
      {
        values[point.x][point.y][0] = 0.0;
        values[point.x][point.y][1] = radius;
      }

      DEBUG_REQUEST("Vision:BallKeyPointExtractor:draw_value",
          double value = ((double)inner)/((double)(size*size));

          value = Math::clamp(value / 200.0, 0.0,1.0);
          PEN(Color(1.0,1.0-value,1.0-value,0.8),0.1);

          FILLBOX((point.x)*integralImage.FACTOR - integralImage.FACTOR/2, 
                  (point.y)*integralImage.FACTOR - integralImage.FACTOR/2, 
                  (point.x)*integralImage.FACTOR + integralImage.FACTOR/2, 
                  (point.y)*integralImage.FACTOR + integralImage.FACTOR/2);
      );
    }
  }


  for(int y = 1; y+1 < 480/4; ++y) {
    for(int x = 1; x+1 < 640/4; ++x) {
      if(values[x][y][0] > 0) {
        if( values[x][y][0] > values[x-1][y][0] && 
            values[x][y][0] > values[x+1][y][0] && 
            values[x][y][0] > values[x  ][y-1][0] && 
            values[x][y][0] > values[x  ][y+1][0] &&

            values[x][y][0] > values[x-1][y-1][0] && 
            values[x][y][0] > values[x+1][y-1][0] && 
            values[x][y][0] > values[x-1][y+1][0] && 
            values[x][y][0] > values[x+1][y+1][0])
        {
          double value = values[x][y][0];
          int rad = (int)values[x][y][1];
          best.add(
            (x-rad)*integralImage.FACTOR, 
            (y-rad)*integralImage.FACTOR, 
            (x+rad)*integralImage.FACTOR, 
            (y+rad)*integralImage.FACTOR, 
            value);
        }
      }
    }
  }
}

#endif // _BallKeyPointExtractor_H_-