aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/gra/FeaturePointsDetector_Harris.h
blob: 8f0f8fe9a0b9fc09230c192d6d3c12f35bcbb96f (plain) (blame)
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
#ifndef   gra_FeaturePointsDetector_Harris
#define   gra_FeaturePointsDetector_Harris

#include "FeaturePointsDetector.h"

#include "Bitmap.h"
#include "FeaturePoint.h"
#include "FeaturePointsDetector.h"

#include "../dsa/DisjointSet.h"

#include "../Self.h"

#include <vector>

namespace meow {

/*!
 * @brief Harris-Corner-Detect algorithm for finding feature points.
 *
 * @author cat_leopard
 */
template<class Pixel>
class FeaturePointsDetector_Harris: public FeaturePointsDetector<Pixel> {
# define FPD_Harris FeaturePointsDetector_Harris
private:
  struct Myself {
    double     ratioK_;
    double thresholdR_;
    double      sizeW_;
    double     noiseN_;
    double     lightL_;
    double   featureG_;
    size_t     boundB_;

    Myself():
    ratioK_(0.03),
    thresholdR_(0.001),
    sizeW_(2.0),
    noiseN_(3.0),
    lightL_(30.0),
    featureG_(3.0),
    boundB_(10u) {
    }
    Myself(Myself const& m):
    ratioK_(m.ratioK_),
    thresholdR_(m.thresholdR_),
    sizeW_(m.sizeW_),
    noiseN_(m.noiseN_),
    lightL_(m.lightL_),
    featureG_(m.featureG_),
    boundB_(m.boundB_){
    }
    ~Myself() {
    }
  };

  Self<Myself> const self;
public:
  typedef FeaturePoint<double, double> MyFeaturePoint;
  typedef std::vector<MyFeaturePoint>  MyFeaturePoints;
  //! @brief constructor 使用預設參數
  FPD_Harris(): self() {
  }

  //! @brief constructor 參數複製自另一個 FeaturePointsDetector_Harris
  FPD_Harris(FPD_Harris const& fps): self(fps.self, Self<Myself>::COPY_FROM) {
  }

  //! @brief 解構子
  ~FPD_Harris() {
  }

  //! @brief 複製
  FPD_Harris& copyFrom(FPD_Harris const& fps) {
    self().copyFrom(fps.self);
    return *this;
  }

  //! @brief 參照
  FPD_Harris& referenceFrom(FPD_Harris const& fps) {
    self().referenceFrom(fps.self);
    return *this;
  }

  //! @brief K
  double paramK() const {
    return self->ratioK_;
  }

  //! @brief R
  double paramR() const {
    return self->thresholdR_;
  }

  //! @brief W
  double paramW() const {
    return self->sizeW_;
  }

  //! @brief N
  double paramN() const {
    return self->noiseN_;
  }

  //! @brief G
  double paramG() const {
    return self->featureG_;
  }

  //! @brief L
  double paramL() const {
    return self->lightL_;
  }

  //! @brief bound
  size_t paramB() const {
    return self->boundB_;
  }

  //! @brief K
  double paramK(double k) {
    self()->ratioK_ = k;
    return paramK();
  }

  //! @brief R
  double paramR(double r) {
    self()->thresholdR_ = r;
    return paramR();
  }

  //! @brief W
  double paramW(double w) {
    self()->sizeW_ = w;
    return paramW();
  }

  //! @brief N
  double paramN(double n){
    self()->noiseN_ = n;
    return paramN();
  }

  //! @brief L
  double paramL(double l) {
    self()->lightL_ = l;
    return paramL();
  }

  //! @brief G
  double paramG(double g) {
    self()->featureG_ = g;
    return paramG();
  }

  //! @brief B
  size_t paramB(size_t b) {
    self()->boundB_ = b;
    return paramB();
  }

