File: morphologicalflagger.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (120 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download | duplicates (2)
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
#include "morphologicalflagger.h"

#include <utility>

namespace algorithms {

bool MorphologicalFlagger::SquareContainsFlag(const Mask2D* mask, size_t xLeft,
                                              size_t yTop, size_t xRight,
                                              size_t yBottom) {
  for (size_t y = yTop; y <= yBottom; ++y) {
    for (size_t x = xLeft; x <= xRight; ++x) {
      if (mask->Value(x, y)) return true;
    }
  }
  return false;
}

void MorphologicalFlagger::DilateFlagsHorizontally(Mask2D* mask,
                                                   size_t timeSize) {
  if (timeSize != 0) {
    Mask2D destination(Mask2D::MakeUnsetMask(mask->Width(), mask->Height()));
    if (timeSize > mask->Width()) timeSize = mask->Width();
    const int intSize = (int)timeSize;

    for (size_t y = 0; y < mask->Height(); ++y) {
      int dist = intSize + 1;
      for (size_t x = 0; x < timeSize; ++x) {
        if (mask->Value(x, y)) dist = -intSize;
        dist++;
      }
      for (size_t x = 0; x < mask->Width() - timeSize; ++x) {
        if (mask->Value(x + timeSize, y)) dist = -intSize;
        if (dist <= intSize) {
          destination.SetValue(x, y, true);
          dist++;
        } else {
          destination.SetValue(x, y, false);
        }
      }
      for (size_t x = mask->Width() - timeSize; x < mask->Width(); ++x) {
        if (dist <= intSize) {
          destination.SetValue(x, y, true);
          dist++;
        } else {
          destination.SetValue(x, y, false);
        }
      }
    }
    *mask = std::move(destination);
  }
}

void MorphologicalFlagger::DilateFlagsVertically(Mask2D* mask,
                                                 size_t frequencySize) {
  if (frequencySize != 0) {
    Mask2D destination(Mask2D::MakeUnsetMask(mask->Width(), mask->Height()));
    if (frequencySize > mask->Height()) frequencySize = mask->Height();
    const int intSize = (int)frequencySize;

    for (size_t x = 0; x < mask->Width(); ++x) {
      int dist = intSize + 1;
      for (size_t y = 0; y < frequencySize; ++y) {
        if (mask->Value(x, y)) dist = -intSize;
        dist++;
      }
      for (size_t y = 0; y < mask->Height() - frequencySize; ++y) {
        if (mask->Value(x, y + frequencySize)) dist = -intSize;
        if (dist <= intSize) {
          destination.SetValue(x, y, true);
          dist++;
        } else {
          destination.SetValue(x, y, false);
        }
      }
      for (size_t y = mask->Height() - frequencySize; y < mask->Height(); ++y) {
        if (dist <= intSize) {
          destination.SetValue(x, y, true);
          dist++;
        } else {
          destination.SetValue(x, y, false);
        }
      }
    }
    *mask = std::move(destination);
  }
}

void MorphologicalFlagger::LineRemover(Mask2D* mask,
                                       size_t maxTimeContamination,
                                       size_t maxFreqContamination) {
  for (size_t x = 0; x < mask->Width(); ++x) {
    size_t count = 0;
    for (size_t y = 0; y < mask->Height(); ++y) {
      if (mask->Value(x, y)) ++count;
    }
    if (count > maxFreqContamination) FlagTime(mask, x);
  }

  for (size_t y = 0; y < mask->Height(); ++y) {
    size_t count = 0;
    for (size_t x = 0; x < mask->Width(); ++x) {
      if (mask->Value(x, y)) ++count;
    }
    if (count > maxTimeContamination) FlagFrequency(mask, y);
  }
}

void MorphologicalFlagger::FlagTime(Mask2D* mask, size_t x) {
  for (size_t y = 0; y < mask->Height(); ++y) {
    mask->SetValue(x, y, true);
  }
}

void MorphologicalFlagger::FlagFrequency(Mask2D* mask, size_t y) {
  for (size_t x = 0; x < mask->Width(); ++x) {
    mask->SetValue(x, y, true);
  }
}

}  // namespace algorithms