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
/**
* @file GoalDetector.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 GoalDetector
*/

#ifndef GoalDetector_H
#define GoalDetector_H

#include <ModuleFramework/Module.h>

#include <Representations/Infrastructure/CameraInfo.h>
#include <Representations/Infrastructure/Image.h>
#include <Representations/Infrastructure/FrameInfo.h>
#include "Representations/Perception/GoalPercept.h"
#include "Representations/Perception/GoalFeaturePercept.h"
#include "Representations/Perception/Histograms.h"
#include "Representations/Perception/FieldPercept.h"
#include "Representations/Perception/CameraMatrix.h"

// tools
#include "Tools/DoubleCamHelpers.h"

#include <vector>

#include "Tools/naoth_opencv.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(GoalDetector)
  PROVIDE(StopwatchManager)
  PROVIDE(DebugRequest)
  PROVIDE(DebugModify)
  PROVIDE(DebugImageDrawings)
  PROVIDE(DebugImageDrawingsTop)
  PROVIDE(DebugParameterList)

  REQUIRE(FrameInfo)

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

  REQUIRE(FieldPercept)
  REQUIRE(FieldPerceptTop)

  REQUIRE(GoalFeaturePercept)
  REQUIRE(GoalFeaturePerceptTop)

  PROVIDE(GoalPercept)
  PROVIDE(GoalPerceptTop)
  PROVIDE(GoalPostHistograms)
END_DECLARE_MODULE(GoalDetector)


class GoalDetector: private GoalDetectorBase
{
public:

  GoalDetector();
  virtual ~GoalDetector();

  // override the Module execute method
  virtual void execute()
  {
    execute(CameraInfo::Top);
    execute(CameraInfo::Bottom);

    debugStuff(CameraInfo::Top);
    debugStuff(CameraInfo::Bottom);
  }

private:
  void execute(CameraInfo::CameraID id);
  CameraInfo::CameraID cameraID;

  class Parameters: public ParameterList
  {
  public:

    Parameters() : ParameterList("GoalDetectorParameters")
    {
      PARAMETER_REGISTER(threshold) = 140;
      PARAMETER_REGISTER(thresholdGradient) = 30;
      PARAMETER_REGISTER(minGoodPoints) = 3;

      PARAMETER_REGISTER(detectWhiteGoals) = true;

      PARAMETER_REGISTER(colorRegionDeviation) = 2;
      PARAMETER_REGISTER(thresholdFeatureSimilarity) = 0.8;

      syncWithConfig();
    }

    virtual ~Parameters() {
    }

    int threshold;
    int thresholdGradient;
    int minGoodPoints;

    bool detectWhiteGoals;

    double thresholdFeatureSimilarity;
    double colorRegionDeviation;
  };

  Parameters params;

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

  public:
    Cluster() : summedWidths(0.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]);
  
      // always point down in the image
      if(v.y < 0) {
        v *= -1.0;
      }

      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;
  void clusterEdgelFeatures();
  void calcuateGoalPosts();
  Vector2i scanForEndPoint(const Vector2i& start, const Vector2d& direction) const;

  void debugStuff(CameraInfo::CameraID camID);

  DOUBLE_CAM_PROVIDE(GoalDetector, DebugImageDrawings);

  // double cam stuff
  DOUBLE_CAM_REQUIRE(GoalDetector, CameraInfo);
  DOUBLE_CAM_REQUIRE(GoalDetector, Image);
  DOUBLE_CAM_REQUIRE(GoalDetector, CameraMatrix);
  DOUBLE_CAM_REQUIRE(GoalDetector, FieldPercept);
  DOUBLE_CAM_REQUIRE(GoalDetector, GoalFeaturePercept);

  DOUBLE_CAM_PROVIDE(GoalDetector, GoalPercept);

};//end class GoalDetector

#endif // GoalDetector_H