Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 1 | #ifndef AOS_VISION_BLOB_THRESHOLD_H_ |
| 2 | #define AOS_VISION_BLOB_THRESHOLD_H_ |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 3 | |
| 4 | #include "aos/vision/blob/range_image.h" |
| 5 | #include "aos/vision/image/image_types.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace vision { |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 9 | namespace threshold_internal { |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 10 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 11 | // Performs thresholding in a given region using a function which determines |
| 12 | // whether a given point is in or out of the region. |
| 13 | // |
| 14 | // fn must return a bool when called with two integers (x, y). |
| 15 | template <typename PointTestFn> |
| 16 | RangeImage ThresholdPointsWithFunction(ImageFormat fmt, PointTestFn &&fn) { |
| 17 | static_assert( |
| 18 | std::is_convertible<PointTestFn, std::function<bool(int, int)>>::value, |
| 19 | "Invalid threshold function"); |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 20 | std::vector<std::vector<ImageRange>> result; |
| 21 | result.reserve(fmt.h); |
| 22 | // Iterate through each row. |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame] | 23 | for (int y = 0; y < fmt.h; ++y) { |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 24 | // Whether we're currently in a range. |
| 25 | bool in_range = false; |
| 26 | int current_range_start = -1; |
| 27 | std::vector<ImageRange> current_row_ranges; |
| 28 | // Iterate through each pixel. |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame] | 29 | for (int x = 0; x < fmt.w; ++x) { |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 30 | if (fn(x, y) != in_range) { |
| 31 | if (in_range) { |
| 32 | current_row_ranges.emplace_back(ImageRange(current_range_start, x)); |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 33 | } else { |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 34 | current_range_start = x; |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 35 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 36 | in_range = !in_range; |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 37 | } |
| 38 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 39 | if (in_range) { |
| 40 | current_row_ranges.emplace_back(ImageRange(current_range_start, fmt.w)); |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 41 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 42 | result.push_back(current_row_ranges); |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 43 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 44 | return RangeImage(0, std::move(result)); |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 47 | } // namespace threshold_internal |
| 48 | |
| 49 | // Thresholds an image using a function which determines whether a given pixel |
| 50 | // value is in or out of the region. |
| 51 | // |
| 52 | // fn must return a bool when called with a PixelRef. |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame] | 53 | template <typename ThresholdFn> |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 54 | RangeImage ThresholdImageWithFunction(const ImagePtr &img, ThresholdFn &&fn) { |
| 55 | static_assert( |
| 56 | std::is_convertible<ThresholdFn, std::function<bool(PixelRef)>>::value, |
| 57 | "Invalid threshold function"); |
| 58 | return threshold_internal::ThresholdPointsWithFunction( |
| 59 | img.fmt(), [&](int x, int y) { return fn(img.get_px(x, y)); }); |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 62 | // Thresholds an image in YUYV format, selecting pixels with a Y (luma) greater |
| 63 | // than value. |
| 64 | // |
| 65 | // This is implemented via a simple function that pulls out the Y values and |
| 66 | // compares them each. It mostly exists for tests to compare against |
| 67 | // FastYuyvYThreshold, because it's obviously correct. |
| 68 | inline RangeImage SlowYuyvYThreshold(ImageFormat fmt, const char *data, |
| 69 | uint8_t value) { |
| 70 | return threshold_internal::ThresholdPointsWithFunction( |
| 71 | fmt, [&](int x, int y) { |
| 72 | uint8_t v = data[x * 2 + y * fmt.w * 2]; |
| 73 | return v > value; |
| 74 | }); |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 77 | // Thresholds an image in YUYV format, selecting pixels with a Y (luma) greater |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame^] | 78 | // than value. The width must be a multiple of 4. |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 79 | // |
| 80 | // This is implemented via some tricky bit shuffling that goes fast. |
| 81 | RangeImage FastYuyvYThreshold(ImageFormat fmt, const char *data, uint8_t value); |
| 82 | |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 83 | } // namespace vision |
| 84 | } // namespace aos |
| 85 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 86 | #endif // AOS_VISION_BLOB_THRESHOLD_H_ |