Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 1 | #include "aos/vision/blob/threshold.h" |
| 2 | |
Stephan Pleines | cc500b9 | 2024-05-30 10:58:40 -0700 | [diff] [blame] | 3 | #include <string.h> |
| 4 | |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
| 6 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 7 | namespace aos::vision { |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 8 | namespace { |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 9 | |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 10 | constexpr int kChunkSize = 8; |
| 11 | |
| 12 | } // namespace |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 13 | |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 14 | // At a high level, the algorithm is the same as the slow thresholding, except |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 15 | // it operates in kChunkSize-pixel chunks. |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 16 | RangeImage FastYuyvYThreshold(ImageFormat fmt, const char *data, |
| 17 | uint8_t value) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 18 | AOS_CHECK_EQ(0, fmt.w % kChunkSize); |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 19 | std::vector<std::vector<ImageRange>> result; |
| 20 | result.reserve(fmt.h); |
| 21 | |
| 22 | // Iterate through each row. |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 23 | for (int y = 0; y < fmt.h; ++y) { |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 24 | // The start of the data for the current row. |
| 25 | const char *const current_row = fmt.w * y * 2 + data; |
| 26 | bool in_range = false; |
| 27 | int current_range_start = -1; |
| 28 | std::vector<ImageRange> current_row_ranges; |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 29 | // Iterate through each kChunkSize-pixel chunk |
| 30 | for (int x = 0; x < fmt.w / kChunkSize; ++x) { |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 31 | // The per-channel (YUYV) values in the current chunk. |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 32 | uint8_t chunk_channels[2 * kChunkSize]; |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 33 | memcpy(&chunk_channels[0], current_row + x * kChunkSize * 2, |
| 34 | 2 * kChunkSize); |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 35 | __builtin_prefetch(current_row + (x + 1) * kChunkSize * 2); |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 36 | |
| 37 | for (int i = 0; i < kChunkSize; ++i) { |
| 38 | if ((chunk_channels[i * 2] > value) != in_range) { |
| 39 | const int here = x * kChunkSize + i; |
| 40 | if (in_range) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 41 | current_row_ranges.emplace_back( |
| 42 | ImageRange(current_range_start, here)); |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 43 | } else { |
| 44 | current_range_start = here; |
| 45 | } |
| 46 | in_range = !in_range; |
| 47 | } |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 48 | } |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 49 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 50 | if (in_range) { |
| 51 | current_row_ranges.emplace_back(ImageRange(current_range_start, fmt.w)); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 52 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 53 | result.push_back(current_row_ranges); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 54 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 55 | return RangeImage(0, std::move(result)); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 58 | FastYuyvYPooledThresholder::FastYuyvYPooledThresholder() { |
| 59 | states_.fill(ThreadState::kWaitingForInputData); |
| 60 | for (int i = 0; i < kThreads; ++i) { |
| 61 | threads_[i] = std::thread([this, i]() { RunThread(i); }); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | FastYuyvYPooledThresholder::~FastYuyvYPooledThresholder() { |
| 66 | { |
| 67 | std::unique_lock<std::mutex> locker(mutex_); |
| 68 | quit_ = true; |
| 69 | condition_variable_.notify_all(); |
| 70 | } |
| 71 | for (int i = 0; i < kThreads; ++i) { |
| 72 | threads_[i].join(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | RangeImage FastYuyvYPooledThresholder::Threshold(ImageFormat fmt, |
| 77 | const char *data, |
| 78 | uint8_t value) { |
| 79 | input_format_ = fmt; |
| 80 | input_data_ = data; |
| 81 | input_value_ = value; |
| 82 | { |
| 83 | std::unique_lock<std::mutex> locker(mutex_); |
| 84 | for (int i = 0; i < kThreads; ++i) { |
| 85 | states_[i] = ThreadState::kProcessing; |
| 86 | } |
| 87 | condition_variable_.notify_all(); |
| 88 | while (!AllThreadsDone()) { |
| 89 | condition_variable_.wait(locker); |
| 90 | } |
| 91 | } |
| 92 | std::vector<std::vector<ImageRange>> result; |
| 93 | result.reserve(fmt.h); |
| 94 | for (int i = 0; i < kThreads; ++i) { |
| 95 | result.insert(result.end(), outputs_[i].begin(), outputs_[i].end()); |
| 96 | } |
| 97 | return RangeImage(0, std::move(result)); |
| 98 | } |
| 99 | |
| 100 | void FastYuyvYPooledThresholder::RunThread(int i) { |
| 101 | while (true) { |
| 102 | { |
| 103 | std::unique_lock<std::mutex> locker(mutex_); |
| 104 | while (states_[i] == ThreadState::kWaitingForInputData) { |
| 105 | if (quit_) { |
| 106 | return; |
| 107 | } |
| 108 | condition_variable_.wait(locker); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | ImageFormat shard_format = input_format_; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 113 | AOS_CHECK_EQ(shard_format.h % kThreads, 0); |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 114 | shard_format.h /= kThreads; |
| 115 | |
| 116 | outputs_[i] = FastYuyvYThreshold( |
| 117 | shard_format, input_data_ + shard_format.w * 2 * shard_format.h * i, |
| 118 | input_value_); |
| 119 | { |
| 120 | std::unique_lock<std::mutex> locker(mutex_); |
| 121 | states_[i] = ThreadState::kWaitingForInputData; |
| 122 | condition_variable_.notify_all(); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 127 | } // namespace aos::vision |