Apollo  v5.5.0
Open source self driving car software
data_provider.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 #pragma once
17 
18 #include <nppi.h>
19 
20 #include <memory>
21 #include <string>
22 
27 
28 namespace apollo {
29 namespace perception {
30 namespace camera {
31 
32 class DataProvider {
33  public:
34  struct InitOptions {
36  : image_height(0),
37  image_width(0),
38  device_id(-1),
39  do_undistortion(false) {}
40 
43  int device_id;
45  std::string sensor_name;
46  };
47 
48  struct ImageOptions {
50  this->target_color = base::Color::NONE;
51  this->do_crop = false;
52  }
53 
54  ImageOptions(base::Color target_color, bool do_crop, base::RectI crop_roi) {
55  this->target_color = target_color;
56  this->do_crop = do_crop;
57  this->crop_roi = crop_roi;
58  }
59 
60  std::string ToString() {
61  std::stringstream ss;
62  ss << " " << static_cast<int>(target_color);
63  ss << " " << do_crop;
64  if (do_crop) {
65  ss << " " << crop_roi.x << " " << crop_roi.y << " " << crop_roi.width
66  << " " << crop_roi.height;
67  }
68  return ss.str();
69  }
70 
71  base::Color target_color = base::Color::BGR;
72  bool do_crop = false; // default: DONOT crop
74  };
75 
76  DataProvider() = default;
77  ~DataProvider() = default;
78 
79  DataProvider(const DataProvider &) = delete;
80  DataProvider &operator=(const DataProvider &) = delete;
81 
82  bool Init(const InitOptions &options = InitOptions());
83 
84  // @brief: fill raw image data.
85  // @param [in]: options
86  // @param [in/out]: blob
87  // image blob with specified size should be filled, required.
88  bool FillImageData(int rows, int cols, const uint8_t *data,
89  const std::string &encoding);
90 
91 #if 0
92  // @brief: get blob converted from raw message.
93  // @param [in]: options
94  // @param [in/out]: NHWC blob (4D)
95  // image blob with specified size should be filled, required.
96  bool GetImageBlob(const ImageOptions &options, base::Blob<float> *blob);
97 #endif
98 
99  // @brief: get blob converted from raw message.
100  // @param [in]: options
101  // @param [in/out]: NHWC blob (4D)
102  // image blob with specified size should be filled, required.
103  bool GetImageBlob(const ImageOptions &options, base::Blob<uint8_t> *blob);
104 
105  // @brief: get Image8U converted from raw message.
106  // @param [in]: options
107  // @return: Image8U
108  // image blob with specified size should be filled, required.
109  bool GetImage(const ImageOptions &options, base::Image8U *image);
110 
111  int src_height() const { return src_height_; }
112  int src_width() const { return src_width_; }
113  const std::string &sensor_name() const { return sensor_name_; }
114 
115  bool to_gray_image();
116  bool to_rgb_image();
117  bool to_bgr_image();
118 
119  protected:
120  std::string sensor_name_;
121  int src_height_ = 0;
122  int src_width_ = 0;
123  int device_id_ = -1;
124 
125  std::shared_ptr<base::Image8U> ori_gray_;
126  std::shared_ptr<base::Image8U> ori_rgb_;
127  std::shared_ptr<base::Image8U> ori_bgr_;
128  std::shared_ptr<base::Image8U> gray_;
129  std::shared_ptr<base::Image8U> rgb_;
130  std::shared_ptr<base::Image8U> bgr_;
131  bool gray_ready_ = false;
132  bool rgb_ready_ = false;
133  bool bgr_ready_ = false;
134 
137  std::shared_ptr<UndistortionHandler> handler_ = nullptr;
138 }; // class DataProvider
139 
140 } // namespace camera
141 } // namespace perception
142 } // namespace apollo
std::shared_ptr< base::Image8U > ori_bgr_
Definition: data_provider.h:127
Definition: data_provider.h:32
bool GetImageBlob(const ImageOptions &options, base::Blob< uint8_t > *blob)
std::shared_ptr< base::Image8U > rgb_
Definition: data_provider.h:129
std::shared_ptr< base::Image8U > ori_rgb_
Definition: data_provider.h:126
std::string sensor_name
Definition: data_provider.h:45
Definition: blob.h:72
base::Blob< float > temp_float_
Definition: data_provider.h:135
const std::string & sensor_name() const
Definition: data_provider.h:113
std::shared_ptr< base::Image8U > ori_gray_
Definition: data_provider.h:125
A wrapper around Blob holders serving as the basic computational unit for images. ...
Definition: image_8u.h:44
std::shared_ptr< base::Image8U > gray_
Definition: data_provider.h:128
ImageOptions(base::Color target_color, bool do_crop, base::RectI crop_roi)
Definition: data_provider.h:54
int src_height_
Definition: data_provider.h:121
bool rgb_ready_
Definition: data_provider.h:132
int src_width() const
Definition: data_provider.h:112
base::RectI crop_roi
Definition: data_provider.h:73
bool bgr_ready_
Definition: data_provider.h:133
int image_width
Definition: data_provider.h:42
int src_height() const
Definition: data_provider.h:111
bool FillImageData(int rows, int cols, const uint8_t *data, const std::string &encoding)
std::shared_ptr< base::Image8U > bgr_
Definition: data_provider.h:130
bool do_undistortion
Definition: data_provider.h:44
DataProvider & operator=(const DataProvider &)=delete
bool Init(const InitOptions &options=InitOptions())
int src_width_
Definition: data_provider.h:122
bool gray_ready_
Definition: data_provider.h:131
int image_height
Definition: data_provider.h:41
std::shared_ptr< UndistortionHandler > handler_
Definition: data_provider.h:137
std::string sensor_name_
Definition: data_provider.h:120
int device_id
Definition: data_provider.h:43
bool GetImage(const ImageOptions &options, base::Image8U *image)
base::Blob< uint8_t > temp_uint8_
Definition: data_provider.h:136
int device_id_
Definition: data_provider.h:123
std::string ToString()
Definition: data_provider.h:60
Color
Definition: image_8u.h:28