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