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
/**
* @file GoalCrossBarDetector.h
*
* @author <a href="mailto:mellmann@informatik.hu-berlin.de">Heinrich Mellmann</a>
* @author <a href="mailto:critter@informatik.hu-berlin.de">CNR</a>
* Definition of class GoalCrossBarDetector
*/

#ifndef GOALCROSSBARDETECTOR_H
#define GOALCROSSBARDETECTOR_H

#include <ModuleFramework/Module.h>

#include <Representations/Infrastructure/FieldInfo.h>
#include <Representations/Infrastructure/CameraInfo.h>
#include <Representations/Infrastructure/Image.h>
#include "Representations/Perception/ArtificialHorizon.h"
#include "Representations/Perception/GoalPercept.h"
#include "Representations/Perception/GoalCrossBarPercept.h"
#include "Representations/Perception/CameraMatrix.h"
#include "Tools/ImageProcessing/GoalBarFeature.h"

// tools
#include "Tools/DoubleCamHelpers.h"
#include <Tools/DataStructures/RingBuffer.h>
#include <Tools/DataStructures/RingBufferWithSum.h>

#include "Tools/naoth_opencv.h"
#include "Tools/ImageProcessing/Edgel.h"
#include "Tools/ImageProcessing/Filter.h"
#include "Tools/ImageProcessing/MaximumScan.h"

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

BEGIN_DECLARE_MODULE(GoalCrossBarDetector)
  PROVIDE(StopwatchManager)
  PROVIDE(DebugRequest)
  PROVIDE(DebugModify)
  PROVIDE(DebugImageDrawings)
  PROVIDE(DebugImageDrawingsTop)
  PROVIDE(DebugParameterList)

  REQUIRE(FieldInfo)

  REQUIRE(CameraInfo)
  REQUIRE(CameraInfoTop)
  REQUIRE(Image)
  REQUIRE(ImageTop)
  REQUIRE(CameraMatrix)
  REQUIRE(CameraMatrixTop)
  REQUIRE(ArtificialHorizon)
  REQUIRE(ArtificialHorizonTop)

  REQUIRE(GoalPercept)
  REQUIRE(GoalPerceptTop)

  PROVIDE(GoalCrossBarPercept)
  PROVIDE(GoalCrossBarPerceptTop)
END_DECLARE_MODULE(GoalCrossBarDetector)


class GoalCrossBarDetector: private GoalCrossBarDetectorBase
{
public:

  GoalCrossBarDetector();
  virtual ~GoalCrossBarDetector();

  // override the Module execute method
  bool execute(CameraInfo::CameraID id);

  virtual void execute()
  {
    execute(CameraInfo::Top);
    execute(CameraInfo::Bottom);
  }

  Vector2d calculateGradientUV(const Vector2i& point) const;
  Vector2d calculateGradientY(const Vector2i& point) const;

private:
  static const int imageBorderOffset = 5;
  CameraInfo::CameraID cameraID;

  class Parameters: public ParameterList
  {
  public:
    Parameters() : ParameterList("GoalCrossBarParameters")
    {
      //PARAMETER_REGISTER(numberOfScanlines) = 9;
      PARAMETER_REGISTER(scanlinesDistance) = 20;
      PARAMETER_REGISTER(minGoodPoints) = 3;

      PARAMETER_REGISTER(detectWhiteGoals) = true;
      PARAMETER_REGISTER(useColorFeatures) = false;
      PARAMETER_REGISTER(threshold) = 140;
      PARAMETER_REGISTER(thresholdGradient) = 40;
      PARAMETER_REGISTER(maxFeatureWidth) = 213;
      PARAMETER_REGISTER(thresholdFeatureGradient) = 0.5;
      PARAMETER_REGISTER(barWidthSimilarity) = 0.25;
      PARAMETER_REGISTER(thresholdFeatureSimilarity) = 0.8;
      PARAMETER_REGISTER(maxGoalWidthRatio) = 1.5;

      syncWithConfig();
    }

    virtual ~Parameters() {
    }

    //int numberOfScanlines;
    int scanlinesDistance;
    int minGoodPoints;

    bool detectWhiteGoals;
    //bool usePrewitt;
    bool useColorFeatures;
    int threshold;
    int thresholdGradient;
    int maxFeatureWidth;

    double thresholdFeatureGradient;
    double barWidthSimilarity;
    double thresholdFeatureSimilarity;
    double maxGoalWidthRatio;
  };

  Parameters params;

  struct Goal
  {
    Goal() : post0(0), post1(0) {}

    int post0;
    int post1;
  };

