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/core/DataTypes.h>
28 #include <VisionX/interface/components/Calibration.h>
29 
33 
34 class CByteImage;
35 class CFloatImage;
36 
37 namespace visionx
38 {
39  struct ImageFormatInfo;
40  class ImageProviderInfo;
41 }
42 namespace visionx::tools
43 {
44  /**
45  * This converts the input image data into a desired destination image data type specified
46  * in the given ImageProviderInfo.
47  *
48  * If source image type equals destination image type, the image is simply copied, otherwise
49  * the image is converted if the conversion type is supported.
50  *
51  * Supported conversions:
52  * - BayerPattern-{bg,gb,gr,rg} to BayerPattern-{bg,gb,gr,rg} (copy only)
53  * - BayerPattern-{bg,gb,gr,rg} to GrayScale
54  * - BayerPattern-{bg,gb,gr,rg} to RGB
55  * - GrayScale to GrayScale (copy only)
56  * - GrayScale to RGB
57  * - RGB to GrayScale
58  * - RGB to RGB (copy only)
59  * - Float single channel to Float single channel (copy only)
60  * - Float multiple channel to Float multiple channel (copy only)
61  *
62  * @param ImageFormatInfo Image format information which contains the source image type,
63  * source image size and more details needed for conversions.
64  * @param destinationType destination image type of outputData
65  * @param inputData input data array
66  * @param outputData output data array
67  *
68  * @throws visionx::UnsupportedImageConversion if conversion from a specific source type
69  * into a destination type is not supported or implemented.
70  */
71  void convertImage(const ImageFormatInfo& imageFormat, const ImageType destinationImageType, void* inputData, void* outputData);
72 
73  /**
74  * Simplifies usage of visionx::tools:convertImage in ImageProcessors.
75  *
76  * @see visionx::tools::convertImage(const ImageFormatInfo&, const ImageType, unsigned char*, unsigned char*)
77  *
78  * @param ImageProviderInfo Registered image provider information which contains the source image type,
79  * destination image type, source image size and more details needed for
80  * conversions.
81  * @param inputData input data array
82  * @param outputData output data array
83  *
84  * @throws visionx::UnsupportedImageConversion if conversion from a specific source type
85  * into a destination type is not supported or implemented.
86  */
87  void convertImage(const ImageProviderInfo& imageProviderInfo, void* inputData, void* outputData);
88 
89  /**
90  * Creates a ByteImage for the destination type specified in the given imageProviderInfo.
91  *
92  * @param ImageFormatInfo Image format information which contains the source image size
93  * @param imageType Required VisionX image type which will be mapped onto
94  * an appropriate CByteImage::ImageType
95  *
96  * @return Appropriate CByteImage
97  */
98  CByteImage* createByteImage(const ImageFormatInfo& imageFormat, const ImageType imageType);
99  CByteImage* createByteImage(const ImageFormatInfo& imageFormat);
100 
101  /**
102  * Simplifies usage of visionx::tools:convertImage in ImageProcessors.
103  *
104  * @see visionx::tools::createImage(const ImageFormatInfo&, const ImageType)
105  *
106  * @param imageProviderInfo Registered image provider information which contains the
107  * ImageFormatInfo and destination image type.
108  *
109  * @return CByteImage Appropriate CByteImage
110  */
111  CByteImage* createByteImage(const ImageProviderInfo& imageProviderInfo);
112 
113  /**
114  * Creates a FloatImage for the destination type specified in the given imageProviderInfo.
115  *
116  * @param ImageFormatInfo Image format information which contains the source image size
117  * @param imageType Required VisionX image type which will be mapped onto
118  * an appropriate number of channels
119  *
120  * @return CByteImage Appropriate CFloatImage
121  */
122  CFloatImage* createFloatImage(const ImageFormatInfo& imageFormat, const ImageType imageType);
123 
124  /**
125  * Simplifies usage of visionx::tools:convertImage in ImageProcessors.
126  *
127  * @see visionx::tools::createFloatImage(const ImageFormatInfo&, const ImageType)
128  *
129  * @param imageProviderInfo Registered image provider information which contains the
130  * ImageFormatInfo and destination image type.
131  *
132  * @return CByteImage Appropriate CFloatImage
133  */
134  CFloatImage* createFloatImage(const ImageProviderInfo& imageProviderInfo);
135 
136  /**
137  * Creates a MonocularCalibration with all parameters set to a neutral value.
138  *
139  */
140  MonocularCalibration createDefaultMonocularCalibration();
141 
142  inline float rgbToDepthValue(unsigned char r, unsigned char g, unsigned char b, bool noiseResistant = false)
143  {
144  float result = 0;
145  if (noiseResistant)
146  {
147  constexpr char size = 8;
148  constexpr int channels = 3;
149 
150  int sum = 0;
151  auto add = [&](unsigned char v, char channelNumber)
152  {
153  auto end = size * channels;
154  for (int offset = 0; offset < end; offset += channels)
155  {
156  auto tmp = (v & 0b1) << (channelNumber + offset);
157  sum += tmp;
158  v = (v >> 1);
159  }
160  };
161  add(r, 0);
162  add(g, 1);
163  add(b, 2);
164 
165 
166  result = sum;
167  result /= 256; // stretch back from 24 bit to 16 bit - see depthValueToRGB()
168  }
169  else
170  {
171  result = (r + (g << 8) + (b << 16));
172  }
173  return result;
174  }
175 
176  inline void depthValueToRGB(unsigned int depthInMM, unsigned char& r, unsigned char& g, unsigned char& b, bool noiseResistant = false)
177  {
178  if (noiseResistant)
179  {
180  depthInMM *= 256; // stretch from 16 bit to 24 bit
181  constexpr char size = 8;
182  constexpr int channels = 3;
183  auto toRGB = [&](unsigned int v, char channelNumber)
184  {
185  int mask = 0b1;
186  v = v >> channelNumber;
187  unsigned char r = 0;
188  for (int offset = 0; offset < size; offset++)
189  {
190  int shift = std::max(offset * channels - offset, 0);
191  int masked = v & mask;
192  int shiftedV = masked >> shift;
193  r += shiftedV;
194  mask = mask << channels;
195  }
196  return r;
197  };
198 
199  r = toRGB(depthInMM, 0);
200  g = toRGB(depthInMM, 1);
201  b = toRGB(depthInMM, 2);
202  }
203  else
204  {
205  r = depthInMM & 0xFF;
206  g = (depthInMM >> 8) & 0xFF;
207  b = (depthInMM >> 16) & 0xFF;
208  }
209  }
210 
211 
212 }
213 
214 
215 namespace std
216 {
217  inline ostream& operator<<(ostream& os, const visionx::ImageDimension& dimension)
218  {
219  os << dimension.width << "x" << dimension.height;
220  return os;
221  }
222 }
223 
InvalidByteImageTypeException.h
visionx::tools
Definition: PCLUtilities.cpp:4
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::tools::createFloatImage
CFloatImage * createFloatImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a FloatImage for the destination type specified in the given imageProviderInfo.
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
UnsupportedImageConversionException.h
visionx::tools::depthValueToRGB
void depthValueToRGB(unsigned int depthInMM, unsigned char &r, unsigned char &g, unsigned char &b, bool noiseResistant=false)
Definition: ImageUtil.h:176
max
T max(T t1, T t2)
Definition: gdiam.h:48
visionx::tools::convertImage
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 ...
std::operator<<
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
std
Definition: Application.h:66
visionx::tools::rgbToDepthValue
float rgbToDepthValue(unsigned char r, unsigned char g, unsigned char b, bool noiseResistant=false)
Definition: ImageUtil.h:142
visionx::tools::createDefaultMonocularCalibration
MonocularCalibration createDefaultMonocularCalibration()
Creates a MonocularCalibration with all parameters set to a neutral value.
Definition: ImageUtil.cpp:237
InvalidFloatImageTypeException.h