blob: 8b6051f030e8d0c6d302133e81290f47de729989 [file] [log] [blame]
Parker Schuh6691f192017-01-14 17:01:02 -08001#ifndef _AOS_VIISON_BLOB_THRESHOLD_H_
2#define _AOS_VIISON_BLOB_THRESHOLD_H_
3
4#include "aos/vision/blob/range_image.h"
5#include "aos/vision/image/image_types.h"
6
7namespace aos {
8namespace vision {
9
10// ThresholdFn should be a lambda.
11template <typename ThresholdFn>
12RangeImage DoThreshold(const ImagePtr &img, ThresholdFn &&fn) {
13 std::vector<std::vector<ImageRange>> ranges;
14 ranges.reserve(img.fmt().h);
15 for (int y = 0; y < img.fmt().h; ++y) {
16 bool p_score = false;
17 int pstart = -1;
18 std::vector<ImageRange> rngs;
19 for (int x = 0; x < img.fmt().w; ++x) {
20 if (fn(img.get_px(x, y)) != p_score) {
21 if (p_score) {
22 rngs.emplace_back(ImageRange(pstart, x));
23 } else {
24 pstart = x;
25 }
26 p_score = !p_score;
27 }
28 }
29 if (p_score) {
30 rngs.emplace_back(ImageRange(pstart, img.fmt().w));
31 }
32 ranges.push_back(rngs);
33 }
34 return RangeImage(0, std::move(ranges));
35}
36
37} // namespace vision
38} // namespace aos
39
40#endif // _AOS_VIISON_BLOB_THRESHOLD_H_