mmcfilters
Public API documentation
Loading...
Searching...
No Matches
AttributeFilters.hpp
1#pragma once
2
3#include "../utils/Image.hpp"
4#include "../utils/Common.hpp"
5#include "../trees/TreeAltitudeAlgorithms.hpp"
6#include "../trees/ValuedMorphologicalTree.hpp"
7#include "../trees/ValuedMorphologicalTreeView.hpp"
8#include "../trees/detail/TreeKernelAccess.hpp"
9#include "../utils/CommittedImageAccess.hpp"
10#include "../utils/Contract.hpp"
11#include "AttributeReconstructionFilters.hpp"
12#include "detail/ViterbiDecision.hpp"
13
14#include <cmath>
15#include <concepts>
16#include <memory>
17#include <stack>
18#include <stdexcept>
19#include <string>
20#include <vector>
21
22namespace mmcfilters {
23
70template <AltitudeValue T> class AttributeFilters {
71 protected:
73
74 using AltitudeView = ValuedMorphologicalTreeView<T>;
75
77 AltitudeView view_;
79 const ValuedMorphologicalTree<T>* valuedTree_ = nullptr;
81 const MorphologicalTree& tree;
83 std::size_t treeMutationVersion_ = 0;
84
90 AltitudeView view() const noexcept { return view_; }
91
97 void requireStableTree(const char* context) const { MMCFILTERS_CONTRACT_CHECKED_ONLY(tree.requireMutationVersion(treeMutationVersion_, context)); }
98
105 template <std::floating_point Real> static void requireAttributePointer(const Real* attribute, const char* context) {
106 MMCFILTERS_CONTRACT_REQUIRE(attribute != nullptr,
107 throw std::invalid_argument(std::string(context) + " requires a non-null attribute buffer."));
108 }
109
117 template <detail::NodePreservationDecisions Mask>
118 static void requireNodePreservationMaskSize(const MorphologicalTree& tree, const Mask& nodePreservationMask,
119 const char* context) {
120 MMCFILTERS_CONTRACT_REQUIRE(nodePreservationMask.size() == static_cast<std::size_t>(tree.numInternalNodeSlots()),
121 throw std::invalid_argument(std::string(context) +
122 " node-preservation-mask size must match the internal node slot count."));
123 }
124
132 template <typename TImagePtr> static void requireOutputImage(const MorphologicalTree& tree, const TImagePtr& image, const char* context) {
133 MMCFILTERS_CONTRACT_REQUIRE(image != nullptr, throw std::invalid_argument(std::string(context) + " requires a non-null output image."));
134 MMCFILTERS_CONTRACT_REQUIRE(image->getNumRows() == tree.numRows() && image->getNumColumns() == tree.numColumns(),
135 throw std::invalid_argument(std::string(context) + " output image shape must match the tree image domain."));
136 }
137
145 static T altitudeOf(const AltitudeView& view, NodeId nodeId) noexcept { return view.nodeAltitudes()[static_cast<std::size_t>(nodeId)]; }
146
158 template <typename TValue> static void writeProperParts(const MorphologicalTree& tree, NodeId nodeId, TValue* output, TValue value) {
159 for (PixelId pixel : detail::TreeKernelAccess::properParts(tree, nodeId)) {
160 output[pixel] = value;
161 }
162 }
163
176 template <typename TValue> static void writeSubtreeProperParts(const MorphologicalTree& tree, NodeId nodeId, TValue* output, TValue value) {
177 for (NodeId subtreeNodeId : detail::TreeKernelAccess::subtree(tree, nodeId)) {
178 writeProperParts(tree, subtreeNodeId, output, value);
179 }
180 }
181
195 template <detail::NodePreservationDecisions Mask>
196 static void filteringByPruningMinMaskImpl(AltitudeView view, const Mask& nodePreservationMask, ImagePtr<T> imgOutputPtr) {
197 const char* context = "AttributeFilters::filteringByPruningMin";
198 view.requireTopologyUnchanged(context);
199 const MorphologicalTree& tree = view.topology();
200 requireNodePreservationMaskSize(tree, nodePreservationMask, context);
201 requireOutputImage(tree, imgOutputPtr, context);
202 std::stack<NodeId> stack;
203 stack.push(tree.root());
204 auto imgOutput = imgOutputPtr->rawData();
205
206 while (!stack.empty()) {
207 const NodeId nodeId = stack.top();
208 stack.pop();
209 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
210 for (NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
211 if (nodePreservationMask[static_cast<std::size_t>(childNodeId)]) {
212 stack.push(childNodeId);
213 } else {
214 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, nodeId));
215 }
216 }
217 }
218 }
219
233 template <detail::NodePreservationDecisions Mask>
234 static void filteringByPruningMaxMaskImpl(AltitudeView view, const Mask& nodePreservationMask, ImagePtr<T> imgOutputPtr) {
235 const char* context = "AttributeFilters::filteringByPruningMax";
236 view.requireTopologyUnchanged(context);
237 const MorphologicalTree& tree = view.topology();
238 requireNodePreservationMaskSize(tree, nodePreservationMask, context);
239 requireOutputImage(tree, imgOutputPtr, context);
240 // This internal marker means "this whole subtree can be collapsed",
241 // which is the opposite of the caller's preservation decision at the leaves
242 // before descendant information is merged.
243 std::vector<uint8_t> collapsibleRejectedSubtree(tree.numInternalNodeSlots(), false);
244 detail::TreeKernelAccess::traversePostOrder(
245 tree, tree.root(),
246 [&collapsibleRejectedSubtree, &nodePreservationMask](NodeId nodeId) -> void {
247 collapsibleRejectedSubtree[nodeId] = !nodePreservationMask[static_cast<std::size_t>(nodeId)];
248 },
249 [&collapsibleRejectedSubtree](NodeId parentNodeId, NodeId childNodeId) -> void {
250 collapsibleRejectedSubtree[parentNodeId] =
251 (collapsibleRejectedSubtree[parentNodeId] & collapsibleRejectedSubtree[childNodeId]);
252 },
253 [](NodeId) -> void {});
254
255 auto imgOutput = imgOutputPtr->rawData();
256 std::stack<NodeId> stack;
257 stack.push(tree.root());
258 while (!stack.empty()) {
259 const NodeId nodeId = stack.top();
260 stack.pop();
261 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
262 for (NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
263 if (!collapsibleRejectedSubtree[childNodeId]) {
264 stack.push(childNodeId);
265 } else {
266 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, childNodeId));
267 }
268 }
269 }
270 }
271
286 template <std::floating_point Real>
287 static void filteringByPruningMinAttributeImpl(AltitudeView view, const Real* attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
288 const char* context = "AttributeFilters::filteringByPruningMin";
289 view.requireTopologyUnchanged(context);
290 const MorphologicalTree& tree = view.topology();
291 requireAttributePointer(attribute, context);
292 requireOutputImage(tree, imgOutputPtr, context);
293 auto imgOutput = imgOutputPtr->rawData();
294 std::stack<NodeId> stack;
295 stack.push(tree.root());
296 while (!stack.empty()) {
297 const NodeId nodeId = stack.top();
298 stack.pop();
299 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
300 for (NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
301 if (attribute[childNodeId] >= threshold) {
302 stack.push(childNodeId);
303 } else {
304 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, nodeId));
305 }
306 }
307 }
308 }
309
323 template <std::floating_point Real>
324 static void filteringByPruningMaxAttributeImpl(AltitudeView view, const Real* attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
325 const char* context = "AttributeFilters::filteringByPruningMax";
326 view.requireTopologyUnchanged(context);
327 const MorphologicalTree& tree = view.topology();
328 requireAttributePointer(attribute, context);
329 requireOutputImage(tree, imgOutputPtr, context);
330 // This internal marker records collapsible rejected subtrees, not a
331 // direct keep decision. The post-order merge implements the universal
332 // quantifier over descendants.
333 std::vector<uint8_t> collapsibleRejectedSubtree(tree.numInternalNodeSlots(), false);
334 detail::TreeKernelAccess::traversePostOrder(
335 tree, tree.root(),
336 [&collapsibleRejectedSubtree, attribute, threshold](NodeId nodeId) -> void {
337 if (attribute[nodeId] < threshold) {
338 collapsibleRejectedSubtree[nodeId] = true;
339 }
340 },
341 [&collapsibleRejectedSubtree](NodeId parentNodeId, NodeId childNodeId) -> void {
342 collapsibleRejectedSubtree[parentNodeId] =
343 (collapsibleRejectedSubtree[parentNodeId] & collapsibleRejectedSubtree[childNodeId]);
344 },
345 [](NodeId) -> void {});
346
347 auto imgOutput = imgOutputPtr->rawData();
348 std::stack<NodeId> stack;
349 stack.push(tree.root());
350 while (!stack.empty()) {
351 const NodeId nodeId = stack.top();
352 stack.pop();
353 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
354 for (NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
355 if (!collapsibleRejectedSubtree[childNodeId]) {
356 stack.push(childNodeId);
357 } else {
358 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, childNodeId));
359 }
360 }
361 }
362 }
363
365
366 public:
375 explicit AttributeFilters(AltitudeView view) : view_{view}, tree{view_.topology()}, treeMutationVersion_{tree.getMutationVersion()} {
376 MMCFILTERS_CONTRACT_CHECKED_ONLY(view_.requireTopologyUnchanged("AttributeFilters"));
377 }
378
388 explicit AttributeFilters(const ValuedMorphologicalTree<T>& valuedTree) : AttributeFilters(valuedTree.asView()) { valuedTree_ = &valuedTree; }
389
393 ~AttributeFilters() = default;
394
407 template <std::floating_point Real> [[nodiscard]] ImagePtr<T> filteringByPruningMin(const std::shared_ptr<Real[]>& attr, Real threshold) {
408 return filteringByPruningMin(attr.get(), threshold);
409 }
410
419 template <std::floating_point Real> [[nodiscard]] ImagePtr<T> filteringByPruningMin(const Real* attr, Real threshold) {
420 ImagePtr<T> imgOutput = Image<T>::create(this->tree.numRows(), this->tree.numColumns());
421 filteringByPruningMinAttributeImpl(view(), attr, threshold, imgOutput);
422 return imgOutput;
423 }
424
436 template <std::floating_point Real> [[nodiscard]] ImagePtr<T> filteringByPruningMax(const std::shared_ptr<Real[]>& attr, Real threshold) {
437 return filteringByPruningMax(attr.get(), threshold);
438 }
439
448 template <std::floating_point Real> [[nodiscard]] ImagePtr<T> filteringByPruningMax(const Real* attr, Real threshold) {
449 ImagePtr<T> imgOutput = Image<T>::create(this->tree.numRows(), this->tree.numColumns());
450 filteringByPruningMaxAttributeImpl(view(), attr, threshold, imgOutput);
451 return imgOutput;
452 }
453
468 template <std::floating_point Real> [[nodiscard]] ImagePtr<T> filteringByViterbiRule(const Real* attr, Real threshold) {
469 requireStableTree("AttributeFilters::filteringByViterbiRule");
470 auto costs = detail::makeThresholdViterbiCosts(tree, attr, threshold);
471 NodePreservationMask nodePreservationMask(detail::computeViterbiPreservationDecisions(tree, costs));
472 return applyDirectAttributeFilter(view(), nodePreservationMask);
473 }
474
482 template <detail::NodePreservationDecisions Mask>
483 [[nodiscard]] ImagePtr<T> filteringByPruningMin(const Mask& nodePreservationMask) {
484 ImagePtr<T> imgOutput = Image<T>::create(this->tree.numRows(), this->tree.numColumns());
485 filteringByPruningMinMaskImpl(view(), nodePreservationMask, imgOutput);
486 return imgOutput;
487 }
488
496 template <detail::NodePreservationDecisions Mask>
497 [[nodiscard]] ImagePtr<T> filteringByPruningMax(const Mask& nodePreservationMask) {
498 ImagePtr<T> imgOutput = Image<T>::create(this->tree.numRows(), this->tree.numColumns());
499 filteringByPruningMaxMaskImpl(view(), nodePreservationMask, imgOutput);
500 return imgOutput;
501 }
502
511 template <detail::NodePreservationDecisions Mask>
512 static void filteringByPruningMin(const ValuedMorphologicalTreeView<T>& valuedTree, const Mask& nodePreservationMask,
513 ImagePtr<T> imgOutputPtr) {
514 filteringByPruningMinMaskImpl(valuedTree, nodePreservationMask, imgOutputPtr);
515 }
516
525 template <detail::NodePreservationDecisions Mask>
526 static void filteringByPruningMin(const ValuedMorphologicalTree<T>& valuedTree, const Mask& nodePreservationMask,
527 ImagePtr<T> imgOutputPtr) {
528 filteringByPruningMin(valuedTree.asView(), nodePreservationMask, imgOutputPtr);
529 }
530
539 template <detail::NodePreservationDecisions Mask>
540 static void filteringByPruningMax(const ValuedMorphologicalTreeView<T>& valuedTree, const Mask& nodePreservationMask,
541 ImagePtr<T> imgOutputPtr) {
542 filteringByPruningMaxMaskImpl(valuedTree, nodePreservationMask, imgOutputPtr);
543 }
544
553 template <detail::NodePreservationDecisions Mask>
554 static void filteringByPruningMax(const ValuedMorphologicalTree<T>& valuedTree, const Mask& nodePreservationMask,
555 ImagePtr<T> imgOutputPtr) {
556 filteringByPruningMax(valuedTree.asView(), nodePreservationMask, imgOutputPtr);
557 }
558
568 template <std::floating_point Real>
569 static void filteringByPruningMin(const ValuedMorphologicalTreeView<T>& valuedTree, const std::shared_ptr<Real[]>& attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
570 filteringByPruningMin(valuedTree, attribute.get(), threshold, imgOutputPtr);
571 }
572
582 template <std::floating_point Real>
583 static void filteringByPruningMin(const ValuedMorphologicalTree<T>& valuedTree, const std::shared_ptr<Real[]>& attribute, Real threshold,
584 ImagePtr<T> imgOutputPtr) {
585 filteringByPruningMin(valuedTree.asView(), attribute.get(), threshold, imgOutputPtr);
586 }
587
597 template <std::floating_point Real>
598 static void filteringByPruningMin(const ValuedMorphologicalTreeView<T>& valuedTree, const Real* attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
599 filteringByPruningMinAttributeImpl(valuedTree, attribute, threshold, imgOutputPtr);
600 }
601
611 template <std::floating_point Real>
612 static void filteringByPruningMin(const ValuedMorphologicalTree<T>& valuedTree, const Real* attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
613 filteringByPruningMin(valuedTree.asView(), attribute, threshold, imgOutputPtr);
614 }
615
625 template <std::floating_point Real>
626 static void filteringByPruningMax(const ValuedMorphologicalTreeView<T>& valuedTree, const std::shared_ptr<Real[]>& attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
627 filteringByPruningMax(valuedTree, attribute.get(), threshold, imgOutputPtr);
628 }
629
639 template <std::floating_point Real>
640 static void filteringByPruningMax(const ValuedMorphologicalTree<T>& valuedTree, const std::shared_ptr<Real[]>& attribute, Real threshold,
641 ImagePtr<T> imgOutputPtr) {
642 filteringByPruningMax(valuedTree.asView(), attribute.get(), threshold, imgOutputPtr);
643 }
644
654 template <std::floating_point Real>
655 static void filteringByPruningMax(const ValuedMorphologicalTreeView<T>& valuedTree, const Real* attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
656 filteringByPruningMaxAttributeImpl(valuedTree, attribute, threshold, imgOutputPtr);
657 }
658
668 template <std::floating_point Real>
669 static void filteringByPruningMax(const ValuedMorphologicalTree<T>& valuedTree, const Real* attribute, Real threshold, ImagePtr<T> imgOutputPtr) {
670 filteringByPruningMax(valuedTree.asView(), attribute, threshold, imgOutputPtr);
671 }
672
673};
674
675} // namespace mmcfilters
Shared scalar conventions used by the C++ API.
int PixelId
Pixel identifier type used by source and active construction domains.
Definition Common.hpp:26
int NodeId
Node identifier type used throughout the project.
Definition Common.hpp:17
Compile-time policy for defensive checks at untrusted API boundaries.
#define MMCFILTERS_CONTRACT_REQUIRE(condition,...)
Evaluates a caller precondition and its failure action only in checked builds.
Definition Contract.hpp:53
#define MMCFILTERS_CONTRACT_CHECKED_ONLY(...)
Executes validation statements only when defensive checks are enabled.
Definition Contract.hpp:67
Contiguous row-major image container and coordinate helpers.
std::shared_ptr< Image< PixelType > > ImagePtr
Shared pointer alias for an image with arbitrary pixel type.
Definition Image.hpp:269
AttributeFilters(const ValuedMorphologicalTree< T > &valuedTree)
Creates filtering operators over an owned valued tree.
static void filteringByPruningMin(const ValuedMorphologicalTree< T > &valuedTree, const std::shared_ptr< Real[]> &attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-min filtering from a valued-tree owner and owned attribute buffer.
static void filteringByPruningMin(const ValuedMorphologicalTreeView< T > &valuedTree, const std::shared_ptr< Real[]> &attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-min filtering from an owned attribute buffer into an output image.
static void filteringByPruningMax(const ValuedMorphologicalTree< T > &valuedTree, const Real *attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-max filtering from a valued-tree owner and raw attribute buffer.
static void filteringByPruningMin(const ValuedMorphologicalTree< T > &valuedTree, const Real *attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-min filtering from a valued-tree owner and raw attribute buffer.
static void filteringByPruningMax(const ValuedMorphologicalTree< T > &valuedTree, const std::shared_ptr< Real[]> &attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-max filtering from a valued-tree owner and owned attribute buffer.
ImagePtr< T > filteringByViterbiRule(const Real *attr, Real threshold)
Applies Salembier-style Viterbi filtering from a raw attribute buffer.
~AttributeFilters()=default
Destroys the attribute-filter facade.
AttributeFilters(AltitudeView view)
Creates filtering operators over a non-owning valued tree view.
static void filteringByPruningMin(const ValuedMorphologicalTreeView< T > &valuedTree, const Real *attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-min filtering from a raw attribute buffer into an output image.
ImagePtr< T > filteringByPruningMin(const Mask &nodePreservationMask)
Applies pruning-min filtering from a dense node-preservation mask.
static void filteringByPruningMax(const ValuedMorphologicalTree< T > &valuedTree, const Mask &nodePreservationMask, ImagePtr< T > imgOutputPtr)
Writes pruning-max filtering from a valued-tree owner into an output image.
static void filteringByPruningMin(const ValuedMorphologicalTree< T > &valuedTree, const Mask &nodePreservationMask, ImagePtr< T > imgOutputPtr)
Writes pruning-min filtering from a valued-tree owner into an output image.
static void filteringByPruningMax(const ValuedMorphologicalTreeView< T > &valuedTree, const std::shared_ptr< Real[]> &attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-max filtering from an owned attribute buffer into an output image.
static void filteringByPruningMax(const ValuedMorphologicalTreeView< T > &valuedTree, const Mask &nodePreservationMask, ImagePtr< T > imgOutputPtr)
Writes pruning-max filtering from a node-preservation mask into an output image.
ImagePtr< T > filteringByPruningMin(const std::shared_ptr< Real[]> &attr, Real threshold)
Applies pruning-min filtering from an attribute buffer.
static void filteringByPruningMin(const ValuedMorphologicalTreeView< T > &valuedTree, const Mask &nodePreservationMask, ImagePtr< T > imgOutputPtr)
Writes pruning-min filtering from a node-preservation mask into an output image.
ImagePtr< T > filteringByPruningMax(const Real *attr, Real threshold)
Applies pruning-max filtering from a raw internal-node attribute buffer.
static void filteringByPruningMax(const ValuedMorphologicalTreeView< T > &valuedTree, const Real *attribute, Real threshold, ImagePtr< T > imgOutputPtr)
Writes pruning-max filtering from a raw attribute buffer into an output image.
ImagePtr< T > filteringByPruningMax(const Mask &nodePreservationMask)
Applies pruning-max filtering from a dense node-preservation mask.
ImagePtr< T > filteringByPruningMin(const Real *attr, Real threshold)
Applies pruning-min filtering from a raw internal-node attribute buffer.
ImagePtr< T > filteringByPruningMax(const std::shared_ptr< Real[]> &attr, Real threshold)
Applies pruning-max filtering from an attribute buffer.
static Ptr create(int rows, int columns)
Creates an owned image with uninitialised pixel values.
Definition Image.hpp:89
Mutable connected-subset tree on a finite pixel domain.
int numRows() const
Returns the number of rows in the regular 2D pixel domain.
int numInternalNodeSlots() const
Returns the size of the dense internal-node id domain.
void requireMutationVersion(std::size_t expectedVersion, const char *context) const
Rejects stale read-only views that captured an older mutation version.
int numColumns() const
Returns the number of columns in the regular 2D pixel domain.
NodeId root() const
Returns the current hierarchy root.
Dense Boolean decisions where true means preserve the node.
Non-owning view pairing a topology with an external altitude span.
Wrapper pairing MorphologicalTree topology with an external altitude buffer.
ValuedMorphologicalTreeView< T > asView() const
Creates a non-owning view using the established altitude guarantees of this owner.