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