John Richardson | 0434d75 | 2017-10-30 13:02:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "EqualizeHistogram.h" |
| 25 | |
| 26 | namespace arm_compute |
| 27 | { |
| 28 | namespace test |
| 29 | { |
| 30 | namespace validation |
| 31 | { |
| 32 | namespace reference |
| 33 | { |
| 34 | template <typename T> |
| 35 | SimpleTensor<T> equalize_histogram(const SimpleTensor<T> &src) |
| 36 | { |
| 37 | const size_t num_bins = 256; // 0-255 inclusive |
| 38 | |
| 39 | std::vector<T> lut(num_bins); |
| 40 | std::vector<uint32_t> hist(num_bins); |
| 41 | std::vector<uint32_t> cd(num_bins); // cumulative distribution |
| 42 | |
| 43 | SimpleTensor<T> dst(src.shape(), src.data_type()); |
| 44 | |
| 45 | // Create the histogram |
| 46 | for(int element_idx = 0; element_idx < src.num_elements(); ++element_idx) |
| 47 | { |
| 48 | hist[src[element_idx]]++; |
| 49 | } |
| 50 | |
| 51 | // Calculate cumulative distribution |
| 52 | std::partial_sum(hist.begin(), hist.end(), cd.begin()); |
| 53 | |
| 54 | // Get the number of pixels that have the lowest non-zero value |
| 55 | const uint32_t cd_min = *std::find_if(hist.begin(), hist.end(), [](const uint32_t &x) |
| 56 | { |
| 57 | return x > 0; |
| 58 | }); |
| 59 | |
| 60 | const size_t total_num_pixels = cd.back(); |
| 61 | |
| 62 | // Single color - create linear distribution |
| 63 | if(total_num_pixels == cd_min) |
| 64 | { |
| 65 | std::iota(lut.begin(), lut.end(), 0); |
| 66 | } |
| 67 | else |
| 68 | { |
Michele Di Giorgio | 7cd7021 | 2018-09-05 16:29:28 +0100 | [diff] [blame] | 69 | const float diff = total_num_pixels - cd_min; |
John Richardson | 0434d75 | 2017-10-30 13:02:37 +0000 | [diff] [blame] | 70 | |
| 71 | for(size_t i = 0; i < num_bins; ++i) |
| 72 | { |
| 73 | lut[i] = lround((cd[i] - cd_min) / diff * 255.f); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Fill output tensor with equalized values |
| 78 | for(int i = 0; i < src.num_elements(); ++i) |
| 79 | { |
| 80 | dst[i] = lut[src[i]]; |
| 81 | } |
| 82 | |
| 83 | return dst; |
| 84 | } |
| 85 | |
| 86 | template SimpleTensor<uint8_t> equalize_histogram(const SimpleTensor<uint8_t> &src); |
| 87 | } // namespace reference |
| 88 | } // namespace validation |
| 89 | } // namespace test |
| 90 | } // namespace arm_compute |