Beatmup
Kernels::ComputeBounds< in_t > Class Template Reference

Static Public Member Functions

static void process (AbstractBitmap &bitmap, std::vector< IntegerContour2D * > &boundary, std::vector< IntPoint > &border, BinaryMaskWriter &testedPixels, float level)
 

Detailed Description

template<class in_t>
class Kernels::ComputeBounds< in_t >

Definition at line 27 of file contours.cpp.

Member Function Documentation

◆ process()

template<class in_t >
static void Kernels::ComputeBounds< in_t >::process ( AbstractBitmap bitmap,
std::vector< IntegerContour2D * > &  boundary,
std::vector< IntPoint > &  border,
BinaryMaskWriter testedPixels,
float  level 
)
inlinestatic

Definition at line 29 of file contours.cpp.

29  {
30  in_t in(bitmap);
31 
32  typedef struct {
33  int x, y;
34  } Displacement;
35 
36  // clockwise direction lookup in function of four neighbour values
37  const Displacement LOOKUP_DIRECTION[2][2][2][2] = {
38  //rb //lb //rb+lb //rt //rt+rb //rt+lb //rt+lb+rb
39  { { { { 0, 0 }, { 1, 0 } }, { { 0, +1 }, { +1, 0 } } }, { { { 0, -1 }, { 0, -1 } }, { { 9, 9 }, { 0, -1 } } } },
40  //lt //lt+rb //lt+lb //lt+lb+rb //lt+rt //lt+rt+rb //lt+rt+lb
41  { { { { -1, 0 }, { 9, 9 } }, { { 0, 1 }, { +1, 0 } } }, { { { -1, 0 }, { -1, 0 } }, { { 0, 1 }, { 0, 0 } } } }
42  };
43 
44  const Displacement LOOKUP_INTERNAL_PIXEL[3][3] = {
45  { { 0, 0 }, { 0, -1 }, { 0, 0 } },
46  { { -1, -1 }, { 9, 0 }, { 0, 0 } },
47  { { 0, 0 }, { -1, 0 }, { 0, 0 } }
48  };
49 
50  const int W = in.getWidth(), H = in.getHeight();
51  const float LEVEL = level * in.MAX_VALUE;
52  IntPoint seed;
53  int x, y;
54 
55  // scanning boundary pixels
56  for (auto seed : border) {
57 
58  // picking a seed
59  x = seed.x;
60  y = seed.y;
61  testedPixels.goTo(x, y);
62  if (testedPixels.getValue() > 0)
63  continue;
64  testedPixels.putValue(1);
65 
66  // a good seed has one neighboring point inside
67  int ctr = 0;
68  if (x < W && y < H && in().mean() > LEVEL) ++ctr;
69  if (x > 0 && y < H && in(x - 1, y).mean() > LEVEL) ++ctr;
70  if (x < W && y > 0 && in(x, y - 1).mean() > LEVEL) ++ctr;
71  if (x > 0 && y > 0 && in(x - 1, y - 1).mean() > LEVEL) ++ctr;
72  if (ctr != 1)
73  continue;
74 
75  // good seed found: go
76  in.goTo(x, y);
77  Displacement prev = { 0, 0 };
78  IntegerContour2D* contour = new IntegerContour2D();
79  boundary.push_back(contour);
80 
81  // go
82  do {
83  // binarize the neighbours
84  bool
85  rb = x < W && y < H && in().mean() > LEVEL,
86  lb = x > 0 && y < H && in(x - 1, y).mean() > LEVEL,
87  rt = x < W && y > 0 && in(x, y - 1).mean() > LEVEL,
88  lt = x > 0 && y > 0 && in(x - 1, y - 1).mean() > LEVEL;
89 
90  // check the point: must have a neighbor inside and a neighbor outside
91  if ((rb && lb && rt && lt) || !(rb || lb || rt || lt))
92  throw IntegerContour2D::BadSeedPoint(x, y, lt, rt, lb, rb);
93 
94  // add the point
95  contour->addPoint(x, y);
96 
97  // compute step direction
98  Displacement d = LOOKUP_DIRECTION[lt][rt][lb][rb];
99  if (d.x == 9) { // special case: displacement depends on the previous direction
100  if (prev.x == 0 && prev.y == 0)
101  throw IntegerContour2D::BadSeedPoint(x, y, lt, rt, lb, rb);
102  d.x = -prev.y;
103  d.y = prev.x;
104  }
105 
106  // marking used pixel
107  Displacement ip = LOOKUP_INTERNAL_PIXEL[d.y + 1][d.x + 1];
108  int ipx = x + ip.x, ipy = y + ip.y;
109  if (ipx >= 0 && ipy >= 0 && ipx < W && ipy < H) {
110  testedPixels.goTo(ipx, ipy);
111  testedPixels.putValue(1);
112  }
113 
114  // step
115  x += d.x;
116  y += d.y;
117  prev = d;
118 
119  if (x < W && y < H)
120  in.goTo(x, y);
121 
122  } while (seed.x != x || seed.y != y);
123 
124  // adding the last point
125  contour->addPoint(x, y);
126  }
127  }
A sequence of integer-valued 2D points.
Definition: contours.h:33
void addPoint(int x, int y)
Adds a new point to the end of the contour.
Definition: contours.cpp:137
unsigned char getValue() const
Returns 0..MAX_UNNORM_VALUE value at current position.
void goTo(int x, int y)
Changes current position.
void putValue(unsigned char x)
Puts a properly scaled (0..MAX_UNNORM_VALUE) value at the current position.
jobject jlong jint jint y
jobject jlong jint x
Beatmup::InternalBitmap * bitmap

The documentation for this class was generated from the following file: