1 /**
2 * Copyright 2020-2023 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "minddata/dataset/kernels/image/math_utils.h"
18
19 #include <algorithm>
20 #include <numeric>
21 #include <string>
22
23 namespace mindspore {
24 namespace dataset {
ComputeUpperAndLowerPercentiles(std::vector<int32_t> * hist,int32_t hi_p,int32_t low_p,int32_t * hi,int32_t * lo)25 Status ComputeUpperAndLowerPercentiles(std::vector<int32_t> *hist, int32_t hi_p, int32_t low_p, int32_t *hi,
26 int32_t *lo) {
27 CHECK_FAIL_RETURN_UNEXPECTED(hist != nullptr, "hist is nullptr");
28 CHECK_FAIL_RETURN_UNEXPECTED(hi != nullptr, "hi is nullptr");
29 CHECK_FAIL_RETURN_UNEXPECTED(lo != nullptr, "lo is nullptr");
30 try {
31 int32_t n = std::accumulate(hist->begin(), hist->end(), 0);
32 constexpr float kMaxPerc = 100.0;
33 auto cut = static_cast<int32_t>(static_cast<float>(low_p) / kMaxPerc * static_cast<float>(n));
34 for (int32_t lb = 0; lb < static_cast<int32_t>(hist->size()) && cut > 0; lb++) {
35 if (cut > (*hist)[lb]) {
36 cut -= (*hist)[lb];
37 (*hist)[lb] = 0;
38 } else {
39 (*hist)[lb] -= cut;
40 cut = 0;
41 }
42 }
43 cut = static_cast<int32_t>(static_cast<float>(hi_p) / kMaxPerc * static_cast<float>(n));
44 for (auto ub_iter = hist->end() - 1; ub_iter >= hist->begin() && cut > 0; ub_iter--) {
45 if (cut > *ub_iter) {
46 cut -= *ub_iter;
47 *ub_iter = 0;
48 } else {
49 *ub_iter -= cut;
50 cut = 0;
51 }
52 }
53 *lo = 0;
54 *hi = static_cast<int32_t>(hist->size()) - 1;
55 for (; (*lo) < (*hi) && !(*hist)[*lo]; (*lo)++) {
56 }
57 for (; (*hi) >= 0 && !(*hist)[*hi]; (*hi)--) {
58 }
59 } catch (const std::exception &e) {
60 std::string err_message = "AutoContrast: ComputeUpperAndLowerPercentiles failed: ";
61 err_message += e.what();
62 RETURN_STATUS_UNEXPECTED(err_message);
63 }
64 return Status::OK();
65 }
66
DegreesToRadians(float_t degrees,float_t * radians_target)67 Status DegreesToRadians(float_t degrees, float_t *radians_target) {
68 CHECK_FAIL_RETURN_UNEXPECTED(radians_target != nullptr, "radians_target is nullptr");
69 *radians_target = CV_PI * degrees / 180.0;
70 return Status::OK();
71 }
72
GenerateRealNumber(float_t a,float_t b,std::mt19937 * rnd,float_t * result)73 Status GenerateRealNumber(float_t a, float_t b, std::mt19937 *rnd, float_t *result) {
74 CHECK_FAIL_RETURN_UNEXPECTED(rnd != nullptr, "rnd is nullptr");
75 CHECK_FAIL_RETURN_UNEXPECTED(result != nullptr, "result is nullptr");
76 try {
77 std::uniform_real_distribution<float_t> distribution{a, b};
78 *result = distribution(*rnd);
79 } catch (const std::exception &e) {
80 std::string err_message = "RandomAffine: GenerateRealNumber failed: ";
81 err_message += e.what();
82 RETURN_STATUS_UNEXPECTED(err_message);
83 }
84 return Status::OK();
85 }
86 } // namespace dataset
87 } // namespace mindspore
88