IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
peak_icv_default_pipeline.hpp
Go to the documentation of this file.
1
11#pragma once
12
43
44#include <unordered_map>
45#include <chrono>
46#include <ctime>
47#include <iomanip>
48
49namespace peak
50{
51namespace pipeline
52{
160{
161public:
170
176 ~DefaultPipeline() override;
177
194 PEAK_COMMON_NO_DISCARD std::string ExportSettingsToString() const override;
195
214 void ImportSettingsFromString(const std::string& settings) override;
215
224 void ResetToDefault() override;
225
231 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
232
254 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& image) const;
255
274 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::common::IImageView& imageView) const;
275
287 PEAK_COMMON_NO_DISCARD peak::icv::Image operator<<(const peak::icv::Image& image) const;
288
300 PEAK_COMMON_NO_DISCARD peak::icv::Image operator<<(const peak::common::IImageView& imageView) const;
301
315 void ReleaseBuffers() const;
316
327 PEAK_COMMON_NO_DISCARD features::MirrorFeature& Mirror() const;
328
339 PEAK_COMMON_NO_DISCARD features::RotationFeature& Rotation() const;
340
351 PEAK_COMMON_NO_DISCARD features::GainFeature& Gain() const;
352
363 PEAK_COMMON_NO_DISCARD features::ColorCorrectionFeature& ColorCorrection() const;
364
375 PEAK_COMMON_NO_DISCARD features::SaturationFeature& Saturation() const;
376
387 PEAK_COMMON_NO_DISCARD features::GammaFeature& Gamma() const;
388
399 PEAK_COMMON_NO_DISCARD features::DigitalBlackFeature& DigitalBlack() const;
400
411 PEAK_COMMON_NO_DISCARD features::SharpeningFeature& Sharpening() const;
412
423 PEAK_COMMON_NO_DISCARD features::HotpixelCorrectionFeature& HotpixelCorrection() const;
424
435 PEAK_COMMON_NO_DISCARD features::BinningFeature& Binning() const;
436
447 PEAK_COMMON_NO_DISCARD features::DecimationFeature& Decimation() const;
448
459 PEAK_COMMON_NO_DISCARD features::ChromaticAdaptionFeature& ChromaticAdaption() const;
460
479
485 PEAK_COMMON_NO_DISCARD peak::common::PixelFormat GetOutputPixelFormat() const;
486
502
508 PEAK_COMMON_NO_DISCARD ProcessingPolicy GetProcessingPolicy() const
509 {
510 return m_processingPolicy;
511 }
512
528 void SetAutoFeatureModule(const std::shared_ptr<modules::IAutoFeature>& autoFeatureModule);
529
530protected:
531 PEAK_COMMON_NO_DISCARD std::vector<std::shared_ptr<modules::IModule>> GetModules() const override;
532
533 PEAK_COMMON_NO_DISCARD virtual std::vector<std::shared_ptr<features::IFeature>> GetFeatures() const;
534
535 PEAK_COMMON_NO_DISCARD virtual std::shared_ptr<detail::MonoConversionModule> MonoConversionModule() const;
536
537 PEAK_COMMON_NO_DISCARD virtual std::shared_ptr<detail::PixelFormatConversionModule> FinalConversionModule() const;
538
539private:
540 static std::string GetCurrentIsoUtcTime();
541
542 PEAK_COMMON_NO_DISCARD inline peak::common::Any Process(const peak::common::Any& input) const override;
543
544 peak::common::Any operator<<(const peak::common::Any& input) override
545 {
546 return Process(input);
547 }
548
549 ProcessingPolicy m_processingPolicy{ ProcessingPolicy::Fast };
550
551 std::shared_ptr<detail::UnpackModule> m_unpack{ std::make_shared<detail::UnpackModule>() };
552 std::shared_ptr<detail::HotpixelCorrectionModule> m_hotpixel{ std::make_shared<detail::HotpixelCorrectionModule>() };
553 std::shared_ptr<detail::DownsamplingModule> m_binning{ std::make_shared<detail::DownsamplingModule>(
555 std::shared_ptr<detail::DownsamplingModule> m_decimation{ std::make_shared<detail::DownsamplingModule>(
557 std::shared_ptr<detail::TransformationModule> m_transformation{ std::make_shared<detail::TransformationModule>() };
558 std::shared_ptr<detail::GainModule> m_gain{ std::make_shared<detail::GainModule>() };
559 std::shared_ptr<detail::DebayerModule> m_debayer{ std::make_shared<detail::DebayerModule>() };
560 std::shared_ptr<detail::ColorMatrixTransformationModule> m_colorCorrection{
561 std::make_shared<detail::ColorMatrixTransformationModule>()
562 };
563 std::shared_ptr<detail::MonoConversionModule> m_monoConversion{ std::make_shared<detail::MonoConversionModule>() };
564 std::shared_ptr<detail::SharpeningModule> m_sharpening{ std::make_shared<detail::SharpeningModule>() };
565 std::shared_ptr<detail::ToneCurveCorrectionModule> m_toneCurveCorrection{ std::make_shared<detail::ToneCurveCorrectionModule>() };
566 std::shared_ptr<detail::PixelFormatConversionModule> m_finalConversion{ std::make_shared<detail::PixelFormatConversionModule>() };
567
568 std::shared_ptr<features::HotpixelCorrectionFeature> m_hotpixelFeature{ new features::HotpixelCorrectionFeature{ *m_hotpixel } };
569 std::shared_ptr<features::BinningFeature> m_binningFeature{ new features::BinningFeature{ *m_binning } };
570 std::shared_ptr<features::DecimationFeature> m_decimationFeature{ new features::DecimationFeature{ *m_decimation } };
571 std::shared_ptr<features::MirrorFeature> m_mirrorFeature{ new features::MirrorFeature{ *m_transformation } };
572 std::shared_ptr<features::RotationFeature> m_rotationFeature{ new features::RotationFeature{ *m_transformation } };
573 std::shared_ptr<features::GainFeature> m_gainFeature{ new features::GainFeature{ *m_gain } };
574 std::shared_ptr<features::ColorCorrectionFeature> m_colorCorrectionFeature{ new features::ColorCorrectionFeature{
575 *m_colorCorrection } };
576 std::shared_ptr<features::SaturationFeature> m_saturationFeature{ new features::SaturationFeature{ *m_colorCorrection } };
577 std::shared_ptr<features::SharpeningFeature> m_sharpeningFeature{ new features::SharpeningFeature{ *m_sharpening } };
578 std::shared_ptr<features::GammaFeature> m_gammaFeature{ new features::GammaFeature{ *m_toneCurveCorrection } };
579 std::shared_ptr<features::DigitalBlackFeature> m_digitalBlackFeature{ new features::DigitalBlackFeature{ *m_toneCurveCorrection } };
580 std::shared_ptr<features::ChromaticAdaptionFeature> m_chromaticAdaptionFeature{ new features::ChromaticAdaptionFeature{
581 *m_colorCorrection } };
582
583 std::shared_ptr<modules::IAutoFeature> m_autoFeatureModule;
584
585 constexpr static int pipelineVersion{ 1 };
586};
587
589{
590 m_monoConversion->SetEnabled(false);
591
592 const auto format = m_finalConversion->GetOutputPixelFormat();
593
594 const auto bitDepth = peak::common::PixelFormatInfo(format).GetStorageBitsPerChannel();
595 m_unpack->SetProcessingPolicy(m_processingPolicy);
596 m_unpack->SetTargetBitDepth(bitDepth);
597 m_debayer->SetProcessingPolicy(m_processingPolicy);
598 m_debayer->SetTargetBitDepth(bitDepth);
599}
600
602{
603 if (m_autoFeatureModule)
604 {
605 m_autoFeatureModule->SetGainModule(nullptr);
606 }
607}
608
610{
611 const auto archive = std::make_shared<peak::icv::Archive>();
612
613 const auto moduleList = GetModules();
614 std::vector<std::shared_ptr<peak::common::serialization::IArchive>> modules;
615 modules.reserve(moduleList.size());
616
617 for (const auto& module : moduleList)
618 {
619 if (module == nullptr)
620 {
621 continue;
622 }
623 auto moduleArchive = archive->CreateArchive();
624 moduleArchive->SetString("Type", module->GetType());
625
626 auto moduleDataArchive = moduleArchive->CreateArchive();
627 module->Serialize(*moduleDataArchive);
628 moduleArchive->SetArchive("Data", moduleDataArchive);
629 modules.push_back(moduleArchive);
630 }
631 archive->SetArchiveArray("Modules", modules);
632 archive->SetInt("Version", pipelineVersion);
633 archive->SetString("Type", GetType());
634 archive->SetString("Creation", GetCurrentIsoUtcTime());
635
636 peak::icv::Serializer serializer;
637
638 std::stringstream ss;
639 serializer.Write(*archive, ss);
640 return ss.str();
641}
642
643inline void DefaultPipeline::ImportSettingsFromString(const std::string& settings)
644{
645 peak::icv::Deserializer deserializer;
646
647 std::stringstream ss;
648 ss << settings;
649 const auto archive = deserializer.Read(ss);
650
651 const auto version = archive->GetInt("Version");
652
653 if (version < 1)
654 {
656 "The 'Version' entry " + std::to_string(version) + " in the settings string is invalid! Must be at least 1.");
657 }
658
659 const auto pipelineType = archive->GetString("Type");
660 if (pipelineType != GetType())
661 {
663 "The settings for " + pipelineType + " are not compatible with this pipeline (" + GetType() + ").");
664 }
665
666 const auto modules = GetModules();
667
668 std::unordered_map<std::string, std::shared_ptr<peak::common::serialization::IArchive>> archiveMap;
669 auto moduleArchives = archive->GetArchiveArray("Modules");
670
671 for (const auto& moduleArchive : moduleArchives)
672 {
673 const auto moduleType = moduleArchive->GetString("Type");
674 const auto data = moduleArchive->GetArchive("Data");
675 archiveMap.emplace(moduleType, data);
676 }
677
678 for (const auto& module : modules)
679 {
680 const auto found = archiveMap.find(module->GetType());
681 if (found == archiveMap.end())
682 {
683 throw peak::icv::IOException(std::string("The settings for module ") + module->GetType() + " are missing!");
684 }
685
686 module->Deserialize(*found->second);
687 }
688}
689
691{
692 for (const auto& module : GetModules())
693 {
694 module->ResetToDefault();
695 module->SetEnabled(true);
696 }
697 /*
698 * Not every feature calls reset to default of its underlying module. Therefor, every feature's reset to default
699 * can have its own logic and must be resetted separately.
700 */
701 for (const auto& feature : GetFeatures())
702 {
703 feature->ResetToDefault();
704 feature->SetEnabled(true);
705 }
706 MonoConversionModule()->SetEnabled(false);
707 m_processingPolicy = ProcessingPolicy::Fast;
708}
709
710inline const char* DefaultPipeline::GetType() const
711{
712 return "DefaultPipeline";
713}
714
716{
717 m_unpack->ReleaseBuffers();
718 m_debayer->ReleaseBuffers();
719 MonoConversionModule()->ReleaseBuffers();
720 FinalConversionModule()->ReleaseBuffers();
721}
722
724{
725 return *m_mirrorFeature;
726}
727
729{
730 return *m_rotationFeature;
731}
732
734{
735 return *m_gainFeature;
736}
737
739{
740 return *m_colorCorrectionFeature;
741}
742
744{
745 return *m_saturationFeature;
746}
747
749{
750 return *m_gammaFeature;
751}
752
754{
755 return *m_digitalBlackFeature;
756}
757
759{
760 return *m_sharpeningFeature;
761}
762
764{
765 return *m_hotpixelFeature;
766}
767
769{
770 return *m_binningFeature;
771}
772
774{
775 return *m_decimationFeature;
776}
777
779{
780 return *m_chromaticAdaptionFeature;
781}
782
784{
785 auto data = input;
786
787 for (const auto& mod : GetModules())
788 {
789 if (!mod)
790 {
791 continue;
792 }
793
794 try
795 {
796 data = mod->Process(data);
797 }
798 catch (const peak::icv::Exception& e)
799 {
800 const auto errorCode = e.GetStatus();
801 const auto* const what = e.what();
802 const auto* const moduleType = mod->GetType();
803
804 const auto errorText = std::string{ "[" } + moduleType + "] " + what;
805 switch (errorCode)
806 {
807 case PEAK_ICV_STATUS_LIBRARY_NOT_INITIALIZED:
808 case PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING:
809 throw peak::icv::InvalidConfigurationException(errorText, errorCode);
810 case PEAK_ICV_STATUS_INVALID_HANDLE:
811 case PEAK_ICV_STATUS_INVALID_BUFFER_SIZE:
812 case PEAK_ICV_STATUS_MISMATCH:
813 case PEAK_ICV_STATUS_NULL_POINTER:
814 case PEAK_ICV_STATUS_NOT_SUPPORTED:
815 case PEAK_ICV_STATUS_NOT_POSSIBLE:
816 throw peak::icv::NotSupportedException(errorText);
817 case PEAK_ICV_STATUS_CORRUPTED:
818 throw peak::icv::CorruptedException(errorText);
819 case PEAK_ICV_STATUS_OUT_OF_RANGE:
820 throw peak::icv::OutOfRangeException(errorText);
821 case PEAK_ICV_STATUS_INTERNAL_ERROR:
822 throw peak::icv::Exception(errorText, errorCode);
823 default:
824 throw peak::icv::Exception("Generic Error!", errorCode);
825 }
826 }
827 }
828
829 if (input.AnyCast<peak::icv::Image>().GetData() == data.AnyCast<peak::icv::Image>().GetData())
830 {
831 return peak::common::Any(data.AnyCast<peak::icv::Image>().Copy());
832 }
833 return data;
834}
835
837{
838 return Process(peak::common::Any(image)).AnyCast<peak::icv::Image>();
839}
840
842{
843 return Process(peak::icv::Image(imageView));
844}
845
847{
848 return Process(image);
849}
850
852{
853 return Process(imageView);
854}
855
857{
858 return FinalConversionModule()->GetOutputPixelFormat();
859}
860
862{
863 const peak::common::PixelFormatInfo info(format);
864
865 const bool isBayerFormat = info.IsSingleChannel() && info.HasChannel(peak::common::Channel::Bayer);
866 const bool isMonoFormat = info.IsSingleChannel() && info.HasIntensityChannel();
867
868 if (isBayerFormat)
869 {
870 m_debayer->SetConversionPolicy(detail::DebayerConversionPolicy::Bypass);
871 }
872 else if (isMonoFormat)
873 {
874 m_debayer->SetConversionPolicy(detail::DebayerConversionPolicy::BayerOnly);
875 m_debayer->SetChannelLayout(DebayerChannelLayout::RGB);
876 }
877 else
878 {
879 m_debayer->SetConversionPolicy(detail::DebayerConversionPolicy::BayerAndMono);
880
883 {
884 m_debayer->SetConversionPolicy(detail::DebayerConversionPolicy::BayerAndMono);
885 switch (format)
886 {
890 m_debayer->SetChannelLayout(DebayerChannelLayout::RGB);
891 break;
895 m_debayer->SetChannelLayout(DebayerChannelLayout::RGBA);
896 break;
900 m_debayer->SetChannelLayout(DebayerChannelLayout::BGR);
901 break;
905 m_debayer->SetChannelLayout(DebayerChannelLayout::BGRA);
906 break;
907 default:
908 break;
909 }
910 }
911 }
912
913 MonoConversionModule()->SetEnabled(isMonoFormat);
914
915 FinalConversionModule()->SetOutputPixelFormat(format);
916
917 const auto bitDepth = info.GetStorageBitsPerChannel();
918 m_unpack->SetTargetBitDepth(bitDepth);
919 m_debayer->SetTargetBitDepth(bitDepth);
920}
921
923{
924 m_processingPolicy = policy;
925
926 m_unpack->SetProcessingPolicy(m_processingPolicy);
927 m_debayer->SetProcessingPolicy(m_processingPolicy);
928}
929
930inline void DefaultPipeline::SetAutoFeatureModule(const std::shared_ptr<modules::IAutoFeature>& autoFeatureModule)
931{
932 if (m_autoFeatureModule != nullptr)
933 {
934 m_autoFeatureModule->SetGainModule(nullptr);
935 }
936
937 m_autoFeatureModule = autoFeatureModule;
938
939 m_colorCorrection->SetAutoFeatureModule(m_autoFeatureModule);
940
941 if (m_autoFeatureModule != nullptr)
942 {
943 m_autoFeatureModule->SetGainModule(m_gain);
944 }
945}
946
947inline std::string DefaultPipeline::GetCurrentIsoUtcTime()
948{
949 const auto now = std::chrono::system_clock::now();
950 const std::time_t nowC = std::chrono::system_clock::to_time_t(now);
951 std::tm tm{};
952
953#if defined(_WIN32) || defined(_WIN64)
954 gmtime_s(&tm, &nowC);
955#else
956 gmtime_r(&nowC, &tm);
957#endif
958
959 std::ostringstream oss;
960 oss << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
961 return oss.str();
962}
963
964inline std::vector<std::shared_ptr<modules::IModule>> DefaultPipeline::GetModules() const
965{
966 if (m_autoFeatureModule == nullptr)
967 {
968 return { m_unpack, m_hotpixel, m_binning, m_decimation, m_transformation, m_gain, m_debayer, m_colorCorrection,
969 MonoConversionModule(), m_sharpening, m_toneCurveCorrection, FinalConversionModule() };
970 }
971
972 return { m_unpack, m_hotpixel, m_binning, m_decimation, m_transformation, m_gain, m_debayer, m_colorCorrection, m_autoFeatureModule,
973 MonoConversionModule(), m_sharpening, m_toneCurveCorrection, FinalConversionModule() };
974}
975
976inline std::vector<std::shared_ptr<features::IFeature>> DefaultPipeline::GetFeatures() const
977{
978 return { m_hotpixelFeature, m_binningFeature, m_decimationFeature, m_mirrorFeature, m_rotationFeature, m_gainFeature,
979 m_colorCorrectionFeature, m_chromaticAdaptionFeature, m_saturationFeature, m_sharpeningFeature, m_gammaFeature,
980 m_digitalBlackFeature };
981}
982
983inline std::shared_ptr<detail::MonoConversionModule> DefaultPipeline::MonoConversionModule() const
984{
985 return m_monoConversion;
986}
987
988inline std::shared_ptr<detail::PixelFormatConversionModule> DefaultPipeline::FinalConversionModule() const
989{
990 return m_finalConversion;
991}
992} // namespace pipeline
993} // namespace peak
IDSImaging.Peak.ICV.Pipeline.Types.ProcessingPolicy ProcessingPolicy
A type-erased container for any copyable value.
T & AnyCast()
Casts the stored value to the specified type.
Interface for viewing image pixel data from arbitrary sources.
Provides information and utility functions for pixel formats.
bool HasIntensityChannel() const
Checks whether the pixel format includes an intensity channel.
bool HasChannel(Channel channel) const
Checks whether a specific channel is present in the pixel format.
size_t GetStorageBitsPerChannel() const
Returns the number of bits used per channel for storage.
bool IsSingleChannel() const
Checks whether the pixel format contains only a single channel.
Occurs when reading from a file or stream that is incomplete, damaged, or in an unexpected format.
Deserializer for reading serialized data and converting it into an IArchive object.
std::shared_ptr< peak::common::serialization::IArchive > Read(std::istream &stream) override
Reads serialized data from the specified stream and deserializes it into an archive.
Base class for all exceptions thrown by the IDS peak ICV library.
peak_icv_status GetStatus() const
Occurs when a file operation fails (e.g., missing file, insufficient permissions, or inaccessible dir...
For detailed information about the use of ids_peak_icv Image refer to the Image Guide.
uint8_t * GetData() const
Provides a pointer to the image data buffer.
Image Copy() const
Creates a deep copy of the current Image.
Occurs when a required dependency is missing, the library is not initialized, or a misconfiguration a...
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Occurs when a provided value falls outside the allowed valid range.
Serializer for writing IArchive objects to an output stream.
void Write(const peak::common::serialization::IArchive &archive, std::ostream &stream) override
Serializes an archive and writes it to a stream.
Image processing pipeline for sequential transformation of raw image frames.
features::MirrorFeature & Mirror() const
Provides access to the mirror feature for image mirroring operations.
void SetAutoFeatureModule(const std::shared_ptr< modules::IAutoFeature > &autoFeatureModule)
Sets an auto feature module for automatic image adjustments.
peak::icv::Image Process(const peak::icv::Image &image) const
Applies the pipeline to the given image and returns the result as a new image.
peak::icv::Image operator<<(const peak::icv::Image &image) const
Convenience operator for processing input using the pipeline.
features::SharpeningFeature & Sharpening() const
Provides access to the sharpening feature for image detail enhancement.
void SetOutputPixelFormat(peak::common::PixelFormat format)
Defines the desired pixel format for processed images.
features::DecimationFeature & Decimation() const
Provides access to the decimation feature for resolution reduction by pixel skipping.
void ImportSettingsFromString(const std::string &settings) override
Restores pipeline settings from a previously serialized string.
features::BinningFeature & Binning() const
Provides access to the binning feature for resolution reduction and noise improvement.
features::DigitalBlackFeature & DigitalBlack() const
Provides access to the digital black feature for sensor offset correction.
void ReleaseBuffers() const
Frees unused internal buffers.
features::SaturationFeature & Saturation() const
Provides access to the saturation feature for color intensity adjustments.
peak::common::PixelFormat GetOutputPixelFormat() const
features::ColorCorrectionFeature & ColorCorrection() const
Provides access to the color correction feature for color space transformations.
features::HotpixelCorrectionFeature & HotpixelCorrection() const
Provides access to the hot pixel correction feature for defective pixel repair.
features::GammaFeature & Gamma() const
Provides access to the gamma feature for brightness and contrast adjustments.
features::ChromaticAdaptionFeature & ChromaticAdaption() const
Provides access to the chromatic adaption feature for adjusting to lighting conditions.
DefaultPipeline()
Creates a DefaultPipeline with default module settings.
features::GainFeature & Gain() const
Provides access to the gain feature for brightness and white balance adjustments.
std::string ExportSettingsToString() const override
Converts the current pipeline settings into a string format.
void SetProcessingPolicy(ProcessingPolicy policy)
Sets the processing policy for the image processing pipeline.
features::RotationFeature & Rotation() const
Provides access to the rotation feature for image rotation operations.
void ResetToDefault() override
Resets all modules in the pipeline to their default state. Also resets their enabled state....
Base class for implementing a custom pipeline.
Binning is used to decrease the image size.
Adjusts to changes in lighting conditions to maintain consistent color perception despite variations ...
Color correction applies a 3x3 ColorCorrectionMatrix to the image.
Decimation is used to decrease the image size.
Digital black applies digital black correction to an image.
The GainFeature applies gain to the image.
Gamma applies an inverse gamma transformation to an image.
HotpixelCorrection detects and corrects hot pixels in camera images.
Mirror provides functionality to flip the image left-right or up-down.
Rotation provides fixed-angle rotation functionality.
Adjusts the saturation of an image by modifying a 3x3 ColorCorrectionMatrix.
Sharpening applies a sharpening filter to enhance image detail.
PixelFormat
Enum listing all supported pixel formats and their internal IDs.
@ RGBa10
RGBA 10-Bit pixel format.
@ RGB8
RGB 8-Bit pixel format.
@ RGBa8
RGBA 8-Bit pixel format.
@ RGBa12
RGBA 12-Bit pixel format.
@ BGRa12
BGRA 12-Bit pixel format.
@ BGR12
BGR 12-Bit pixel format.
@ RGB10
RGB 10-Bit pixel format.
@ BGR10
BGR 10-Bit pixel format.
@ RGB12
RGB 12-Bit pixel format.
@ BGRa8
BGRA 8-Bit pixel format.
@ BGRa10
BGRA 10-Bit pixel format.
@ BGR8
BGR 8-Bit pixel format.
@ BinningAverage
The averaged pixel values of neighboring rows and/or columns are computed during binning.
@ Decimation
The additional pixel values of neighboring rows and/or columns are skipped during decimation.
ProcessingPolicy
Specifies the processing policy.
Enum holding the possible pixel format names.