Apollo  v5.5.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 #include <climits>
34 #include "modules/perception/lidar/segmentation/ncut/common/graph_felzenszwalb/image.h"
35 #include "modules/perception/lidar/segmentation/ncut/common/graph_felzenszwalb/imutil.h"
36 #include "modules/perception/lidar/segmentation/ncut/common/graph_felzenszwalb/misc.h"
37 
38 namespace apollo {
39 namespace perception {
40 namespace lidar {
41 const double RED_WEIGHT = 0.299;
42 const double GREEN_WEIGHT = 0.587;
43 const double BLUE_WEIGHT = 0.114;
45  int width = input->width();
46  int height = input->height();
47  Image<uchar> *output = new Image<uchar>(width, height, false);
48  for (int y = 0; y < height; y++) {
49  for (int x = 0; x < width; x++) {
50  imRef(output, x, y) = (uchar)(imRef(input, x, y).r * RED_WEIGHT +
51  imRef(input, x, y).g * GREEN_WEIGHT +
52  imRef(input, x, y).b * BLUE_WEIGHT);
53  }
54  }
55  return output;
56 }
58  int width = input->width();
59  int height = input->height();
60  Image<rgb> *output = new Image<rgb>(width, height, false);
61  for (int y = 0; y < height; y++) {
62  for (int x = 0; x < width; x++) {
63  imRef(output, x, y).r = imRef(input, x, y);
64  imRef(output, x, y).g = imRef(input, x, y);
65  imRef(output, x, y).b = imRef(input, x, y);
66  }
67  }
68  return output;
69 }
71  int width = input->width();
72  int height = input->height();
73  Image<float> *output = new Image<float>(width, height, false);
74  for (int y = 0; y < height; y++) {
75  for (int x = 0; x < width; x++) {
76  imRef(output, x, y) = imRef(input, x, y);
77  }
78  }
79  return output;
80 }
82  int width = input->width();
83  int height = input->height();
84  Image<float> *output = new Image<float>(width, height, false);
85  for (int y = 0; y < height; y++) {
86  for (int x = 0; x < width; x++) {
87  imRef(output, x, y) = imRef(input, x, y);
88  }
89  }
90  return output;
91 }
92 Image<uchar> *image_float2uchar(Image<float> *input, float min, float max) {
93  int width = input->width();
94  int height = input->height();
95  Image<uchar> *output = new Image<uchar>(width, height, false);
96  if (max == min) {
97  return output;
98  }
99  float scale = UCHAR_MAX / (max - min);
100  for (int y = 0; y < height; y++) {
101  for (int x = 0; x < width; x++) {
102  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
103  imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
104  }
105  }
106  return output;
107 }
109  float min, max;
110  min_max(input, &min, &max);
111  return image_float2uchar(input, min, max);
112 }
114  int width = input->width();
115  int height = input->height();
116  Image<uint32_t> *output = new Image<uint32_t>(width, height, false);
117  for (int y = 0; y < height; y++) {
118  for (int x = 0; x < width; x++) {
119  imRef(output, x, y) = imRef(input, x, y);
120  }
121  }
122  return output;
123 }
125  uint32_t 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 = UCHAR_MAX / static_cast<float>(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)UCHAR_MAX);
137  }
138  }
139  return output;
140 }
142  uint32_t min, max;
143  min_max(input, &min, &max);
144  return image_long2uchar(input, min, max);
145 }
147  uint16_t max) {
148  int width = input->width();
149  int height = input->height();
150  Image<uchar> *output = new Image<uchar>(width, height, false);
151  if (max == min) {
152  return output;
153  }
154  float scale = UCHAR_MAX / static_cast<float>(max - min);
155  for (int y = 0; y < height; y++) {
156  for (int x = 0; x < width; x++) {
157  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
158  imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
159  }
160  }
161  return output;
162 }
164  uint16_t min, max;
165  min_max(input, &min, &max);
166  return image_short2uchar(input, min, max);
167 }
168 } // namespace lidar
169 } // namespace perception
170 } // 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:124
Definition: blob.h:72
Image< uchar > * image_rgb2gray(Image< rgb > *input)
Definition: imconv.h:44
int width() const
Definition: image.h:52
#define imRef(im, x, y)
Definition: image.h:68
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:146
Image< float > * image_uchar2float(Image< uchar > *input)
Definition: imconv.h:70
Image< uchar > * image_float2uchar(Image< float > *input, float min, float max)
Definition: imconv.h:92
Image< float > * image_int2float(Image< int > *input)
Definition: imconv.h:81
const double BLUE_WEIGHT
Definition: imconv.h:43
const double GREEN_WEIGHT
Definition: imconv.h:42
Image< rgb > * image_gray2rgb(Image< uchar > *input)
Definition: imconv.h:57
unsigned char uchar
Definition: misc.h:38
Image< uint32_t > * image_uchar2long(Image< uchar > *input)
Definition: imconv.h:113
const double RED_WEIGHT
Definition: imconv.h:41