File: morphology.h

package info (click to toggle)
aoflagger 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,868 kB
  • sloc: cpp: 52,164; python: 152; sh: 60; makefile: 17
file content (99 lines) | stat: -rw-r--r-- 2,952 bytes parent folder | download
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
#ifndef MORPHOLOGY_H
#define MORPHOLOGY_H

#include <vector>
#include <map>

#include "../../structures/mask2d.h"
#include "../../structures/segmentedimage.h"

class Morphology {
 public:
  Morphology()
      : _hLineEnlarging(1),
        _vLineEnlarging(1),
        _hDensityEnlargeRatio(0.5),
        _vDensityEnlargeRatio(0.5) {}
  ~Morphology() {}

  void SegmentByMaxLength(const Mask2D *mask, SegmentedImagePtr output);
  void SegmentByLengthRatio(const Mask2D *mask, SegmentedImagePtr output);
  void Cluster(SegmentedImagePtr segmentedImage);
  void RemoveSmallSegments(SegmentedImagePtr segmentedImage,
                           size_t thresholdLevel);
  void Classify(SegmentedImagePtr segmentedImage);

  static size_t BROADBAND_SEGMENT, LINE_SEGMENT, BLOB_SEGMENT;

 private:
  struct SegmentInfo {
    SegmentInfo()
        : segment(0),
          top(0),
          left(0),
          bottom(0),
          right(0),
          count(0),
          width(0),
          height(0),
          xTotal(0),
          yTotal(0),
          mark(false) {}

    size_t segment;
    size_t top, left, bottom, right;
    size_t count;
    size_t width, height;
    size_t xTotal, yTotal;
    bool mark;

    void AddPoint(size_t x, size_t y) {
      if (x < left) left = x;
      if (x >= right) right = x + 1;
      if (y < top) top = y;
      if (y >= bottom) bottom = y + 1;
      xTotal += x;
      yTotal += y;
      ++count;
    }
    int HorizontalDistance(const SegmentInfo &other) const {
      if (other.left > right)
        return (int)other.left - (int)right;
      else if (left > other.right)
        return (int)left - (int)other.right;
      else
        return 0;
    }
    int VerticalDistance(const SegmentInfo &other) const {
      if (other.top > bottom)
        return (int)other.top - (int)bottom;
      else if (top > other.bottom)
        return (int)top - (int)other.bottom;
      else
        return 0;
    }
    bool Contains(size_t x, size_t y) const {
      return (x >= left && x < right && y >= top && y < bottom);
    }
  };

  void calculateOpenings(const Mask2D *mask, int **values);
  void calculateOpenings(const Mask2D *mask, Mask2DPtr *values, int **hCounts,
                         int **vCounts);
  void calculateVerticalCounts(const Mask2D *mask, int **values);
  void calculateHorizontalCounts(const Mask2D *mask, int **values);
  void floodFill(const Mask2D *mask, SegmentedImagePtr output,
                 const int *const *lengthWidthValues, size_t x, size_t y,
                 size_t value);
  void floodFill(const Mask2D *mask, SegmentedImagePtr output,
                 Mask2DPtr *matrices, size_t x, size_t y, size_t z,
                 size_t value, int **hCounts, int **vCounts);
  std::map<size_t, SegmentInfo> createSegmentMap(
      SegmentedImageCPtr segmentedImage) const;

  size_t _hLineEnlarging;
  size_t _vLineEnlarging;
  double _hDensityEnlargeRatio, _vDensityEnlargeRatio;
};

#endif