1 /**
2 * Copyright 2020 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 <string>
21
22 namespace mindspore {
23 namespace dataset {
ComputeUpperAndLowerPercentiles(std::vector<int32_t> * hist,int32_t hi_p,int32_t low_p,int32_t * hi,int32_t * lo)24 Status ComputeUpperAndLowerPercentiles(std::vector<int32_t> *hist, int32_t hi_p, int32_t low_p, int32_t *hi,
25 int32_t *lo) {
26 CHECK_FAIL_RETURN_UNEXPECTED(hist != nullptr, "hist is nullptr");
27 CHECK_FAIL_RETURN_UNEXPECTED(hi != nullptr, "hi is nullptr");
28 CHECK_FAIL_RETURN_UNEXPECTED(lo != nullptr, "lo is nullptr");
29 try {
30 int32_t n = std::accumulate(hist->begin(), hist->end(), 0);
31 constexpr float kMaxPerc = 100.0;
32 int32_t cut = static_cast<int32_t>((low_p / kMaxPerc) * n);
33 for (int32_t lb = 0; lb < hist->size() && cut > 0; lb++) {
34 if (cut > (*hist)[lb]) {
35 cut -= (*hist)[lb];
36 (*hist)[lb] = 0;
37 } else {
38 (*hist)[lb] -= cut;
39 cut = 0;
40 }
41 }
42 cut = static_cast<int32_t>((hi_p / kMaxPerc) * n);
43 for (auto ub_iter = hist->end() - 1; ub_iter >= hist->begin() && cut > 0; ub_iter--) {
44 if (cut > *ub_iter) {
45 cut -= *ub_iter;
46 *ub_iter = 0;
47 } else {
48 *ub_iter -= cut;
49 cut = 0;
50 }
51 }
52 *lo = 0;
53 *hi = hist->size() - 1;
54 for (; (*lo) < (*hi) && !(*hist)[*lo]; (*lo)++) {
55 }
56 for (; (*hi) >= 0 && !(*hist)[*hi]; (*hi)--) {
57 }
58 } catch (const std::exception &e) {
59 std::string err_message = "AutoContrast: ComputeUpperAndLowerPercentiles failed: ";
60 err_message += e.what();
61 RETURN_STATUS_UNEXPECTED(err_message);
62 }
63 return Status::OK();
64 }
65
DegreesToRadians(float_t degrees,float_t * radians_target)66 Status DegreesToRadians(float_t degrees, float_t *radians_target) {
67 CHECK_FAIL_RETURN_UNEXPECTED(radians_target != nullptr, "radians_target is nullptr");
68 *radians_target = CV_PI * degrees / 180.0;
69 return Status::OK();
70 }
71
GenerateRealNumber(float_t a,float_t b,std::mt19937 * rnd,float_t * result)72 Status GenerateRealNumber(float_t a, float_t b, std::mt19937 *rnd, float_t *result) {
73 CHECK_FAIL_RETURN_UNEXPECTED(rnd != nullptr, "rnd is nullptr");
74 CHECK_FAIL_RETURN_UNEXPECTED(result != nullptr, "result is nullptr");
75 try {
76 std::uniform_real_distribution<float_t> distribution{a, b};
77 *result = distribution(*rnd);
78 } catch (const std::exception &e) {
79 std::string err_message = "RandomAffine: GenerateRealNumber failed: ";
80 err_message += e.what();
81 RETURN_STATUS_UNEXPECTED(err_message);
82 }
83 return Status::OK();
84 }
85
86 } // namespace dataset
87 } // namespace mindspore
88