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
/* 
 * File:   FieldColorClassifier.h
 * Author: Heinrich Mellmann
 *
 */

#ifndef _FieldColorClassifier_H_
#define _FieldColorClassifier_H_

#include <ModuleFramework/Module.h>

// Representations
#include "Representations/Infrastructure/FrameInfo.h"
#include "Representations/Infrastructure/Image.h" // just for debug
#include "Representations/Perception/FieldColorPercept.h"
#include "Representations/Perception/ArtificialHorizon.h"
#include "Representations/Perception/BodyContour.h"
#include "Representations/Perception/ColorTable64.h"

// Tools
#include <Tools/Math/Vector2.h>
#include <Tools/DataStructures/UniformGrid.h>
#include <Tools/DataStructures/Histogram.h>

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

#include "Tools/DoubleCamHelpers.h"

#include "Tools/Math/Moments2.h"

//////////////////// BEGIN MODULE INTERFACE DECLARATION ////////////////////

BEGIN_DECLARE_MODULE(FieldColorClassifier)
  PROVIDE(StopwatchManager)
  PROVIDE(DebugPlot)
  PROVIDE(DebugModify)
  PROVIDE(DebugRequest)
  PROVIDE(DebugDrawings)
  PROVIDE(DebugImageDrawings)
  PROVIDE(DebugImageDrawingsTop)
  PROVIDE(DebugParameterList)
  
  REQUIRE(FrameInfo)
  REQUIRE(Image)
  REQUIRE(ImageTop)
  REQUIRE(ArtificialHorizon)
  REQUIRE(ArtificialHorizonTop)
  REQUIRE(BodyContour)
  REQUIRE(BodyContourTop)

  PROVIDE(FieldColorPercept)
  PROVIDE(FieldColorPerceptTop)

  PROVIDE(ColorTable64)
END_DECLARE_MODULE(FieldColorClassifier)

//////////////////// END MODULE INTERFACE DECLARATION //////////////////////


class FieldColorClassifier : public  FieldColorClassifierBase
{
private:
  class Parameters: public ParameterList
  {
  public:
    Parameters(std::string name = "FieldColorClassifier") : ParameterList(name)
    {
      PARAMETER_REGISTER(green.brightnesConeOffset) = 30;
      PARAMETER_REGISTER(green.brightnesConeRadiusBlack) = 2;
      PARAMETER_REGISTER(green.brightnesConeRadiusWhite) = 50;

      PARAMETER_REGISTER(green.colorAngleCenter) = -2.4;
      PARAMETER_REGISTER(green.colorAngleWith) = 0.8;


      PARAMETER_REGISTER(red.brightnesConeOffset) = 16;
      PARAMETER_REGISTER(red.brightnesConeRadiusBlack) = 10;
      PARAMETER_REGISTER(red.brightnesConeRadiusWhite) = 70;

      PARAMETER_REGISTER(red.colorAngleCenter) = -4.4;
      PARAMETER_REGISTER(red.colorAngleWith) = 0.4;

      PARAMETER_REGISTER(provide_colortable) = false;

      syncWithConfig();
    }

    virtual ~Parameters() {}

    FieldColorPercept::HSISeparatorOptimized::Parameter green;
    FieldColorPercept::HSISeparatorOptimized::Parameter red;

    bool provide_colortable;
  };
public:
  FieldColorClassifier();
  virtual ~FieldColorClassifier();

  void execute()
  {
    if(paramsBottom.check_changed() || paramsTop.check_changed()) {
      frameWhenParameterChanged = getFrameInfo();
    }
    execute(CameraInfo::Top, paramsTop);
    execute(CameraInfo::Bottom, paramsBottom);
  }

private:
  void execute(const CameraInfo::CameraID id, Parameters& parameters);
  void updateCache();
  void debug(Parameters& parameters);

  

  class ParametersBottom: public Parameters
  {
  public:
    ParametersBottom() : Parameters("FieldColorClassifier") {}
    virtual ~ParametersBottom() {}
  } paramsBottom;

  class ParametersTop: public Parameters
  {
  public:
    ParametersTop() :  Parameters("FieldColorClassifierTop") {}
    virtual ~ParametersTop() {}
  } paramsTop;
  
  naoth::FrameInfo frameWhenParameterChanged;

private: // doublecam

    // id of the camera the module is curently running on
    CameraInfo::CameraID cameraID;

    DOUBLE_CAM_PROVIDE(FieldColorClassifier, DebugImageDrawings);

    DOUBLE_CAM_REQUIRE(FieldColorClassifier, Image);
    DOUBLE_CAM_REQUIRE(FieldColorClassifier, BodyContour);
    DOUBLE_CAM_REQUIRE(FieldColorClassifier, ArtificialHorizon);
    DOUBLE_CAM_PROVIDE(FieldColorClassifier, FieldColorPercept);

private:
  UniformGrid uniformGrid; // subsampling of the image

  class Histogram2D
  {
  private:
    std::vector<std::vector<double> > data;
  public:
    void setSize(size_t size) {
      data.resize(size);

      for(size_t i = 0; i < data.size(); i++) {
        data[i].resize(size);
      }
    }

    void clear() {
      for(size_t i = 0; i < data.size(); i++) {
        for(size_t j = 0; j < data[i].size(); j++) {
          data[i][j] = 0.0;
        }
      }
    }

    double& operator() (size_t x, size_t y) {
      return data[x][y];
    }

    double operator() (size_t x, size_t y) const {
      return data[x][y];
    }

    size_t size() const {
      return data.size();
    }
  };

  Histogram2D histogramYCromaArray[CameraInfo::numOfCamera];
  Histogram2D histogramUVArray[CameraInfo::numOfCamera];
  Histogram2D histogramUVBallArray[CameraInfo::numOfCamera];

private: // debug
  void plot(std::string id, Statistics::HistogramX& histogram) const
  {
    for(int i = 0; i < histogram.size; i++) {
      PLOT_GENERIC(id + ":rawHistogram" + ((cameraID == CameraInfo::Top)?"Top":"Bottom"), i, histogram.rawData[i]);
    }
  }

  void draw_UVSeparator(double angleCenter, double angleWidth) const;
  void draw_YChromaSeparator(double brightnesConeOffset, double brightnesConeRadiusBlack, double brightnesConeRadiusWhite) const;
  void draw_histogramUV(const Histogram2D& histUV) const;

};

#endif  /* _FieldColorClassifier_H_ */