Apollo  6.0
Open source self driving car software
imconv.h
Go to the documentation of this file.
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 /*
18 Copyright (C) 2006 Pedro Felzenszwalb
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31 /* image conversion */
32 #pragma once
33 
34 #include <limits>
35 #include "modules/perception/lidar/detector/ncut_segmentation/common/graph_felzenszwalb/image.h"
36 #include "modules/perception/lidar/detector/ncut_segmentation/common/graph_felzenszwalb/imutil.h"
37 #include "modules/perception/lidar/detector/ncut_segmentation/common/graph_felzenszwalb/misc.h"
38 
39 namespace apollo {
40 namespace perception {
41 namespace lidar {
42 const double RED_WEIGHT = 0.299;
43 const double GREEN_WEIGHT = 0.587;
44 const double BLUE_WEIGHT = 0.114;
46  int width = input->width();
47  int height = input->height();
48  Image<uchar> *output = new Image<uchar>(width, height, false);
49  for (int y = 0; y < height; y++) {
50  for (int x = 0; x < width; x++) {
51  imRef(output, x, y) = (uchar)(imRef(input, x, y).r * RED_WEIGHT +
52  imRef(input, x, y).g * GREEN_WEIGHT +
53  imRef(input, x, y).b * BLUE_WEIGHT);
54  }
55  }
56  return output;
57 }
59  int width = input->width();
60  int height = input->height();
61  Image<rgb> *output = new Image<rgb>(width, height, false);
62  for (int y = 0; y < height; y++) {
63  for (int x = 0; x < width; x++) {
64  imRef(output, x, y).r = imRef(input, x, y);
65  imRef(output, x, y).g = imRef(input, x, y);
66  imRef(output, x, y).b = imRef(input, x, y);
67  }
68  }
69  return output;
70 }
72  int width = input->width();
73  int height = input->height();
74  Image<float> *output = new Image<float>(width, height, false);
75  for (int y = 0; y < height; y++) {
76  for (int x = 0; x < width; x++) {
77  imRef(output, x, y) = imRef(input, x, y);
78  }
79  }
80  return output;
81 }
83  int width = input->width();
84  int height = input->height();
85  Image<float> *output = new Image<float>(width, height, false);
86  for (int y = 0; y < height; y++) {
87  for (int x = 0; x < width; x++) {
88  imRef(output, x, y) = imRef(input, x, y);
89  }
90  }
91  return output;
92 }
93 Image<uchar> *image_float2uchar(Image<float> *input, float min, float max) {
94  int width = input->width();
95  int height = input->height();
96  Image<uchar> *output = new Image<uchar>(width, height, false);
97  if (max == min) {
98  return output;
99  }
100  float scale = std::numeric_limits<unsigned char>::max() / (max - min);
101  for (int y = 0; y < height; y++) {
102  for (int x = 0; x < width; x++) {
103  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
104  imRef(output, x, y) = bound(
105  val, (uchar)0, (uchar)std::numeric_limits<unsigned char>::max());
106  }
107  }
108  return output;
109 }
111  float min, max;
112  min_max(input, &min, &max);
113  return image_float2uchar(input, min, max);
114 }
116  int width = input->width();
117  int height = input->height();
118  Image<uint32_t> *output = new Image<uint32_t>(width, height, false);
119  for (int y = 0; y < height; y++) {
120  for (int x = 0; x < width; x++) {
121  imRef(output, x, y) = imRef(input, x, y);
122  }
123  }
124  return output;
125 }
127  uint32_t max) {
128  int width = input->width();
129  int height = input->height();
130  Image<uchar> *output = new Image<uchar>(width, height, false);
131  if (max == min) {
132  return output;
133  }
134  float scale =
135  std::numeric_limits<unsigned char>::max() / static_cast<float>(max - min);
136  for (int y = 0; y < height; y++) {
137  for (int x = 0; x < width; x++) {
138  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
139  imRef(output, x, y) = bound(
140  val, (uchar)0, (uchar)std::numeric_limits<unsigned char>::max());
141  }
142  }
143  return output;
144 }
146  uint32_t min, max;
147  min_max(input, &min, &max);
148  return image_long2uchar(input, min, max);
149 }
151  uint16_t max) {
152  int width = input->width();
153  int height = input->height();
154  Image<uchar> *output = new Image<uchar>(width, height, false);
155  if (max == min) {
156  return output;
157  }
158  float scale =
159  std::numeric_limits<unsigned char>::max() / static_cast<float>(max - min);
160  for (int y = 0; y < height; y++) {
161  for (int x = 0; x < width; x++) {
162  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
163  imRef(output, x, y) = bound(
164  val, (uchar)0, (uchar)std::numeric_limits<unsigned char>::max());
165  }
166  }
167  return output;
168 }
170  uint16_t min, max;
171  min_max(input, &min, &max);
172  return image_short2uchar(input, min, max);
173 }
174 } // namespace lidar
175 } // namespace perception
176 } // namespace apollo
int height() const
Definition: image.h:55
Image< uchar > * image_long2uchar(Image< uint32_t > *input, uint32_t min, uint32_t max)
Definition: imconv.h:126
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Image< uchar > * image_rgb2gray(Image< rgb > *input)
Definition: imconv.h:45
int width() const
Definition: image.h:52
T bound(const T &x, const T &min, const T &max)
Definition: misc.h:60
Definition: image.h:40
void min_max(Image< T > *im, T *ret_min, T *ret_max)
Definition: imutil.h:42
Image< uchar > * image_short2uchar(Image< uint16_t > *input, uint16_t min, uint16_t max)
Definition: imconv.h:150
Image< float > * image_uchar2float(Image< uchar > *input)
Definition: imconv.h:71
Image< uchar > * image_float2uchar(Image< float > *input, float min, float max)
Definition: imconv.h:93
Image< float > * image_int2float(Image< int > *input)
Definition: imconv.h:82
const double BLUE_WEIGHT
Definition: imconv.h:44
const double GREEN_WEIGHT
Definition: imconv.h:43
Image< rgb > * image_gray2rgb(Image< uchar > *input)
Definition: imconv.h:58
unsigned char uchar
Definition: misc.h:38
Image< uint32_t > * image_uchar2long(Image< uchar > *input)
Definition: imconv.h:115
#define imRef(im, x, y)
Definition: image.h:68
const double RED_WEIGHT
Definition: imconv.h:42