ImageUtil.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package VisionX::Tools
19 * @author Jan Issac (jan dot issac at gmx dot net)
20 * @date 2011
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#pragma once
26
27#include <VisionX/interface/components/Calibration.h>
28#include <VisionX/interface/core/DataTypes.h>
29
33
34class CByteImage;
35class CFloatImage;
36
37namespace visionx
38{
39 struct ImageFormatInfo;
41} // namespace visionx
42
43namespace visionx::tools
44{
45 /**
46 * This converts the input image data into a desired destination image data type specified
47 * in the given ImageProviderInfo.
48 *
49 * If source image type equals destination image type, the image is simply copied, otherwise
50 * the image is converted if the conversion type is supported.
51 *
52 * Supported conversions:
53 * - BayerPattern-{bg,gb,gr,rg} to BayerPattern-{bg,gb,gr,rg} (copy only)
54 * - BayerPattern-{bg,gb,gr,rg} to GrayScale
55 * - BayerPattern-{bg,gb,gr,rg} to RGB
56 * - GrayScale to GrayScale (copy only)
57 * - GrayScale to RGB
58 * - RGB to GrayScale
59 * - RGB to RGB (copy only)
60 * - Float single channel to Float single channel (copy only)
61 * - Float multiple channel to Float multiple channel (copy only)
62 *
63 * @param ImageFormatInfo Image format information which contains the source image type,
64 * source image size and more details needed for conversions.
65 * @param destinationType destination image type of outputData
66 * @param inputData input data array
67 * @param outputData output data array
68 *
69 * @throws visionx::UnsupportedImageConversion if conversion from a specific source type
70 * into a destination type is not supported or implemented.
71 */
72 void convertImage(const ImageFormatInfo& imageFormat,
73 const ImageType destinationImageType,
74 void* inputData,
75 void* outputData);
76
77 /**
78 * Simplifies usage of visionx::tools:convertImage in ImageProcessors.
79 *
80 * @see visionx::tools::convertImage(const ImageFormatInfo&, const ImageType, unsigned char*, unsigned char*)
81 *
82 * @param ImageProviderInfo Registered image provider information which contains the source image type,
83 * destination image type, source image size and more details needed for
84 * conversions.
85 * @param inputData input data array
86 * @param outputData output data array
87 *
88 * @throws visionx::UnsupportedImageConversion if conversion from a specific source type
89 * into a destination type is not supported or implemented.
90 */
91 void
92 convertImage(const ImageProviderInfo& imageProviderInfo, void* inputData, void* outputData);
93
94 /**
95 * Creates a ByteImage for the destination type specified in the given imageProviderInfo.
96 *
97 * @param ImageFormatInfo Image format information which contains the source image size
98 * @param imageType Required VisionX image type which will be mapped onto
99 * an appropriate CByteImage::ImageType
100 *
101 * @return Appropriate CByteImage
102 */
103 CByteImage* createByteImage(const ImageFormatInfo& imageFormat, const ImageType imageType);
104 CByteImage* createByteImage(const ImageFormatInfo& imageFormat);
105
106 /**
107 * Simplifies usage of visionx::tools:convertImage in ImageProcessors.
108 *
109 * @see visionx::tools::createImage(const ImageFormatInfo&, const ImageType)
110 *
111 * @param imageProviderInfo Registered image provider information which contains the
112 * ImageFormatInfo and destination image type.
113 *
114 * @return CByteImage Appropriate CByteImage
115 */
116 CByteImage* createByteImage(const ImageProviderInfo& imageProviderInfo);
117
118 /**
119 * Creates a FloatImage for the destination type specified in the given imageProviderInfo.
120 *
121 * @param ImageFormatInfo Image format information which contains the source image size
122 * @param imageType Required VisionX image type which will be mapped onto
123 * an appropriate number of channels
124 *
125 * @return CByteImage Appropriate CFloatImage
126 */
127 CFloatImage* createFloatImage(const ImageFormatInfo& imageFormat, const ImageType imageType);
128
129 /**
130 * Simplifies usage of visionx::tools:convertImage in ImageProcessors.
131 *
132 * @see visionx::tools::createFloatImage(const ImageFormatInfo&, const ImageType)
133 *
134 * @param imageProviderInfo Registered image provider information which contains the
135 * ImageFormatInfo and destination image type.
136 *
137 * @return CByteImage Appropriate CFloatImage
138 */
139 CFloatImage* createFloatImage(const ImageProviderInfo& imageProviderInfo);
140
141 /**
142 * Creates a MonocularCalibration with all parameters set to a neutral value.
143 *
144 */
145 MonocularCalibration createDefaultMonocularCalibration();
146
147 inline float
148 rgbToDepthValue(unsigned char r, unsigned char g, unsigned char b, bool noiseResistant = false)
149 {
150 float result = 0;
151 if (noiseResistant)
152 {
153 constexpr char size = 8;
154 constexpr int channels = 3;
155
156 int sum = 0;
157 auto add = [&](unsigned char v, char channelNumber)
158 {
159 auto end = size * channels;
160 for (int offset = 0; offset < end; offset += channels)
161 {
162 auto tmp = (v & 0b1) << (channelNumber + offset);
163 sum += tmp;
164 v = (v >> 1);
165 }
166 };
167 add(r, 0);
168 add(g, 1);
169 add(b, 2);
170
171
172 result = sum;
173 result /= 256; // stretch back from 24 bit to 16 bit - see depthValueToRGB()
174 }
175 else
176 {
177 result = (r + (g << 8) + (b << 16));
178 }
179 return result;
180 }
181
182 inline void
183 depthValueToRGB(unsigned int depthInMM,
184 unsigned char& r,
185 unsigned char& g,
186 unsigned char& b,
187 bool noiseResistant = false)
188 {
189 if (noiseResistant)
190 {
191 depthInMM *= 256; // stretch from 16 bit to 24 bit
192 constexpr char size = 8;
193 constexpr int channels = 3;
194 auto toRGB = [&](unsigned int v, char channelNumber)
195 {
196 int mask = 0b1;
197 v = v >> channelNumber;
198 unsigned char r = 0;
199 for (int offset = 0; offset < size; offset++)
200 {
201 int shift = std::max(offset * channels - offset, 0);
202 int masked = v & mask;
203 int shiftedV = masked >> shift;
204 r += shiftedV;
205 mask = mask << channels;
206 }
207 return r;
208 };
209
210 r = toRGB(depthInMM, 0);
211 g = toRGB(depthInMM, 1);
212 b = toRGB(depthInMM, 2);
213 }
214 else
215 {
216 r = depthInMM & 0xFF;
217 g = (depthInMM >> 8) & 0xFF;
218 b = (depthInMM >> 16) & 0xFF;
219 }
220 }
221
222
223} // namespace visionx::tools
224
225namespace std
226{
227 inline ostream&
228 operator<<(ostream& os, const visionx::ImageDimension& dimension)
229 {
230 os << dimension.width << "x" << dimension.height;
231 return os;
232 }
233} // namespace std
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
void depthValueToRGB(unsigned int depthInMM, unsigned char &r, unsigned char &g, unsigned char &b, bool noiseResistant=false)
Definition ImageUtil.h:183
void convertImage(const ImageFormatInfo &imageFormat, const ImageType destinationImageType, void *inputData, void *outputData)
This converts the input image data into a desired destination image data type specified in the given ...
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
CFloatImage * createFloatImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a FloatImage for the destination type specified in the given imageProviderInfo.
MonocularCalibration createDefaultMonocularCalibration()
Creates a MonocularCalibration with all parameters set to a neutral value.
float rgbToDepthValue(unsigned char r, unsigned char g, unsigned char b, bool noiseResistant=false)
Definition ImageUtil.h:148
ArmarX headers.