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