Apollo 11.0
自动驾驶开放平台
imconv.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
17/*
18Copyright (C) 2006 Pedro Felzenszwalb
19This program is free software; you can redistribute it and/or modify
20it under the terms of the GNU General Public License as published by
21the Free Software Foundation; either version 2 of the License, or
22(at your option) any later version.
23This program is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27You should have received a copy of the GNU General Public License
28along with this program; if not, write to the Free Software
29Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30*/
31/* image conversion */
32#pragma once
33
34#include <limits>
35
39
40namespace apollo {
41namespace perception {
42namespace lidar {
43const double RED_WEIGHT = 0.299;
44const double GREEN_WEIGHT = 0.587;
45const double BLUE_WEIGHT = 0.114;
53 int width = input->width();
54 int height = input->height();
55 Image<uchar> *output = new Image<uchar>(width, height, false);
56 for (int y = 0; y < height; y++) {
57 for (int x = 0; x < width; x++) {
58 imRef(output, x, y) = (uchar)(imRef(input, x, y).r * RED_WEIGHT + imRef(input, x, y).g * GREEN_WEIGHT
59 + imRef(input, x, y).b * BLUE_WEIGHT);
60 }
61 }
62 return output;
63}
71 int width = input->width();
72 int height = input->height();
73 Image<rgb> *output = new Image<rgb>(width, height, false);
74 for (int y = 0; y < height; y++) {
75 for (int x = 0; x < width; x++) {
76 imRef(output, x, y).r = imRef(input, x, y);
77 imRef(output, x, y).g = imRef(input, x, y);
78 imRef(output, x, y).b = imRef(input, x, y);
79 }
80 }
81 return output;
82}
90 int width = input->width();
91 int height = input->height();
92 Image<float> *output = new Image<float>(width, height, false);
93 for (int y = 0; y < height; y++) {
94 for (int x = 0; x < width; x++) {
95 imRef(output, x, y) = imRef(input, x, y);
96 }
97 }
98 return output;
99}
107 int width = input->width();
108 int height = input->height();
109 Image<float> *output = new Image<float>(width, height, false);
110 for (int y = 0; y < height; y++) {
111 for (int x = 0; x < width; x++) {
112 imRef(output, x, y) = imRef(input, x, y);
113 }
114 }
115 return output;
116}
125Image<uchar> *image_float2uchar(Image<float> *input, float min, float max) {
126 int width = input->width();
127 int height = input->height();
128 Image<uchar> *output = new Image<uchar>(width, height, false);
129 if (max == min) {
130 return output;
131 }
132 float scale = std::numeric_limits<unsigned char>::max() / (max - min);
133 for (int y = 0; y < height; y++) {
134 for (int x = 0; x < width; x++) {
135 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
136 imRef(output, x, y) = bound(val, (uchar)0, (uchar)std::numeric_limits<unsigned char>::max());
137 }
138 }
139 return output;
140}
148 float min, max;
149 min_max(input, &min, &max);
150 return image_float2uchar(input, min, max);
151}
159 int width = input->width();
160 int height = input->height();
161 Image<uint32_t> *output = new Image<uint32_t>(width, height, false);
162 for (int y = 0; y < height; y++) {
163 for (int x = 0; x < width; x++) {
164 imRef(output, x, y) = imRef(input, x, y);
165 }
166 }
167 return output;
168}
177Image<uchar> *image_long2uchar(Image<uint32_t> *input, uint32_t min, uint32_t max) {
178 int width = input->width();
179 int height = input->height();
180 Image<uchar> *output = new Image<uchar>(width, height, false);
181 if (max == min) {
182 return output;
183 }
184 float scale = std::numeric_limits<unsigned char>::max() / static_cast<float>(max - min);
185 for (int y = 0; y < height; y++) {
186 for (int x = 0; x < width; x++) {
187 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
188 imRef(output, x, y) = bound(val, (uchar)0, (uchar)std::numeric_limits<unsigned char>::max());
189 }
190 }
191 return output;
192}
200 uint32_t min, max;
201 min_max(input, &min, &max);
202 return image_long2uchar(input, min, max);
203}
212Image<uchar> *image_short2uchar(Image<uint16_t> *input, uint16_t min, uint16_t max) {
213 int width = input->width();
214 int height = input->height();
215 Image<uchar> *output = new Image<uchar>(width, height, false);
216 if (max == min) {
217 return output;
218 }
219 float scale = std::numeric_limits<unsigned char>::max() / static_cast<float>(max - min);
220 for (int y = 0; y < height; y++) {
221 for (int x = 0; x < width; x++) {
222 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
223 imRef(output, x, y) = bound(val, (uchar)0, (uchar)std::numeric_limits<unsigned char>::max());
224 }
225 }
226 return output;
227}
235 uint16_t min, max;
236 min_max(input, &min, &max);
237 return image_short2uchar(input, min, max);
238}
239} // namespace lidar
240} // namespace perception
241} // namespace apollo
int height() const
Get the height of an image
Definition image.h:73
int width() const
Get the width of an image
Definition image.h:64
#define imRef(im, x, y)
Definition image.h:88
Image< float > * image_uchar2float(Image< uchar > *input)
Transform uchar to float
Definition imconv.h:89
const double BLUE_WEIGHT
Definition imconv.h:45
Image< uchar > * image_long2uchar(Image< uint32_t > *input, uint32_t min, uint32_t max)
Transform long to uchar
Definition imconv.h:177
Image< rgb > * image_gray2rgb(Image< uchar > *input)
Tranform gray to rgb
Definition imconv.h:70
void min_max(Image< T > *im, T *ret_min, T *ret_max)
Compute minimum and maximum value in an image
Definition imutil.h:50
Image< uchar > * image_float2uchar(Image< float > *input, float min, float max)
Transform float to uchar
Definition imconv.h:125
Image< float > * image_int2float(Image< int > *input)
Transform int to float
Definition imconv.h:106
Image< uint32_t > * image_uchar2long(Image< uchar > *input)
Transform unchar to long
Definition imconv.h:158
Image< uchar > * image_rgb2gray(Image< rgb > *input)
Transform rgb to gray
Definition imconv.h:52
Image< uchar > * image_short2uchar(Image< uint16_t > *input, uint16_t min, uint16_t max)
Transform short to uchar
Definition imconv.h:212
unsigned char uchar
Definition misc.h:38
const double RED_WEIGHT
Definition imconv.h:43
const double GREEN_WEIGHT
Definition imconv.h:44
T bound(const T &x, const T &min, const T &max)
Get bound of value x
Definition misc.h:98
class register implement
Definition arena_queue.h:37