• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_ROTATION_OP_H_
17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_ROTATION_OP_H_
18 
19 #include <memory>
20 #include <random>
21 #include <vector>
22 #include <string>
23 
24 #include "minddata/dataset/core/tensor.h"
25 #include "minddata/dataset/kernels/tensor_op.h"
26 #include "minddata/dataset/util/status.h"
27 #include "minddata/dataset/kernels/image/image_utils.h"
28 
29 namespace mindspore {
30 namespace dataset {
31 class RandomRotationOp : public TensorOp {
32  public:
33   // Default values, also used by python_bindings.cc
34   static const std::vector<float> kDefCenter;
35   static const InterpolationMode kDefInterpolation;
36   static const bool kDefExpand;
37   static const uint8_t kDefFillR;
38   static const uint8_t kDefFillG;
39   static const uint8_t kDefFillB;
40 
41   // Constructor for RandomRotationOp
42   // @param startDegree starting range for random degree
43   // @param endDegree ending range for random degree
44   // @param interpolation DE interpolation mode for rotation
45   // @param expand option for the output image shape to change
46   // @param center coordinate for center of image rotation
47   // @param fill_r R value for the color to pad with
48   // @param fill_g G value for the color to pad with
49   // @param fill_b B value for the color to pad with
50   // @details the randomly chosen degree is uniformly distributed
51   // @details the output shape, if changed, will contain the entire rotated image
52   // @note maybe using unsigned long int isn't the best here according to our coding rules
53   RandomRotationOp(float start_degree, float end_degree, InterpolationMode resample = kDefInterpolation,
54                    bool expand = kDefExpand, std::vector<float> center = kDefCenter, uint8_t fill_r = kDefFillR,
55                    uint8_t fill_g = kDefFillG, uint8_t fill_b = kDefFillB);
56 
57   ~RandomRotationOp() override = default;
58 
59   // Overrides the base class compute function
60   // Calls the rotate function in ImageUtils, this function takes an input tensor
61   // and transforms its data using openCV, the output memory is manipulated to contain the result
62   // @return Status The status code returned
63   Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
64 
65   Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
66 
Name()67   std::string Name() const override { return kRandomRotationOp; }
68 
69  private:
70   float degree_start_;
71   float degree_end_;
72   std::vector<float> center_;
73   InterpolationMode interpolation_;
74   bool expand_;
75   uint8_t fill_r_;
76   uint8_t fill_g_;
77   uint8_t fill_b_;
78   std::uniform_real_distribution<float> distribution_{-1.0, 1.0};
79   std::mt19937 rnd_;
80 };
81 }  // namespace dataset
82 }  // namespace mindspore
83 
84 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_ROTATION_OP_H_
85