blob: eef5b20203ecbb26df197ba53ad1eb828cd358b4 [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>
Parker Schuh02f13f62019-02-16 16:42:41 -080012RangeImage DoThreshold(ImageFormat fmt, ThresholdFn &&fn) {
Parker Schuh6691f192017-01-14 17:01:02 -080013 std::vector<std::vector<ImageRange>> ranges;
Parker Schuh02f13f62019-02-16 16:42:41 -080014 ranges.reserve(fmt.h);
15 for (int y = 0; y < fmt.h; ++y) {
Parker Schuh6691f192017-01-14 17:01:02 -080016 bool p_score = false;
17 int pstart = -1;
18 std::vector<ImageRange> rngs;
Parker Schuh02f13f62019-02-16 16:42:41 -080019 for (int x = 0; x < fmt.w; ++x) {
20 if (fn(x, y) != p_score) {
Parker Schuh6691f192017-01-14 17:01:02 -080021 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) {
Parker Schuh02f13f62019-02-16 16:42:41 -080030 rngs.emplace_back(ImageRange(pstart, fmt.w));
Parker Schuh6691f192017-01-14 17:01:02 -080031 }
32 ranges.push_back(rngs);
33 }
34 return RangeImage(0, std::move(ranges));
35}
36
Parker Schuh02f13f62019-02-16 16:42:41 -080037// ThresholdFn should be a lambda.
38template <typename ThresholdFn>
39RangeImage DoThreshold(const ImagePtr &img, ThresholdFn &&fn) {
40 return DoThreshold(img.fmt(),
41 [&](int x, int y) { return fn(img.get_px(x, y)); });
42}
43
44// YUYV image types:
45inline RangeImage DoThresholdYUYV(ImageFormat fmt, const char *data,
46 uint8_t value) {
47 return DoThreshold(fmt, [&](int x, int y) {
48 uint8_t v = data[y * fmt.w * 2 + x * 2];
49 return v > value;
50 });
51}
52
Parker Schuh6691f192017-01-14 17:01:02 -080053} // namespace vision
54} // namespace aos
55
56#endif // _AOS_VIISON_BLOB_THRESHOLD_H_