blob: 1a10c2c30a00078e1d34ede584771f82095c2e9e [file] [log] [blame]
John Richardson0434d752017-10-30 13:02:37 +00001/*
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
26namespace arm_compute
27{
28namespace test
29{
30namespace validation
31{
32namespace reference
33{
34template <typename T>
35SimpleTensor<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 Giorgio7cd70212018-09-05 16:29:28 +010069 const float diff = total_num_pixels - cd_min;
John Richardson0434d752017-10-30 13:02:37 +000070
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
86template SimpleTensor<uint8_t> equalize_histogram(const SimpleTensor<uint8_t> &src);
87} // namespace reference
88} // namespace validation
89} // namespace test
90} // namespace arm_compute