IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
peak_icv_color_matrix_transformation_module.hpp
Go to the documentation of this file.
1
11#pragma once
12
19#include <peak_icv_c/algorithms/preprocessing/peak_icv_color_matrix_transformation.h>
20
21namespace peak
22{
23namespace pipeline
24{
25namespace features
26{
29} // namespace features
30
31namespace detail
32{
33
44{
45public:
54
61
70
77
86
93
105 void Serialize(peak::common::serialization::IArchive& archive) const override;
106
122 void Deserialize(const peak::common::serialization::IArchive& archive) override;
123
131 void SetEnabled(bool enabled) override;
132
140 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
141
149 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
150
157 void ResetToDefault() override;
158
166 void SetMatrix(const ColorCorrectionMatrix& matrix);
167
175 PEAK_COMMON_NO_DISCARD ColorCorrectionMatrix GetMatrix() const;
176
186 void SetSaturation(float saturation);
187
195 PEAK_COMMON_NO_DISCARD float GetSaturation() const;
196
204 PEAK_COMMON_NO_DISCARD peak::common::IntervalF GetSaturationRange() const;
205
220 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& input) const;
221
238 PEAK_COMMON_NO_DISCARD peak::common::Any Process(const peak::common::Any& input) const override;
239
245 void SetAutoFeatureModule(const std::shared_ptr<modules::IAutoFeature>& module);
246
247private:
248#ifndef DOXYGEN_SHOULD_SKIP_THIS
251#endif
252
253 PEAK_COMMON_NO_DISCARD ColorCorrectionMatrix FetchMatrix();
254 void ApplyMatrix() const;
255 void UpdateCCMInAutoFeatureModule() const;
256 PEAK_COMMON_NO_DISCARD ColorCorrectionMatrix GetEffectiveMatrix() const;
257 void SetSaturationEnabled(bool enabled);
258 void SetColorCorrectionEnabled(bool enabled);
259 PEAK_COMMON_NO_DISCARD bool IsSaturationEnabled() const;
260 PEAK_COMMON_NO_DISCARD bool IsColorCorrectionEnabled() const;
261
262 ColorCorrectionMatrix m_colorCorrectionMatrix{ "Matrix", { ColorCorrectionMatrix::Matrix::Identity() } };
263 static constexpr auto saturationDefault = 1.0F;
264
265 static constexpr auto saturationKey = "Saturation";
266 static constexpr auto matrixKey = "ColorCorrection";
267
268 peak_icv_color_matrix_transformation_handle m_handle{};
269
270 bool m_saturationEnabled{ true };
271 bool m_colorCorrectionEnabled{ true };
272
273 float m_saturation{ saturationDefault };
274 std::shared_ptr<modules::IAutoFeature> m_autoFeatureModule;
275};
276
278{
280 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Create(&m_handle);
281 });
282
283 m_colorCorrectionMatrix = FetchMatrix();
284}
285
287{
288 if (m_handle != nullptr)
289 {
290 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
291 }
292}
293
295 : m_colorCorrectionMatrix(std::move(other.m_colorCorrectionMatrix))
296 , m_saturationEnabled(other.m_saturationEnabled)
297 , m_colorCorrectionEnabled(other.m_colorCorrectionEnabled)
298{
299 m_handle = std::exchange(other.m_handle, nullptr);
300}
301
303{
304 if (this != &other)
305 {
306 if (m_handle != nullptr)
307 {
309 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
310 });
311 }
312
313 m_handle = std::exchange(other.m_handle, nullptr);
314 m_colorCorrectionEnabled = other.m_colorCorrectionEnabled;
315 m_saturationEnabled = other.m_saturationEnabled;
316 m_colorCorrectionMatrix = std::move(other.m_colorCorrectionMatrix);
317 }
318
319 return *this;
320}
321
323{
324 archive.SetInt("Version", 1);
325
326 const auto saturationArchive = archive.CreateArchive();
327 saturationArchive->SetBool("Enabled", IsColorCorrectionEnabled());
328 saturationArchive->SetDouble("Value", GetSaturation());
329 archive.SetArchive(saturationKey, saturationArchive);
330
331 const auto matrixArchive = archive.CreateArchive();
332 matrixArchive->SetBool("Enabled", IsColorCorrectionEnabled());
333
334 m_colorCorrectionMatrix.Serialize(*matrixArchive);
335 archive.SetArchive(matrixKey, matrixArchive);
336}
337
339{
340 const auto version = archive.GetInt("Version");
341
343
344 const auto saturationArchive = archive.GetArchive(saturationKey);
345 const auto matrixArchive = archive.GetArchive(matrixKey);
346
347 SetColorCorrectionEnabled(saturationArchive->GetBool("Enabled"));
348 SetSaturationEnabled(matrixArchive->GetBool("Enabled"));
349
350 m_colorCorrectionMatrix.Deserialize(*matrixArchive);
351 SetSaturation(static_cast<float>(saturationArchive->GetDouble("Value")));
352
353 ApplyMatrix();
354}
355
357{
358 SetColorCorrectionEnabled(enabled);
359 SetSaturationEnabled(enabled);
360
361 UpdateCCMInAutoFeatureModule();
362}
363
365{
366 return m_colorCorrectionEnabled || m_saturationEnabled;
367}
368
370{
371 return "ColorMatrixTransformation";
372}
373
375{
376 const auto identity = ColorCorrectionMatrix::Identity();
377 std::copy(identity.cbegin(), identity.cend(), m_colorCorrectionMatrix.begin());
378
379 if (m_colorCorrectionEnabled)
380 {
381 ApplyMatrix();
382 }
383
384 SetSaturation(saturationDefault);
385}
386
388{
389 return m_colorCorrectionMatrix;
390}
391
393{
394 std::copy(matrix.cbegin(), matrix.cend(), m_colorCorrectionMatrix.begin());
395
396 if (m_colorCorrectionEnabled)
397 {
398 ApplyMatrix();
399 }
400}
401
403{
404 if (m_saturationEnabled)
405 {
407 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(m_handle, saturation);
408 });
409 m_saturation = saturation;
410 UpdateCCMInAutoFeatureModule();
411 }
412 else
413 {
414 m_saturation = saturation;
415 }
416}
417
418PEAK_COMMON_NO_DISCARD inline float ColorMatrixTransformationModule::GetSaturation() const
419{
420 return m_saturation;
421}
422
424{
425 peak_common_interval_f intervalF;
427 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange(m_handle, &intervalF);
428 });
429
430 return { intervalF.minimum, intervalF.maximum };
431}
432
437
439{
440 auto needsProcessing = [&] {
441 bool value{};
443 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_NeedsProcessing(m_handle, &value);
444 });
445 return value;
446 };
447
448 if (!IsEnabled() || !needsProcessing())
449 {
450 return input;
451 }
452
453 const auto& inputImage = input.AnyCast<peak::icv::Image>();
454
455 const auto pixelFormat = inputImage.GetPixelFormat();
456 const peak::common::PixelFormatInfo info(pixelFormat);
459 if (!isColorImage || pixelFormat == peak::common::PixelFormat::RGB10p32 || pixelFormat == peak::common::PixelFormat::BGR10p32)
460 {
461 return input;
462 }
463
464 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
465
467 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace(m_handle, inputImageHandle);
468 });
469
470 return peak::common::Any{ inputImage };
471}
472
473inline void ColorMatrixTransformationModule::UpdateCCMInAutoFeatureModule() const
474{
475 if (m_autoFeatureModule != nullptr)
476 {
477 std::array<float, 9> ccm{};
478
479 const auto matrix = (m_saturationEnabled || m_colorCorrectionEnabled) ? GetEffectiveMatrix() :
481 std::copy(matrix.cbegin(), matrix.cend(), ccm.begin());
482
483 m_autoFeatureModule->SetColorCorrectionMatrix(ccm);
484 }
485}
486
487inline void ColorMatrixTransformationModule::SetAutoFeatureModule(const std::shared_ptr<modules::IAutoFeature>& module)
488{
489 m_autoFeatureModule = module;
490
491 UpdateCCMInAutoFeatureModule();
492}
493
494inline ColorCorrectionMatrix ColorMatrixTransformationModule::FetchMatrix()
495{
496 ColorCorrectionMatrix colorCorrectionMatrix{ m_colorCorrectionMatrix };
498 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetColorCorrectionMatrix(
499 m_handle, reinterpret_cast<peak_icv_color_correction_matrix*>(&colorCorrectionMatrix.At(0, 0)));
500 });
501
502 return colorCorrectionMatrix;
503}
504
505inline void ColorMatrixTransformationModule::ApplyMatrix() const
506{
507 auto matrix = m_colorCorrectionEnabled ? m_colorCorrectionMatrix : ColorCorrectionMatrix(ColorCorrectionMatrix::Identity());
509 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix(
510 m_handle, *reinterpret_cast<peak_icv_color_correction_matrix*>(&matrix.At(0, 0)));
511 });
512
513 UpdateCCMInAutoFeatureModule();
514}
515
516inline ColorCorrectionMatrix ColorMatrixTransformationModule::GetEffectiveMatrix() const
517{
518 ColorCorrectionMatrix matrix{};
520 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix(
521 m_handle, reinterpret_cast<peak_icv_color_correction_matrix*>(&matrix.At(0, 0)));
522 });
523 return matrix;
524}
525
526inline void ColorMatrixTransformationModule::SetSaturationEnabled(bool enabled)
527{
528 m_saturationEnabled = enabled;
529
531 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(
532 m_handle, m_saturationEnabled ? m_saturation : saturationDefault);
533 });
534}
535
536inline void ColorMatrixTransformationModule::SetColorCorrectionEnabled(bool enabled)
537{
538 m_colorCorrectionEnabled = enabled;
539 ApplyMatrix();
540}
541
542inline bool ColorMatrixTransformationModule::IsSaturationEnabled() const
543{
544 return m_saturationEnabled;
545}
546
547inline bool ColorMatrixTransformationModule::IsColorCorrectionEnabled() const
548{
549 return m_colorCorrectionEnabled;
550}
551
552} // namespace detail
553} // namespace pipeline
554} // namespace peak
A type-erased container for any copyable value.
T & AnyCast()
Casts the stored value to the specified type.
Provides information and utility functions for pixel formats.
bool HasChannel(Channel channel) const
Checks whether a specific channel is present in the pixel format.
Represents a continuous numeric interval with inclusive lower and upper bounds.
Interface for a hierarchical key-value archive system.
virtual std::shared_ptr< IArchive > GetArchive(const std::string &key) const =0
Retrieves a nested archive by key.
virtual void SetInt(const std::string &key, int64_t value)=0
Stores a 64-bit signed integer.
virtual int64_t GetInt(const std::string &key) const =0
Retrieves a 64-bit signed integer by key.
virtual std::shared_ptr< IArchive > CreateArchive() const =0
Creates a new, empty archive instance.
virtual void SetArchive(const std::string &key, const std::shared_ptr< IArchive > &value)=0
Stores a nested archive object.
auto cbegin() const
Returns a const iterator to the beginning of the array.
auto begin()
Returns an iterator to the beginning of the array.
auto cend() const
Returns a const iterator to the end of the array.
For detailed information about the use of ids_peak_icv Image refer to the Image Guide.
peak::common::PixelFormat GetPixelFormat() const
Provides the pixel format of the image.
void Serialize(peak::common::serialization::IArchive &archive) const override
Serializes the object's internal state into the provided archive.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
Color matrix transformation is an image pipeline module that applies a ColorCorrectionMatrix to an im...
ColorCorrectionMatrix GetMatrix() const
Returns the current ColorCorrectionMatrix.
ColorMatrixTransformationModule()
Creates an instance of class ColorMatrixTransformationModule with the default color correction matrix...
const char * GetType() const override
Returns the type of the module for serialization purposes.
ColorMatrixTransformationModule(const ColorMatrixTransformationModule &other)=delete
Copy constructor for class ColorMatrixTransformation is deleted.
void SetAutoFeatureModule(const std::shared_ptr< modules::IAutoFeature > &module)
Sets the auto feature module that should take the color correction matrix into account for automatic ...
void SetMatrix(const ColorCorrectionMatrix &matrix)
Sets the ColorCorrectionMatrix.
void Serialize(peak::common::serialization::IArchive &archive) const override
Serializes the object's internal state into the provided archive.
peak::icv::Image Process(const peak::icv::Image &input) const
Applies the configured ColorCorrectionMatrix and saturation adjustment to the input image.
void SetEnabled(bool enabled) override
Sets the enabled state of this module.
ColorMatrixTransformationModule & operator=(const ColorMatrixTransformationModule &other)=delete
Copy assignment for class ColorMatrixTransformation is deleted.
peak::common::IntervalF GetSaturationRange() const
Returns the saturation range.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
void ResetToDefault() override
Resets the ColorCorrectionMatrix to the default and the saturation value to the default.
bool IsEnabled() const override
Checks if the module is currently enabled.
Color correction applies a 3x3 ColorCorrectionMatrix to the image.
Adjusts the saturation of an image by modifying a 3x3 ColorCorrectionMatrix.
Interface for a module used within a pipeline.
@ BGR10p32
BGR 10-Bit packed 32-bit format.
@ RGB10p32
RGB 10-Bit packed 32-bit format.
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
void ValidateVersion(int64_t version, const std::string &moduleName)