  /*! @brief 找出特徵點
   *
   *  @param [in] bmp 要抓特徵點的點陣圖
   *  @return \c std::vector<FeaturePoint<double,double>> 型態的一堆特徵點
   */
  MyFeaturePoints detect(Bitmap<Pixel> const& bmp) const {
    Bitmap<Pixel> input = bmp;

    // gradiance
    Bitmap<Pixel> input_gx(input.gradianceX(0, self->noiseN_));
    Bitmap<Pixel> input_gy(input.gradianceY(self->noiseN_, 0));

    // get Matrix I for each pixel
    Bitmap<double> Ixx(input.height(), input.width(), 0.0);
    Bitmap<double> Iyy(input.height(), input.width(), 0.0);
    Bitmap<double> Ixy(input.height(), input.width(), 0.0);
    for (ssize_t y = 0, Y = input.height(); y < Y; y++) {
      for (ssize_t x = 0, X = input.width(); x < X; x++) {
        Pixel gx(input_gx(y, x));
        Pixel gy(input_gy(y, x));
        Ixx.pixel(y, x, gx * gx);
        Iyy.pixel(y, x, gy * gy);
        Ixy.pixel(y, x, gx * gy);
      }
    }

    // blur
    Ixx.gaussianed(self->sizeW_, self->sizeW_);
    Iyy.gaussianed(self->sizeW_, self->sizeW_);
    Ixy.gaussianed(self->sizeW_, self->sizeW_);
    input_gx.clear();
    input_gy.clear();

    // filter too flat or on edge
    Bitmap<double>    R(input.height(), input.width(),   0.0);
    Bitmap<bool>   good(input.height(), input.width(), false);
    ssize_t b = self->boundB_;
    for (ssize_t y = b, Y = -b + input.height(); y < Y; y++) {
      for (ssize_t x = b, X = -b + input.width(); x < X; x++) {
        double det = Ixx(y, x) * Iyy(y, x) - squ(Ixy(y, x));
        double tra = Ixx(y, x) + Iyy(y, x);
        double r = det - self->ratioK_ * squ(tra);
        R.pixel(y, x, r);
        good.pixel(y, x, (r >= self->thresholdR_));
      }
    }
    Ixx.clear();
    Iyy.clear();
    Ixy.clear();

    // find union neighbor
    DisjointSet dsj(input.size());
    ssize_t dy[2] = {0, 1};
    ssize_t dx[2] = {1, 0};
    for (ssize_t y = b, Y = -b + input.height(); y < Y; y++) {
      for (ssize_t x = b, X = -b + input.width(); x < X; x++) {
        if(good.pixel((size_t)y, (size_t)x)){
          for (size_t k = 0; k < 2u; k++) {
            if (good.pixel((size_t)(y + dy[k]), (size_t)(x + dx[k]))) {
              dsj.merge( y          * input.width() +  x,
                        (y + dy[k]) * input.width() + (x + dx[k]));
            }
          }
        }
      }
    }

    // find local maximum
    std::vector<size_t> max_i(input.size());
    for (size_t i = 0, I = input.size(); i < I; i++) {
      max_i[i] = i;
    }
    for (size_t i = 0, I = input.size(); i < I; i++) {
      size_t ri = dsj.root(i);
      if (R.pixel(       i  / input.width(),        i  % input.width()) >
          R.pixel(max_i[ri] / input.width(), max_i[ri] % input.width())) {
        max_i[ri] = i;
      }
    }

    // blur before get description
    input.gaussianed(self->featureG_, self->featureG_);

    MyFeaturePoints ret;
    for (ssize_t y = b, Y = -b + input.height(); y < Y; y++) {
      for (ssize_t x = b, X = -b + input.width(); x < X; x++) {
        if (!good.pixel((size_t)y, (size_t)x)) {
          continue;
        }
        size_t  i = y * input.width() + x;
        if (max_i[dsj.root(i)] != i) {
          continue;
        }
        ssize_t dx[4] = {1, 0, -1, 0};
        ssize_t dy[4] = {0, 1, 0, -1};
        std::vector<double> desc; // description
        for (ssize_t d = 1; d <= (ssize_t)self->boundB_; d++) {
          std::vector<double> light;
          size_t max_id = 0;
          size_t x0 = x - d, y0 = y - d;
          for (size_t k = 0; k < 4; k++) {
            for (ssize_t n = 0;
                 n < (ssize_t)b * 2;
                 n++, x0 += dx[k], y0 += dy[k]){
              Pixel diff = input.pixel(y0, x0) - input.pixel(y, x) * 0.2;
              light.push_back(diff * diff * self->lightL_);
              if (light[max_id] < light[-1 + light.size()]) {
                max_id = -1 + (ssize_t)light.size();
              }
            }
          }
          for (ssize_t n = 0, N = light.size(); n < N; n++) {
            desc.push_back((max_id + n) % N);
            desc.push_back(light[(max_id + n) % N]);
          }
        }
        MyFeaturePoint now(2, desc.size());
        now.position(0, x);
        now.position(1, y);
        now.description(Vector<double>(desc));
        ret.push_back(now);
      }
    }
    return ret;
  }

  //! @brief same as \c copyFrom(fps)
  FPD_Harris& operator=(FPD_Harris const& fps) {
    return copyFrom(fps);
  }

  //! @brief same as \c detect(bmp)
  MyFeaturePoints operator()(Bitmap<Pixel> const& bmp) const {
    return detect(bmp);
  }

  /*! @brief 寫到檔案裡
   *
   * 未完成
   */
  bool write(FILE* f, bool bin, unsigned int fg) const {
    // TODO
    return false;
  }

  /*! @brief 將資料讀入
   *
   * 未完成
   */
  bool read (FILE* f, bool bin, unsigned int fg) {
    // TODO
    return false;
  }

  /*! @brief new一個自己
   *
   *  @return 一個new出來的FeaturePointsDetector_Harris<Pixel>
   */
  ObjBase* create() const {
    return (ObjBase*)new FPD_Harris<Pixel>();
  }

  /*! @brief 複製資料
   *
   * 輸入型別是 \c ObjBase \c const*
   * 這裡假設實體其實是 \c FeaturePointsDetector_Harris.
   * 事實上這個method就只是幫忙轉型然後呼叫原本的\c copyFrom
   *
   *  @param [in] b 資料來源
   *  @return this
   */
  ObjBase* copyFrom(ObjBase const* b) {
    return &(copyFrom(*(FPD_Harris const*)b));
  }

  /*! @brief 回傳class的type
   *
   *  @return \c char \c const\c * 形式的typename
   */
  char const* ctype() const {
    return typeid(*this).name();
  }

  /*! @brief 回傳class的type
   *
   *  @return \c std::string 形式的typename
   */
  std::string  type() const {
    return std::string(ctype());
  }
# undef FPD_Harris
};

}

#endif // gra_FeaturePointsDetector_Harris