  class Cluster 
  {
  private:
    std::vector<cv::Point2f> points;
    std::vector<GoalBarFeature> features;
    double summedWidths;

  public:
    Cluster() : summedWidths(0)
    {}

    Math::Line getLine() const
    {
      ASSERT(points.size() >= 2);

      // estimate the line
      cv::Vec4f line;
      cv::fitLine(cv::Mat(points), line, CV_DIST_L2, 0, 0.01, 0.01);

      Vector2d x(line[2], line[3]);
      Vector2d v(line[0], line[1]);
  
      return Math::Line(x, v);
    }

    void add(const GoalBarFeature& postFeature) {
      features.push_back(postFeature);
      points.push_back(cv::Point2f((float)postFeature.point.x, (float)postFeature.point.y));
      summedWidths += postFeature.width;
    }

    double getFeatureWidth()
    {
      return summedWidths / static_cast<double>(features.size());
    }

    int size() const {
      return (int)points.size();
    }

    double sim(const GoalBarFeature& postFeature) const {
      double max_sim = 0;
      for(size_t i = 0; i < features.size(); i++) {
        double s = postFeature.sim(features[i]);
        if(s > max_sim) {
          max_sim = s;
        }
      }
      return max_sim;
    }
  };

  std::vector<Cluster> clusters;

  class CrossBar
  {
  public:
    CrossBar(Vector2d& dir, double len)
    :
      length(len),
      direction(dir)
    {}

    double length;
    Vector2d direction;
  };

  class GoalCandidate
  {
  public:

    GoalCandidate(const GoalPercept::GoalPost& postLeft, const GoalPercept::GoalPost& postRight, CrossBar& bar)
    :
      postCount(2),
      goalPostLeft(postLeft),
      goalPostRight(postRight),
      crossBar(bar)
    {}

    GoalCandidate(const GoalPercept::GoalPost& post, CrossBar& bar)
    :
      postCount(1),
      goalPost(post),
      crossBar(bar)
    {}

    int postCount;
    GoalPercept::GoalPost goalPostLeft;
    GoalPercept::GoalPost goalPostRight;
    GoalPercept::GoalPost goalPost;
    CrossBar crossBar;
  };

  std::vector<GoalCandidate> goalCandidates;
  std::vector<std::vector<GoalBarFeature> > features;
  size_t lastCrossBarScanLineId;

  void scanAlongCrossBar(const Vector2i& start, const Vector2i& end, const Vector2d& direction, double barWidth);
  size_t scanDown(size_t id, const Vector2i& downStart, const Vector2i& downEnd, double barWidth);
  size_t scanDownColor(size_t id, const Vector2i& downStart, const Vector2i& downEnd, double barWidth);
  size_t scanDownDiff(size_t id, const Vector2i& downStart, const Vector2i& downEnd, double barWidth);
  bool estimateCrossBarDirection(const GoalPercept::GoalPost& post, Vector2i& start, Vector2d& direction);
  Vector2d getBackProjectedTopPoint(const GoalPercept::GoalPost& post);
  int getBackProjectedTopBarWidth(const GoalPercept::GoalPost& post);
  int getBackProjectedPostHeight(const GoalPercept::GoalPost& post);

  bool checkProjectedPostDistance(const GoalPercept::GoalPost& post0, const GoalPercept::GoalPost& post1);
  void checkSinglePost(const GoalPercept::GoalPost& post);
  void checkBothPosts(const GoalPercept::GoalPost& post0, const GoalPercept::GoalPost& post1);<--- Function 'checkBothPosts' argument 1 names different: declaration 'post0' definition 'postLeft'.<--- Function 'checkBothPosts' argument 2 names different: declaration 'post1' definition 'postRight'.

  void clusterEdgelFeatures();
  void calcuateCrossBars();

  void testBackProjection();

  DOUBLE_CAM_PROVIDE(GoalCrossBarDetector, DebugImageDrawings);

  // double cam stuff
  DOUBLE_CAM_REQUIRE(GoalCrossBarDetector, CameraInfo);
  DOUBLE_CAM_REQUIRE(GoalCrossBarDetector, Image);
  DOUBLE_CAM_REQUIRE(GoalCrossBarDetector, CameraMatrix);
  DOUBLE_CAM_REQUIRE(GoalCrossBarDetector, ArtificialHorizon);
  DOUBLE_CAM_REQUIRE(GoalCrossBarDetector, GoalPercept);

  DOUBLE_CAM_PROVIDE(GoalCrossBarDetector, GoalCrossBarPercept);

};//end class GoalCrossBarDetector

#endif // GOALCROSSBARDETECTOR_H