CVB++ 15.0
Loading...
Searching...
No Matches
convert_color_space.hpp
1#pragma once
2
3#if defined _WIN32
4
5# include "../_cexports/c_foundation.h"
6
7# include "../global.hpp"
8# include "../image.hpp"
9# include "../exception.hpp"
10
11# include <vector>
12# include <memory>
13
14namespace Cvb
15{
16 CVB_BEGIN_INLINE_NS
17
18 namespace Foundation
19 {
20
22
27 {
28
30
41 inline std::unique_ptr<Image> ConvertToRGB(const Image &image, ColorModel guessToColorModel = ColorModel::RGB)
42 {
43 auto colorModel = image.ColorModel();
44 if (colorModel == ColorModel::RGBGuess)
45 {
46 colorModel = guessToColorModel;
47 }
48
49 switch (colorModel)
50 {
52 default:
53 /* this case cannot be handled at all */
54 throw std::invalid_argument("unknown color model, cannot convert");
55
56 case ColorModel::RGB:
58 /* in this case nothing needs to be done */
59 return image.Clone();
60
63 return Image::FromImages(MappingOption::CopyPixels, image, image, image);
64
65 /* following below are the "normal" cases (resolved through C-calls) */
67 return Internal::DoResCallObjectOut<Image>(
68 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertLabToRGB(image.Handle(), resimg)); });
69
71 return Internal::DoResCallObjectOut<Image>(
72 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertLUVToRGB(image.Handle(), resimg)); });
73
75 return Internal::DoResCallObjectOut<Image>(
76 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertXYZToRGB(image.Handle(), resimg)); });
77
78 case ColorModel::HLS:
79 return Internal::DoResCallObjectOut<Image>(
80 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertHLSToRGB(image.Handle(), resimg)); });
81
82 case ColorModel::HSI:
83 case ColorModel::HSV:
84 return Internal::DoResCallObjectOut<Image>(
85 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertHSVToRGB(image.Handle(), resimg)); });
86
88 return Internal::DoResCallObjectOut<Image>(
89 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertYCbCrToRGB(image.Handle(), resimg)); });
90
91 case ColorModel::YCC:
92 return Internal::DoResCallObjectOut<Image>(
93 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertYCCToRGB(image.Handle(), resimg)); });
94
95 case ColorModel::YUV:
96 return Internal::DoResCallObjectOut<Image>(
97 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertYUVToRGB(image.Handle(), resimg)); });
98 }
99 }
100
102
112 {
113 auto colorModel = image.ColorModel();
114 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
115 {
116 return Internal::DoResCallObjectOut<Image>(
117 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToYUV(image.Handle(), resimg)); });
118 }
119 else
120 {
121 return ConvertToYUV(*ConvertToRGB(image));
122 }
123 }
124
126
136 {
137 auto colorModel = image.ColorModel();
138 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
139 {
140 return Internal::DoResCallObjectOut<Image>(
141 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToYCbCr(image.Handle(), resimg)); });
142 }
143 else
144 {
145 return ConvertToYCbCr(*ConvertToRGB(image));
146 }
147 }
148
150
160 {
161 auto colorModel = image.ColorModel();
162 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
163 {
164 return Internal::DoResCallObjectOut<Image>(
165 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToXYZ(image.Handle(), resimg)); });
166 }
167 else
168 {
169 return ConvertToCieXYZ(*ConvertToRGB(image));
170 }
171 }
172
174
184 {
185 auto colorModel = image.ColorModel();
186 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
187 {
188 return Internal::DoResCallObjectOut<Image>(
189 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToLUV(image.Handle(), resimg)); });
190 }
191 else
192 {
193 return ConvertToCieLUV(*ConvertToRGB(image));
194 }
195 }
196
198
208 {
209 auto colorModel = image.ColorModel();
210 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
211 {
212 return Internal::DoResCallObjectOut<Image>(
213 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToLab(image.Handle(), true, resimg)); });
214 }
215 else
216 {
217 return ConvertToCieLab16(*ConvertToRGB(image));
218 }
219 }
220
222
232 {
233 auto colorModel = image.ColorModel();
234 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
235 {
236 return Internal::DoResCallObjectOut<Image>(
237 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToLab(image.Handle(), false, resimg)); });
238 }
239 else
240 {
241 return ConvertToCieLab8(*ConvertToRGB(image));
242 }
243 }
244
246
256 {
257 auto colorModel = image.ColorModel();
258 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
259 {
260 return Internal::DoResCallObjectOut<Image>(
261 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToYCC(image.Handle(), resimg)); });
262 }
263 else
264 {
265 return ConvertToYCC(*ConvertToRGB(image));
266 }
267 }
268
270
280 {
281 auto colorModel = image.ColorModel();
282 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
283 {
284 return Internal::DoResCallObjectOut<Image>(
285 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToHLS(image.Handle(), resimg)); });
286 }
287 else
288 {
289 return ConvertToHLS(*ConvertToRGB(image));
290 }
291 }
292
294
304 {
305 auto colorModel = image.ColorModel();
306 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
307 {
308 return Internal::DoResCallObjectOut<Image>(
309 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToHSV(image.Handle(), resimg)); });
310 }
311 else
312 {
313 return ConvertToHSV(*ConvertToRGB(image));
314 }
315 }
316
318
328 {
329 auto colorModel = image.ColorModel();
330 if (colorModel == ColorModel::Mono || colorModel == ColorModel::MonoGuess)
331 {
332 return image.Clone();
333 }
334 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
335 {
336 return Internal::DoResCallObjectOut<Image>(
337 [&](void *&resimg) { return CVB_CALL_CAPI(ConvertRGBToGrayScaleStandard(image.Handle(), resimg)); });
338 }
339 else
340 {
341 return ConvertToMono(*ConvertToRGB(image));
342 }
343 }
344
346
360 inline std::unique_ptr<Image> ConvertToMono(const Image &image, double weightR, double weightG, double weightB)
361 {
362 auto colorModel = image.ColorModel();
363 if (colorModel == ColorModel::Mono || colorModel == ColorModel::MonoGuess)
364 {
365 return image.Clone();
366 }
367 if (colorModel == ColorModel::RGB || colorModel == ColorModel::RGBGuess)
368 {
369 return Internal::DoResCallObjectOut<Image>([&](void *&resimg) {
370 return CVB_CALL_CAPI(ConvertRGBToGrayScale(image.Handle(), weightR, weightG, weightB, resimg));
371 });
372 }
373 else
374 {
375 return ConvertToMono(*ConvertToRGB(image), weightR, weightG, weightB);
376 }
377 }
378
380
388 template <class RANGE>
389 inline typename TypedRange<std::unique_ptr<Image>, int, RANGE>::type SwapChannels(const Image &image,
390 const RANGE &newArrangement)
391 {
392 if (3 != std::distance(std::begin(newArrangement), std::end(newArrangement)))
393 {
394 throw std::invalid_argument("the specification of new channels arrangement must have exactly three entries");
395 }
396 std::vector<CExports::cvbdim_t> newArrDimT(std::begin(newArrangement),
397 std::end(newArrangement)); /* (type conversion) */
398 return Internal::DoResCallObjectOut<Image>(
399 [&](void *&resimg) { return CVB_CALL_CAPI(SwapRGBChannels(image.Handle(), newArrDimT.data(), resimg)); });
400 }
401
403
406 {
407
409 double A11;
410
412 double A12;
413
415 double A13;
416
418 double A14;
419
421 double A21;
422
424 double A22;
425
427 double A23;
428
430 double A24;
431
433 double A31;
434
436 double A32;
437
439 double A33;
440
442 double A34;
443 }; /* struct ColorTwistMatrix */
444
446
454 {
455 return Internal::DoResCallObjectOut<Image>([&](void *&resimg) {
456 return CVB_CALL_CAPI(ApplyColorTwistMatrix(image.Handle(), matrix.A11, matrix.A12, matrix.A13, matrix.A14,
457 matrix.A21, matrix.A22, matrix.A23, matrix.A24, matrix.A31,
458 matrix.A32, matrix.A33, matrix.A34, resimg));
459 });
460 }
461
462 } /* namespace ConvertColorSpace */
463
476
477 using ConvertColorSpace::ColorTwistMatrix;
479
480 } /* namespace Foundation */
481 CVB_END_INLINE_NS
482} /* namespace Cvb */
483
484#endif
T begin(T... args)
The Common Vision Blox image.
Definition decl_image.hpp:45
std::unique_ptr< Image > Clone() const
Creates a new image object, that is a copy of the current instance.
Definition detail_image.hpp:116
static TypedRange< std::unique_ptr< Image >, ImagePtr, RANGE >::type FromImages(MappingOption mapping, const RANGE &images)
Create an image that is the result of concatenating a series of input images.
Definition decl_image.hpp:96
Cvb::ColorModel ColorModel() const noexcept
Color model realized by this image.
Definition decl_image.hpp:314
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
T distance(T... args)
T end(T... args)
cvbres_t ConvertRGBToLab(IMG ImgIn, cvbbool_t UseLab16, IMG &ImgOut)
cvbres_t ConvertHLSToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertYUVToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToLUV(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToHSV(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertLabToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToYUV(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertYCbCrToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToGrayScale(IMG ImgIn, double CoeffRed, double CoeffGreen, double CoeffBlue, IMG &ImgOut)
cvbres_t ApplyColorTwistMatrix(IMG ImgIn, double A11, double A12, double A13, double A14, double A21, double A22, double A23, double A24, double A31, double A32, double A33, double A34, IMG &ImgOut)
cvbres_t ConvertRGBToGrayScaleStandard(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertXYZToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToHLS(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToYCbCr(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToYCC(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertRGBToXYZ(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertLUVToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertYCCToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t ConvertHSVToRGB(IMG ImgIn, IMG &ImgOut)
cvbres_t SwapRGBChannels(IMG ImgIn, long Order[3], IMG &ImgOut)
Namespace for collection of color space conversion functions from the Foundation package.
Definition convert_color_space.hpp:27
std::unique_ptr< Image > ConvertToCieLab8(const Image &image)
Convert the input image to CIE Lab with DataType::Int8BppUnsigned().
Definition convert_color_space.hpp:231
std::unique_ptr< Image > ConvertToMono(const Image &image)
Convert the input image to mono.
Definition convert_color_space.hpp:327
TypedRange< std::unique_ptr< Image >, int, RANGE >::type SwapChannels(const Image &image, const RANGE &newArrangement)
Create a new image by rearranging the planes of the input image.
Definition convert_color_space.hpp:389
std::unique_ptr< Image > ConvertToCieXYZ(const Image &image)
Convert the input image to CIE XYZ.
Definition convert_color_space.hpp:159
std::unique_ptr< Image > ConvertToYUV(const Image &image)
Convert the input image to YUV.
Definition convert_color_space.hpp:111
std::unique_ptr< Image > ConvertToCieLab16(const Image &image)
Convert the input image to CIE Lab with DataType::Int16BppUnsigned() for better precision.
Definition convert_color_space.hpp:207
std::unique_ptr< Image > ConvertToYCC(const Image &image)
Convert the input image to YCC.
Definition convert_color_space.hpp:255
std::unique_ptr< Image > ConvertToYCbCr(const Image &image)
Convert the input image to YCbCr.
Definition convert_color_space.hpp:135
std::unique_ptr< Image > ConvertToRGB(const Image &image, ColorModel guessToColorModel=ColorModel::RGB)
Convert the input image to RGB.
Definition convert_color_space.hpp:41
std::unique_ptr< Image > TwistColors(const Image &image, ColorTwistMatrix matrix)
Apply the color twist matrix to the input image.
Definition convert_color_space.hpp:453
std::unique_ptr< Image > ConvertToHLS(const Image &image)
Convert the input image to HLS.
Definition convert_color_space.hpp:279
std::unique_ptr< Image > ConvertToHSV(const Image &image)
Convert the input image to HSV.
Definition convert_color_space.hpp:303
std::unique_ptr< Image > ConvertToCieLUV(const Image &image)
Convert the input image to CIE LUV.
Definition convert_color_space.hpp:183
Namespace for the Foundation package.
Definition decl_metric_aqs12_calibration_piece.hpp:11
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
@ CopyPixels
Definition global.hpp:365
ColorModel
Color model that this image is using.
Definition global.hpp:176
@ CieLUV
Definition global.hpp:216
@ HLS
Definition global.hpp:224
@ CieLab
Definition global.hpp:220
@ MonoGuess
Definition global.hpp:184
@ HSI
Definition global.hpp:208
@ Mono
Definition global.hpp:192
@ HSV
Definition global.hpp:232
@ CieXYZ
Definition global.hpp:236
@ Unknown
Definition global.hpp:188
@ RGB
Definition global.hpp:200
@ YUV
Definition global.hpp:204
@ YCC
Definition global.hpp:228
@ YCbCr
Definition global.hpp:212
@ RGBGuess
Definition global.hpp:180
Matrix defining color transformations.
Definition convert_color_space.hpp:406
double A14
Coefficient 1,4 of the matrix.
Definition convert_color_space.hpp:418
double A21
Coefficient 2,1 of the matrix.
Definition convert_color_space.hpp:421
double A11
Coefficient 1,1 of the matrix.
Definition convert_color_space.hpp:409
double A12
Coefficient 1,2 of the matrix.
Definition convert_color_space.hpp:412
double A33
Coefficient 3,3 of the matrix.
Definition convert_color_space.hpp:439
double A13
Coefficient 1,3 of the matrix.
Definition convert_color_space.hpp:415
double A32
Coefficient 3,2 of the matrix.
Definition convert_color_space.hpp:436
double A34
Coefficient 3,4 of the matrix.
Definition convert_color_space.hpp:442
double A23
Coefficient 2,3 of the matrix.
Definition convert_color_space.hpp:427
double A31
Coefficient 3,1 of the matrix.
Definition convert_color_space.hpp:433
double A24
Coefficient 2,4 of the matrix.
Definition convert_color_space.hpp:430
double A22
Coefficient 2,2 of the matrix.
Definition convert_color_space.hpp:424