IDS peak AFL v1.8.0
Loading...
Searching...
No Matches
peak_afl_weighted_rois.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <peak_afl/peak_afl.hpp>
15
18
19#include <memory>
20
21namespace peak
22{
23namespace pipeline
24{
25namespace modules
26{
27namespace autofeature
28{
29
40enum class Weight
41{
48};
49
68
81PEAK_COMMON_NO_DISCARD inline std::string ToString(Weight weight)
82{
83 switch (weight)
84 {
85 case Weight::Weak:
86 return "Weak";
87 case Weight::Medium:
88 return "Medium";
89 case Weight::Strong:
90 return "Strong";
91 }
92
93 throw peak::common::InternalErrorException("Invalid weight!");
94}
95
114inline std::ostream& operator<<(std::ostream& os, Weight weight)
115{
116 return os << ToString(weight);
117}
118
119namespace features
120{
121
122namespace detail
123{
124
125PEAK_COMMON_NO_DISCARD inline Weight ToWeight(const std::string& name)
126{
127 if (name == "Weak")
128 {
129 return Weight::Weak;
130 }
131 if (name == "Medium")
132 {
133 return Weight::Medium;
134 }
135 if (name == "Strong")
136 {
137 return Weight::Strong;
138 }
139 throw peak::common::InternalErrorException("Invalid weight!");
140}
141
143{
144 switch (weight)
145 {
146 case Weight::Weak:
148 case Weight::Medium:
150 case Weight::Strong:
152 }
153
154 throw peak::common::InternalErrorException("Invalid weight!");
155}
156
158{
159 switch (weight)
160 {
162 return Weight::Weak;
164 return Weight::Medium;
166 return Weight::Strong;
167 }
168
169 throw peak::common::InternalErrorException("Invalid weight!");
170}
171
172PEAK_COMMON_NO_DISCARD inline peak_afl_weighted_rectangle ToCType(WeightedROI roi)
173{
174 peak_afl_weighted_rectangle result;
175 result.roi.x = roi.rect.GetX();
176 result.roi.y = roi.rect.GetY();
177 result.roi.width = roi.rect.GetWidth();
178 result.roi.height = roi.rect.GetHeight();
179 result.weight = ToCType(roi.weight);
180 return result;
181}
182
183PEAK_COMMON_NO_DISCARD inline WeightedROI ToWeightedROI(peak_afl_weighted_rectangle roi)
184{
185 const peak::common::Rectangle rect{ { static_cast<int32_t>(roi.roi.x), static_cast<int32_t>(roi.roi.y) },
186 { roi.roi.width, roi.roi.height } };
187 const auto weight = ToWeight(roi.weight);
188 return { rect, weight };
189}
190} // namespace detail
191
224class WeightedRois : public IFeature<std::vector<WeightedROI>>
225{
226public:
239 explicit WeightedRois(std::shared_ptr<peak::afl::Controller> controller)
240 : m_controller{ std::move(controller) }
241 {}
242
260 void Set(const WeightedROI& value)
261 {
262 m_controller->SetWeightedROI(detail::ToCType(value));
263 }
264
283 void Set(const std::vector<WeightedROI>& value) override
284 {
285 std::vector<peak_afl_weighted_rectangle> cRois;
286 cRois.reserve(value.size());
287 std::transform(value.cbegin(), value.cend(), std::back_inserter(cRois), [](const auto& roi) { return detail::ToCType(roi); });
288 m_controller->SetWeightedROIs(cRois);
289 }
290
306 PEAK_COMMON_NO_DISCARD std::vector<WeightedROI> Get() const override
307 {
308 const auto cRois = m_controller->GetWeightedROIs();
309
310 std::vector<WeightedROI> rois;
311 rois.reserve(cRois.size());
312 std::transform(
313 cRois.cbegin(), cRois.cend(), std::back_inserter(rois), [](const auto& cRoi) { return detail::ToWeightedROI(cRoi); });
314 return rois;
315 }
316
333 {
334 std::vector<std::shared_ptr<peak::common::serialization::IArchive>> archives;
335 const auto weightedRois = Get();
336 for (const auto& roi : weightedRois)
337 {
338 auto subArchives = archive.CreateArchive();
339 subArchives->SetInt("X", roi.rect.GetX());
340 subArchives->SetInt("Y", roi.rect.GetY());
341 subArchives->SetInt("Width", roi.rect.GetWidth());
342 subArchives->SetInt("Height", roi.rect.GetHeight());
343 subArchives->SetString("Weight", ToString(roi.weight));
344
345 archives.push_back(subArchives);
346 }
347
348 archive.SetArchiveArray("WeightedRois", archives);
349 }
350
368 {
369 std::vector<WeightedROI> rois;
370 const auto archives = archive.GetArchiveArray("WeightedRois");
371 for (const auto& subArchive : archives)
372 {
374 roi.roi.x = static_cast<uint32_t>(subArchive->GetInt("X"));
375 roi.roi.y = static_cast<uint32_t>(subArchive->GetInt("Y"));
376 roi.roi.width = static_cast<uint32_t>(subArchive->GetInt("Width"));
377 roi.roi.height = static_cast<uint32_t>(subArchive->GetInt("Height"));
378 roi.weight = detail::ToCType(detail::ToWeight(subArchive->GetString("Weight")));
379 rois.emplace_back(detail::ToWeightedROI(roi));
380 }
381
382 Set(rois);
383 }
384
385private:
393 std::shared_ptr<peak::afl::Controller> m_controller;
394};
395
396} // namespace features
397} // namespace autofeature
398} // namespace modules
399} // namespace pipeline
400} // namespace peak
Thrown when an unexpected internal error occurs.
Definition peak_common_exceptions.hpp:74
Interface for a hierarchical key-value archive system.
Definition peak_common_iarchive.hpp:59
virtual void SetArchiveArray(const std::string &key, const std::vector< std::shared_ptr< IArchive > > &value)=0
Stores an array of nested archives.
virtual PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< IArchive > > GetArchiveArray(const std::string &key) const =0
Retrieves an array of nested archives by key.
virtual PEAK_COMMON_NO_DISCARD std::shared_ptr< IArchive > CreateArchive() const =0
Creates a new, empty archive instance.
Base interface for auto feature implementations.
Definition peak_afl_ifeature.hpp:61
void Set(const std::vector< WeightedROI > &value) override
Sets multiple weighted regions of interest.
Definition peak_afl_weighted_rois.hpp:283
PEAK_COMMON_NO_DISCARD std::vector< WeightedROI > Get() const override
Gets the current weighted regions of interest.
Definition peak_afl_weighted_rois.hpp:306
WeightedRois(std::shared_ptr< peak::afl::Controller > controller)
Constructs a WeightedRois feature with the specified controller.
Definition peak_afl_weighted_rois.hpp:239
void Serialize(peak::common::serialization::IArchive &archive) const override
Serializes the current weighted ROIs to an archive.
Definition peak_afl_weighted_rois.hpp:332
void Set(const WeightedROI &value)
Sets a single weighted region of interest.
Definition peak_afl_weighted_rois.hpp:260
void Deserialize(const peak::common::serialization::IArchive &archive) override
Deserializes weighted ROIs from an archive.
Definition peak_afl_weighted_rois.hpp:367
PEAK_COMMON_NO_DISCARD std::string ToString(ControllerType mode)
Converts ControllerType enumeration to string representation.
Definition peak_afl_icontroller.hpp:79
Weight
Weight enumeration for weighted regions of interest.
Definition peak_afl_weighted_rois.hpp:41
@ Weak
Low importance - minimal influence on auto algorithms.
Definition peak_afl_weighted_rois.hpp:43
@ Medium
Normal importance - standard influence on auto algorithms.
Definition peak_afl_weighted_rois.hpp:45
@ Strong
High importance - strong influence on auto algorithms.
Definition peak_afl_weighted_rois.hpp:47
#define PEAK_COMMON_NO_DISCARD
Compatability macro for the nodiscard attribute.
Definition peak_common_defines.hpp:91
detail::RectangleT< int32_t, uint32_t > Rectangle
Rectangle with integer coordinates and unsigned dimensions.
Definition peak_common_rectangle.hpp:235
peak_afl_roi_weight
ROI weight values for focus control.
Definition peak_afl.h:770
@ PEAK_AFL_CONTROLLER_ROI_WEIGHT_WEAK
Low importance weight.
Definition peak_afl.h:777
@ PEAK_AFL_CONTROLLER_ROI_WEIGHT_STRONG
High importance weight.
Definition peak_afl.h:793
@ PEAK_AFL_CONTROLLER_ROI_WEIGHT_MEDIUM
Medium importance weight.
Definition peak_afl.h:785
Interface definitions and implementations for individual module types used within a pipeline.
Definition peak_afl_generic_auto_brightness.hpp:39
Interfaces and implementations for defining and controlling data processing pipelines.
Definition peak_afl_generic_auto_brightness.hpp:37
Weighted region of interest structure.
Definition peak_afl_weighted_rois.hpp:61
peak::common::Rectangle rect
The rectangular region of interest.
Definition peak_afl_weighted_rois.hpp:63
Weight weight
The importance weight for this region.
Definition peak_afl_weighted_rois.hpp:66
uint32_t width
Width of rectangle in pixels.
Definition peak_afl.h:1248
uint32_t height
Height of rectangle in pixels.
Definition peak_afl.h:1255
uint32_t y
Y-coordinate of top-left corner.
Definition peak_afl.h:1241
uint32_t x
X-coordinate of top-left corner.
Definition peak_afl.h:1234
Weighted region of interest for focus control.
Definition peak_afl.h:1270
peak_afl_roi_weight weight
Weight of this region.
Definition peak_afl.h:1285
peak_afl_rectangle roi
Rectangular region definition.
Definition peak_afl.h:1277