Apollo 10.0
自动驾驶开放平台
image_8u.h
浏览该文件的文档.
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 <map>
19#include <memory>
20
23
24namespace apollo {
25namespace perception {
26namespace base {
27
28enum class Color {
29 NONE = 0x00,
30 GRAY = 0x01,
31 RGB = 0x02,
32 BGR = 0x03,
33};
34
35const std::map<Color, int> kChannelsMap{
36 {Color::GRAY, 1}, {Color::RGB, 3}, {Color::BGR, 3}};
37
44class Image8U {
45 public:
47 : rows_(0),
48 cols_(0),
50 channels_(0),
51 width_step_(0),
52 blob_(nullptr),
53 offset_(0) {}
54
55 Image8U(int rows, int cols, Color type, std::shared_ptr<Blob<uint8_t>> blob,
56 int offset = 0)
57 : rows_(rows), cols_(cols), type_(type), blob_(blob), offset_(offset) {
59 CHECK_EQ(blob_->num_axes(), 3);
60 CHECK_EQ(blob_->shape(2), channels_);
61 CHECK_LE(offset_ + blob_->offset({rows - 1, cols - 1, channels_ - 1}),
62 (int)(blob_->count()));
63 width_step_ = blob_->offset({1, 0, 0}) * static_cast<int>(sizeof(uint8_t));
64 }
65
67 : rows_(rows), cols_(cols), type_(type), offset_(0) {
69 blob_.reset(new Blob<uint8_t>({rows_, cols_, channels_}));
70 width_step_ = blob_->offset({1, 0, 0}) * static_cast<int>(sizeof(uint8_t));
71 }
72
73 Image8U(const Image8U &src)
74 : rows_(src.rows_),
75 cols_(src.cols_),
76 type_(src.type_),
79 blob_(src.blob_),
80 offset_(src.offset_) {}
81
82 Image8U &operator=(const Image8U &src) {
83 this->rows_ = src.rows_;
84 this->cols_ = src.cols_;
85 this->type_ = src.type_;
86 this->channels_ = src.channels_;
87 this->width_step_ = src.width_step_;
88 this->blob_ = src.blob_;
89 this->offset_ = src.offset_;
90 return *this;
91 }
92
94
95 uint8_t *mutable_cpu_data() { return mutable_cpu_ptr(0); }
96
97 uint8_t *mutable_gpu_data() { return mutable_gpu_ptr(0); }
98
99 const uint8_t *cpu_data() const { return cpu_ptr(0); }
100
101 const uint8_t *gpu_data() const { return gpu_ptr(0); }
102
103 const uint8_t *cpu_ptr(int row = 0) const {
104 return blob_->cpu_data() + blob_->offset({row, 0, 0}) + offset_;
105 }
106
107 const uint8_t *gpu_ptr(int row = 0) const {
108 return blob_->gpu_data() + blob_->offset({row, 0, 0}) + offset_;
109 }
110
111 uint8_t *mutable_cpu_ptr(int row = 0) {
112 return blob_->mutable_cpu_data() + blob_->offset({row, 0, 0}) + offset_;
113 }
114
115 uint8_t *mutable_gpu_ptr(int row = 0) {
116 return blob_->mutable_gpu_data() + blob_->offset({row, 0, 0}) + offset_;
117 }
118
119 Color type() const { return type_; }
120 int rows() const { return rows_; }
121 int cols() const { return cols_; }
122 int channels() const { return channels_; }
123 int width_step() const { return width_step_; }
124 // @brief: returns the total number of pixels.
125 int total() const { return rows_ * cols_ * channels_; }
126
127 Image8U operator()(const Rect<int> &roi) const {
128 int offset = offset_ + blob_->offset({roi.y, roi.x, 0});
129 // return Image8U(roi.height, roi.width, type_, blob_, offset);
130 return Image8U(roi.height, roi.width, type_, blob_, offset);
131 }
132
133 std::shared_ptr<Blob<uint8_t>> blob() { return blob_; }
134
135 // DONOT return `std::shared_ptr<const Blob<uint8_t>> &` or `const std::... &`
136 std::shared_ptr<const Blob<uint8_t>> blob() const { return blob_; }
137
138 protected:
139 int rows_;
140 int cols_;
144 std::shared_ptr<Blob<uint8_t>> blob_;
146}; // class Image8U
147
148typedef std::shared_ptr<Image8U> Image8UPtr;
149typedef std::shared_ptr<const Image8U> Image8UConstPtr;
150
151} // namespace base
152} // namespace perception
153} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
A wrapper around Blob holders serving as the basic computational unit for images.
Definition image_8u.h:44
const uint8_t * cpu_ptr(int row=0) const
Definition image_8u.h:103
std::shared_ptr< Blob< uint8_t > > blob_
Definition image_8u.h:144
Image8U & operator=(const Image8U &src)
Definition image_8u.h:82
Image8U(int rows, int cols, Color type)
Definition image_8u.h:66
std::shared_ptr< Blob< uint8_t > > blob()
Definition image_8u.h:133
const uint8_t * cpu_data() const
Definition image_8u.h:99
uint8_t * mutable_cpu_ptr(int row=0)
Definition image_8u.h:111
Image8U(const Image8U &src)
Definition image_8u.h:73
std::shared_ptr< const Blob< uint8_t > > blob() const
Definition image_8u.h:136
const uint8_t * gpu_data() const
Definition image_8u.h:101
const uint8_t * gpu_ptr(int row=0) const
Definition image_8u.h:107
uint8_t * mutable_gpu_ptr(int row=0)
Definition image_8u.h:115
Image8U(int rows, int cols, Color type, std::shared_ptr< Blob< uint8_t > > blob, int offset=0)
Definition image_8u.h:55
Image8U operator()(const Rect< int > &roi) const
Definition image_8u.h:127
const std::map< Color, int > kChannelsMap
Definition image_8u.h:35
std::shared_ptr< Image8U > Image8UPtr
Definition image_8u.h:148
std::shared_ptr< const Image8U > Image8UConstPtr
Definition image_8u.h:149
class register implement
Definition arena_queue.h:37