Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 1 | #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 | |
| 7 | namespace aos { |
| 8 | namespace vision { |
| 9 | |
| 10 | // ThresholdFn should be a lambda. |
| 11 | template <typename ThresholdFn> |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame^] | 12 | RangeImage DoThreshold(ImageFormat fmt, ThresholdFn &&fn) { |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 13 | std::vector<std::vector<ImageRange>> ranges; |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame^] | 14 | ranges.reserve(fmt.h); |
| 15 | for (int y = 0; y < fmt.h; ++y) { |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 16 | bool p_score = false; |
| 17 | int pstart = -1; |
| 18 | std::vector<ImageRange> rngs; |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame^] | 19 | for (int x = 0; x < fmt.w; ++x) { |
| 20 | if (fn(x, y) != p_score) { |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 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) { |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame^] | 30 | rngs.emplace_back(ImageRange(pstart, fmt.w)); |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 31 | } |
| 32 | ranges.push_back(rngs); |
| 33 | } |
| 34 | return RangeImage(0, std::move(ranges)); |
| 35 | } |
| 36 | |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame^] | 37 | // ThresholdFn should be a lambda. |
| 38 | template <typename ThresholdFn> |
| 39 | RangeImage 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: |
| 45 | inline 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 Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 53 | } // namespace vision |
| 54 | } // namespace aos |
| 55 | |
| 56 | #endif // _AOS_VIISON_BLOB_THRESHOLD_H_ |