• 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_CORE_CV_TENSOR_H_
17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_CV_TENSOR_H_
18 
19 #include <memory>
20 #include <utility>
21 #include <vector>
22 
23 #include <opencv2/core/mat.hpp>
24 
25 #include "./securec.h"
26 
27 #include "minddata/dataset/include/dataset/constants.h"
28 #include "minddata/dataset/core/data_type.h"
29 #include "minddata/dataset/core/tensor.h"
30 
31 namespace mindspore {
32 namespace dataset {
33 using CVTensorPtr = std::shared_ptr<CVTensor>;
34 class CVTensor : public Tensor {
35  public:
36   // Inherit Tensor's constructors
37   using Tensor::Tensor;
38 
39   /// Create a CVTensor from a given tensor. This constructor should not be used directly, use Create* instead.
40   /// The input tensor will be invalidated (i.e., the shape and type will be
41   /// set to unknown and the data buffer will point to null.
42   /// \note there is no memory copying here, the buffer will be assigned to the constructed tensor.
43   /// \param tensor
44   explicit CVTensor(std::shared_ptr<Tensor> tensor);
45 
46   /// Create CV tensor with type and shape. Items of the tensor would be uninitialized.
47   /// \param shape [in] shape of the output tensor
48   /// \param type [in] type of the output tensor
49   /// \param out [out] Generated tensor
50   /// \return Status code
51   static Status CreateEmpty(const TensorShape &shape, DataType type, CVTensorPtr *out);
52 
53   /// Create CV tensor from cv::Mat
54   /// \note This constructor allocates a new space in the memory and copies the CV::Mat buffer into it.
55   /// \param mat [in] cv::Mat to be copied into the new tensor.
56   /// \param shape [in] the rank of output CVTensor.
57   /// \param out [out] Generated tensor
58   /// \return Status code
59   static Status CreateFromMat(const cv::Mat &mat, const dsize_t rank, CVTensorPtr *out);
60 
61   ~CVTensor() override = default;
62 
63   /// Static function to cast a given Tensor as CVTensor. If the input tensor is already of type CVTensor,
64   /// this function would be treated as a no-op. Fot other tensor types, a new CVTensor is created based on the data
65   /// provided. The Passed Tensor will be invalidated.
66   /// \note the input tensor will be invalidated.
67   /// \note there is no memory copying here, the buffer will be assigned to the constructed tensor.
68   /// \param tensor [in]
69   /// \return CVTensor
70   static std::shared_ptr<CVTensor> AsCVTensor(std::shared_ptr<Tensor> tensor);
71 
72   /// Get a reference to the CV::Mat
73   /// \return a reference to the internal CV::Mat
mat()74   cv::Mat &mat() { return mat_; }
75 
76   /// Get a copy of the CV::Mat
77   /// \return a copy of internal CV::Mat
matCopy()78   cv::Mat matCopy() const { return mat_.clone(); }
79 
80   /// Static function to check if the passed information (shape and type) can be treated as a valid description
81   /// of an image in OpenCV. Moreover, it returns OpenCV shape and type
82   /// For example, if the shape is <512,512,3> and type is DE_UINT8, the output would be [512,512] and CV_8UC3.
83   /// In case of invalid shape or type, the function will return pair<null,0>
84   /// \param shape [in] TensorShape
85   /// \param type [in] DataType
86   /// \return std::pair of OpenCV shape and type
87   static std::pair<std::array<int, 2>, int> IsValidImage(const TensorShape &shape, const DataType &type);
88 
89   Status Reshape(const TensorShape &shape) override;
90 
91   Status ExpandDim(const dsize_t &axis) override;
92 
93   void Squeeze() override;
94 
95   Status MatAtIndex(const std::vector<dsize_t> &index, cv::Mat *mat);
96 
97  private:
98   /// Opencv Mat object wrapping the raw data of the tensor.
99   /// Modifying the content of the matrix, modifies the tensor.
100   cv::Mat mat_;
101 
102   /// Create cv::Mat from data, TensorShape and DataType
103   /// \param data [in] Pointer to the data in memory.
104   /// \param shape [in] Shape of the tensor.
105   /// \param type [in] Type of the tensor.
106   /// \param mat [out] cv::Mat initialized with the provided data.
107   /// \return Status code
108   Status MatInit(uchar *data, const TensorShape &shape, const DataType &type, cv::Mat *mat);
109 };
110 }  // namespace dataset
111 }  // namespace mindspore
112 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_CV_TENSOR_H_
113