ImageUtil.cpp
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 #include "ImageUtil.h"
26 
27 #include <map>
28 #include <set>
29 #include <string>
30 
31 #include "TypeMapping.h"
32 
33 // VisionXCore
35 
36 // IVT
37 #include <Image/ByteImage.h>
38 #include <Image/FloatImage.h>
39 #include <Image/ImageProcessor.h>
40 
41 void
42 visionx::tools::convertImage(const visionx::ImageFormatInfo& imageFormat,
43  const visionx::ImageType destinationImageType,
44  void* inputData,
45  void* outputData)
46 {
47  bool succeeded = true;
48 
49  visionx::ImageDimension dimension = imageFormat.dimension;
50  visionx::ImageType sourceType = imageFormat.type;
51  visionx::ImageType destinationType = destinationImageType;
52 
53  /* copy images if types are identical */
54  if (sourceType == destinationType)
55  {
56  memcpy(outputData,
57  inputData,
58  imageFormat.bytesPerPixel * imageFormat.dimension.width *
59  imageFormat.dimension.height);
60  return;
61  }
62 
63  /* process conversion */
64  CByteImage sourceImage(
65  dimension.width, dimension.height, visionx::tools::convert(sourceType), true);
66  sourceImage.pixels = (unsigned char*)inputData;
67 
68  CByteImage destinationImage(
69  dimension.width, dimension.height, visionx::tools::convert(destinationType), true);
70  destinationImage.pixels = (unsigned char*)outputData;
71 
72  switch (sourceType)
73  {
74  // BayerPattern to {RGB, Gray-Scale}
75  case eBayerPattern:
76  {
77  switch (destinationType)
78  {
79  case eGrayScale:
80  {
81  CByteImage tempRgbImage(dimension.width, dimension.height, CByteImage::eRGB24);
82  ::ImageProcessor::ConvertBayerPattern(
83  &sourceImage, &tempRgbImage, tools::convert(imageFormat.bpType));
84  ::ImageProcessor::ConvertImage(&tempRgbImage, &destinationImage);
85  break;
86  }
87 
88  case eRgb:
89  {
90  ::ImageProcessor::ConvertBayerPattern(
91  &sourceImage, &destinationImage, tools::convert(imageFormat.bpType));
92  break;
93  }
94 
95  //case eBayerPattern:
96  //case eFloat1Channel:
97  //case eFloat3Channels:
98  default:
99  {
100  /* Invalid conversion. This will cause an exception */
101  succeeded = false;
102  break;
103  }
104  };
105 
106  break;
107  }
108 
109  // RGB to {Gray-Scale}
110  case eRgb:
111  {
112  switch (destinationType)
113  {
114  case eGrayScale:
115  {
116  ::ImageProcessor::ConvertImage(&sourceImage, &destinationImage);
117  return;
118  }
119 
120  //case eBayerPattern:
121  //case eRgb:
122  //case eFloat1Channel:
123  //case eFloat3Channels:
124  default:
125  {
126  /* Invalid conversion. This will cause an exception */
127  succeeded = false;
128  break;
129  }
130  };
131  }
132  break;
133 
134  // Gray-Scale to {RGB}
135  case eGrayScale:
136  {
137  switch (destinationType)
138  {
139  case eRgb:
140  {
141  ::ImageProcessor::ConvertImage(&sourceImage, &destinationImage);
142  return;
143  }
144 
145  //case eBayerPattern:
146  //case eGrayScale:
147  // case eFloat1Channel:
148  // case eFloat3Channels:
149  default:
150  {
151  /* Invalid conversion. This will cause an exception */
152  succeeded = false;
153  break;
154  }
155  };
156  }
157  break;
158 
159  // HDR BayerPattern to {}
160  // case eFloat1Channel:
161  // HDR RGB to {}
162  //case eFloat3Channels:
163  default:
164  {
165  /* Invalid conversion. This will cause an exception */
166  succeeded = false;
167  break;
168  }
169  }
170 
171  if (!succeeded)
172  {
175  visionx::tools::imageTypeToTypeName(destinationType));
176  }
177 }
178 
179 void
181  void* inputData,
182  void* outputData)
183 {
184  visionx::tools::convertImage(imageProviderInfo.imageFormat,
185  imageProviderInfo.destinationImageType,
186  inputData,
187  outputData);
188 }
189 
190 CByteImage*
191 visionx::tools::createByteImage(const visionx::ImageFormatInfo& imageFormat,
192  const visionx::ImageType imageType)
193 {
194  CByteImage::ImageType type = CByteImage::eGrayScale;
195 
196  switch (imageType)
197  {
198  case eBayerPattern:
199  case eGrayScale:
200  break;
201 
202  case eRgb:
203  type = CByteImage::eRGB24;
204  break;
205 
206  //case eFloat1Channel:
207  //case eFloat3Channels:
208  default:
211  }
212 
213  return new CByteImage(imageFormat.dimension.width, imageFormat.dimension.height, type);
214 }
215 
216 CByteImage*
217 visionx::tools::createByteImage(const ImageFormatInfo& imageFormat)
218 {
219  return createByteImage(imageFormat, imageFormat.type);
220 }
221 
222 CByteImage*
224 {
225  return visionx::tools::createByteImage(imageProviderInfo.imageFormat,
226  imageProviderInfo.destinationImageType);
227 }
228 
229 CFloatImage*
230 visionx::tools::createFloatImage(const visionx::ImageFormatInfo& imageFormat,
231  const visionx::ImageType imageType)
232 {
233  int numberOfChannels = 1;
234 
235  switch (imageType)
236  {
237  case eFloat1Channel:
238  // already set to 1
239  break;
240 
241  case eFloat3Channels:
242  numberOfChannels = 3;
243  break;
244 
245  //case eBayerPattern:
246  //case eGrayScale:
247  //case eRgb:
248  default:
251  }
252 
253  return new CFloatImage(
254  imageFormat.dimension.width, imageFormat.dimension.height, numberOfChannels);
255 }
256 
257 CFloatImage*
259 {
260  return visionx::tools::createFloatImage(imageProviderInfo.imageFormat,
261  imageProviderInfo.destinationImageType);
262 }
263 
264 visionx::MonocularCalibration
266 {
267  visionx::MonocularCalibration calibration;
268  calibration.cameraParam.width = 640;
269  calibration.cameraParam.height = 480;
270  calibration.cameraParam.focalLength.resize(2);
271  calibration.cameraParam.focalLength[0] = 500;
272  calibration.cameraParam.focalLength[1] = 500;
273  calibration.cameraParam.principalPoint.resize(2);
274  calibration.cameraParam.principalPoint[0] = calibration.cameraParam.width / 2;
275  calibration.cameraParam.principalPoint[1] = calibration.cameraParam.height / 2;
276  std::vector<std::vector<float>> mat(3);
277  std::vector<float> rowVec(3);
278  rowVec[0] = 0;
279  rowVec[1] = 0;
280  rowVec[2] = 0;
281  calibration.cameraParam.translation = rowVec;
282  mat[0] = rowVec;
283  mat[1] = rowVec;
284  mat[2] = rowVec;
285  mat[0][0] = 1;
286  mat[1][1] = 1;
287  mat[2][2] = 1;
288  calibration.cameraParam.rotation = mat;
289  rowVec.push_back(0);
290  calibration.cameraParam.distortion = rowVec;
291 
292  return calibration;
293 }
visionx::exceptions::local::InvalidByteImageTypeException
Definition: InvalidByteImageTypeException.h:31
visionx::exceptions::local::UnsupportedImageConversionException
Definition: UnsupportedImageConversionException.h:31
visionx::tools::createFloatImage
CFloatImage * createFloatImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a FloatImage for the destination type specified in the given imageProviderInfo.
visionx::ImageProviderInfo::imageFormat
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
Definition: ImageProcessor.h:509
convert
void convert(const std::filesystem::path &in, const std::filesystem::path &out, bool print_progress)
Performs the actual conversion.
Definition: main.cpp:170
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::ImageProviderInfo
Definition: ImageProcessor.h:479
visionx::tools::convert
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.
Definition: TypeMapping.cpp:97
ImageProcessor.h
visionx::exceptions::local::InvalidFloatImageTypeException
Definition: InvalidFloatImageTypeException.h:31
visionx::tools::imageTypeToTypeName
std::string imageTypeToTypeName(const ImageType imageType)
Converts an image type into a string integer.
Definition: TypeMapping.cpp:72
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 ...
visionx::ImageProviderInfo::destinationImageType
ImageType destinationImageType
Required image destination type.
Definition: ImageProcessor.h:504
ImageUtil.h
TypeMapping.h
visionx::tools::createDefaultMonocularCalibration
MonocularCalibration createDefaultMonocularCalibration()
Creates a MonocularCalibration with all parameters set to a neutral value.
Definition: ImageUtil.cpp:265