3#include "../trees/TreeAltitudeAlgorithms.hpp"
4#include "../trees/ValuedMorphologicalTree.hpp"
5#include "../trees/ValuedMorphologicalTreeView.hpp"
6#include "../trees/detail/TreeKernelAccess.hpp"
7#include "../trees/saliency/HierarchySaliencyMapValidation.hpp"
8#include "../trees/saliency/HierarchySaliencyMap.hpp"
9#include "../trees/saliency/HierarchicalWatershedSaliency.hpp"
13#include "../contours/ContourComputation.hpp"
62 enum class Mode { TopK, MinimumExtinction };
65 Mode mode_ = Mode::TopK;
67 int extremaToKeep_ = 0;
69 Real threshold_ = Real{0};
81 policy.mode_ = Mode::TopK;
95 policy.mode_ = Mode::MinimumExtinction;
96 policy.threshold_ = threshold;
105 [[nodiscard]]
bool selectsTopK() const noexcept {
return mode_ == Mode::TopK; }
127enum class ExtinctionContourScorePolicy {
179 std::vector<RegionalExtremaNode<Real>> regionalExtremaNodes_;
181 std::vector<Real> extinctionValueAttribute_;
189 std::size_t treeMutationVersion_ = 0;
192 struct SelectedExtremum {
209 [[nodiscard]] AltitudeView view()
const {
return valuedTree_ !=
nullptr ? valuedTree_->
asView() : view_; }
220 void requireStableTree(
const char* context)
const { tree.
requireMutationVersion(treeMutationVersion_, context); }
229 static void requireAttributePointer(
const Real* attr,
const char* context) {
231 throw std::invalid_argument(std::string(context) +
" requires a non-null attribute buffer."));
244 static const Real* requireAttributeBuffer(
const MorphologicalTree& tree,
const std::vector<Real>& attr,
const char* context) {
246 throw std::invalid_argument(std::string(context) +
" attribute size must match the internal node slot count."));
260 static T altitudeOf(
const AltitudeView& view,
NodeId nodeId)
noexcept {
261 return view.nodeAltitudes()[
static_cast<std::size_t
>(nodeId)];
275 std::vector<NodeId> collectComponentTreeExtrema()
const {
return this->tree.
leaves(); }
286 static Real dominantExtremumSentinel()
noexcept {
return std::numeric_limits<Real>::max(); }
295 static void requireNonNegativeExtremaToKeep(
int extremaToKeep,
const char* context) {
297 throw std::invalid_argument(std::string(context) +
" requires a non-negative extremaToKeep value."));
307 static void requireFiniteThreshold(Real threshold,
const char* context) {
309 throw std::invalid_argument(std::string(context) +
" requires a finite extinction threshold."));
325 requireNonNegativeExtremaToKeep(policy.
extremaToKeep(), context);
343 static void requireFormalExtinctionValue(Real value,
NodeId nodeId,
const char* context) {
344 if (!std::isfinite(value)) {
345 throw std::invalid_argument(std::string(context) +
" requires finite extinction values.");
347 if (value < Real{0}) {
348 throw std::invalid_argument(std::string(context) +
" requires non-negative extinction values.");
364 void buildExtinctionValueAttribute() {
365 extinctionValueAttribute_.assign(
static_cast<std::size_t
>(tree.
numInternalNodeSlots()), Real{0});
367 Real& localValue = extinctionValueAttribute_[
static_cast<std::size_t
>(record.leaf)];
368 if (localValue < record.extinction) {
369 localValue = record.extinction;
374 Real& nodeValue = extinctionValueAttribute_[
static_cast<std::size_t
>(nodeId)];
375 for (
NodeId childId : detail::TreeKernelAccess::children(tree, nodeId)) {
376 const Real childValue = extinctionValueAttribute_[
static_cast<std::size_t
>(childId)];
377 if (nodeValue < childValue) {
378 nodeValue = childValue;
394 void validateExtinctionValueAttribute(
const char* context)
const {
396 requireFormalExtinctionValue(record.extinction, record.leaf, context);
399 HierarchyValuationPolicy::AllowLevelCollapse,
400 HierarchyValuationRangePolicy::RequireNonNegative, context);
418 requireValidSelectionPolicy(policy, context);
420 std::vector<const RegionalExtremaNode<Real>*> records;
422 const int extremaToSelect = std::min(policy.
extremaToKeep(),
static_cast<int>(regionalExtremaNodes_.size()));
423 records.reserve(
static_cast<std::size_t
>(extremaToSelect));
424 for (
int i = 0; i < extremaToSelect; ++i) {
425 records.push_back(®ionalExtremaNodes_[
static_cast<std::size_t
>(i)]);
428 records.reserve(regionalExtremaNodes_.size());
431 records.push_back(&record);
436 std::vector<SelectedExtremum> selected;
437 selected.reserve(records.size());
438 const int selectedCount =
static_cast<int>(records.size());
439 for (
int i = 0; i < selectedCount; ++i) {
440 selected.push_back(SelectedExtremum{records[
static_cast<std::size_t
>(i)], selectedCount - i});
458 [[nodiscard]]
ImagePtr<T> filteringFromExtremumPreservationMask(std::vector<uint8_t> extremumPreservationMask,
const char* context)
const {
459 requireStableTree(context);
460 const AltitudeView altitudeView = view();
462 if (!tree.
isRoot(nodeId) && extremumPreservationMask[
static_cast<std::size_t
>(nodeId)]) {
463 extremumPreservationMask[
static_cast<std::size_t
>(detail::TreeKernelAccess::nodeParent(tree, nodeId))] =
true;
468 auto imgOutput = imgOutputPtr->rawData();
469 std::stack<NodeId> stack;
470 stack.push(tree.
root());
471 while (!stack.empty()) {
472 const NodeId nodeId = stack.top();
474 const T level = altitudeOf(altitudeView, nodeId);
475 for (
PixelId pixel : detail::TreeKernelAccess::properParts(tree, nodeId)) {
476 imgOutput[pixel] = level;
478 for (
NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
479 if (extremumPreservationMask[
static_cast<std::size_t
>(childNodeId)]) {
480 stack.push(childNodeId);
482 for (
NodeId subtreeNodeId : detail::TreeKernelAccess::subtree(tree, childNodeId)) {
483 for (
PixelId pixel : detail::TreeKernelAccess::properParts(tree, subtreeNodeId)) {
484 imgOutput[pixel] = level;
503 [[nodiscard]]
ImagePtr<T> filteringFromSelectedExtrema(
const std::vector<SelectedExtremum>& selected,
const char* context)
const {
505 for (
const SelectedExtremum& item : selected) {
506 extremumPreservationMask[
static_cast<std::size_t
>(item.record->leaf)] =
true;
508 return filteringFromExtremumPreservationMask(std::move(extremumPreservationMask), context);
532 void initialize(
const Real* attr) {
533 requireAttributePointer(attr,
"ExtinctionValues");
534 std::vector<NodeId> leaves = collectComponentTreeExtrema();
535 regionalExtremaNodes_.reserve(leaves.size());
537 for (
NodeId leafNodeId : leaves) {
538 Real extinction = dominantExtremumSentinel();
539 NodeId cutoffNodeId = leafNodeId;
540 NodeId parentNodeId = detail::TreeKernelAccess::nodeParent(this->tree, cutoffNodeId);
542 while (flag && !this->tree.
isRoot(cutoffNodeId)) {
543 if (detail::TreeKernelAccess::numChildren(this->tree, parentNodeId) > 1) {
544 for (
NodeId sonNodeId : detail::TreeKernelAccess::children(this->tree, parentNodeId)) {
546 if (visited[sonNodeId] && sonNodeId != cutoffNodeId && attr[sonNodeId] == attr[cutoffNodeId]) {
548 }
else if (sonNodeId != cutoffNodeId && attr[sonNodeId] > attr[cutoffNodeId]) {
551 visited[sonNodeId] =
true;
556 cutoffNodeId = parentNodeId;
557 parentNodeId = detail::TreeKernelAccess::nodeParent(this->tree, cutoffNodeId);
560 if (!this->tree.
isRoot(cutoffNodeId)) {
561 extinction = attr[cutoffNodeId];
563 regionalExtremaNodes_.emplace_back(leafNodeId, cutoffNodeId, extinction);
566 std::sort(regionalExtremaNodes_.begin(), regionalExtremaNodes_.end(), [](
const auto& a,
const auto& b) {
567 if (a.extinction != b.extinction) {
568 return a.extinction > b.extinction;
570 if (a.cutoffNode != b.cutoffNode) {
571 return a.cutoffNode < b.cutoffNode;
573 return a.leaf < b.leaf;
575 buildExtinctionValueAttribute();
606 :
ExtinctionValues(view, requireAttributeBuffer(view.topology(), attr,
"ExtinctionValues")) {}
617 ExtinctionValues(
const AltitudeView& view,
const Real* attr) : view_(view), tree(view_.topology()), treeMutationVersion_(tree.getMutationVersion()) {
618 view_.requireTopologyUnchanged(
"ExtinctionValues");
635 valuedTree_ = &valuedTree;
650 valuedTree_ = &valuedTree;
681 constexpr const char* context =
"ExtinctionValues::contourMap";
682 requireStableTree(context);
683 const std::vector<SelectedExtremum> selected = selectExtrema(selection, context);
685 std::vector<uint8_t> keep(tree.numInternalNodeSlots(),
false);
686 std::vector<Real> scoreByNode(tree.numInternalNodeSlots(), Real{0});
687 std::vector<NodeId> keptNodes;
688 for (
const SelectedExtremum& item : selected) {
689 const NodeId cutoffNode = item.record->cutoffNode;
690 Real score = Real{0};
691 switch (scorePolicy) {
692 case ExtinctionContourScorePolicy::RankScore:
693 score =
static_cast<Real
>(item.rankScore);
695 case ExtinctionContourScorePolicy::ExtinctionValue:
696 score = item.record->extinction;
699 throw std::invalid_argument(std::string(context) +
" received an unknown extinction contour score policy.");
701 if (!keep[
static_cast<std::size_t
>(cutoffNode)]) {
702 keptNodes.push_back(cutoffNode);
703 keep[
static_cast<std::size_t
>(cutoffNode)] =
true;
704 scoreByNode[
static_cast<std::size_t
>(cutoffNode)] = score;
705 }
else if (scoreByNode[
static_cast<std::size_t
>(cutoffNode)] < score) {
706 scoreByNode[
static_cast<std::size_t
>(cutoffNode)] = score;
711 auto contourOutput = imgOutputPtr->rawData();
713 if (keptNodes.empty()) {
718 std::vector<std::size_t> priorityByNode(tree.numInternalNodeSlots(), 0);
719 for (std::size_t selectionIndex = 0; selectionIndex < keptNodes.size(); ++selectionIndex) {
720 priorityByNode[
static_cast<std::size_t
>(keptNodes[selectionIndex])] = selectionIndex + 1;
722 std::vector<std::size_t> pixelPriority(tree.numPixels(), 0);
724 const auto selectionPriority = priorityByNode[
static_cast<std::size_t
>(node)];
725 if (selectionPriority == 0) {
729 if (selectionPriority > pixelPriority[
static_cast<std::size_t
>(pixel)]) {
730 contourOutput[pixel] = scoreByNode[
static_cast<std::size_t
>(node)];
731 pixelPriority[
static_cast<std::size_t
>(pixel)] = selectionPriority;
764 constexpr const char* context =
"ExtinctionValues::getExtinctionValueAttribute";
765 requireStableTree(context);
766 validateExtinctionValueAttribute(context);
767 return extinctionValueAttribute_;
913 HierarchyValuationPolicy::AllowLevelCollapse);
939 constexpr const char* context =
"ExtinctionValues::filtering";
940 const std::vector<SelectedExtremum> selected = selectExtrema(selection, context);
941 return filteringFromSelectedExtrema(selected, context);
951 requireStableTree(
"ExtinctionValues::getRegionalExtrema");
952 return regionalExtremaNodes_;
Shared scalar conventions used by the C++ API.
int PixelId
Pixel identifier type used by source and active construction domains.
int NodeId
Node identifier type used throughout the project.
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.
Contiguous row-major image container and coordinate helpers.
std::shared_ptr< Image< PixelType > > ImagePtr
Shared pointer alias for an image with arbitrary pixel type.
Incremental foreground A4 contours on the image domain.
void forEachContour(Consumer &&consumer) const
Calls consumer(node, pixels) once for every live node.
Explicit extinction-extrema selection policy.
int extremaToKeep() const noexcept
Maximum number of ranked extrema retained by a top-k policy.
bool selectsTopK() const noexcept
Returns true when the policy selects the first ranked extrema.
static ExtinctionSelectionPolicy byThreshold(Real threshold) noexcept
Select every extremum whose extinction value is at least threshold.
static ExtinctionSelectionPolicy byTopK(int extremaToKeep) noexcept
Select the strongest extrema by decreasing extinction ranking.
Real minimumExtinction() const noexcept
Minimum accepted extinction value for a threshold policy.
ExtinctionValues(const AltitudeView &view, const std::shared_ptr< Real[]> &attr)
Computes extinction values from a valued-tree view and shared attribute buffer.
EdgeSaliencyMap< Real > computeMonotoneExtinctionProjection(const RegularGridAdjacency2D &adjacency) const
Projects the max-descendant extinction attribute directly by LCA.
ExtinctionValues(const ValuedMorphologicalTree< T > &valuedTree, const std::vector< Real > &attr)
Computes extinction values from a valued tree and vector attribute buffer.
ExtinctionValues(const ValuedMorphologicalTree< T > &valuedTree, const std::shared_ptr< Real[]> &attr)
Computes extinction values from a valued tree and shared attribute buffer.
const std::vector< RegionalExtremaNode< Real > > & getRegionalExtrema() const
Returns regional-extremum records sorted by decreasing extinction.
EdgeSaliencyMap< int > computeRankedMonotoneExtinctionProjection(const RegularGridAdjacency2D &adjacency) const
Computes canonical effective-edge ranks for the monotone projection.
ImagePtr< T > filtering(const ExtinctionSelectionPolicy< Real > &selection) const
Reconstructs an image from selected regional extrema.
EdgeSaliencyMap< Real > computeFormalSaliencyEdgeMap(const RegularGridAdjacency2D &adjacency) const
Computes the formal hierarchical-watershed extinction saliency map.
EdgeSaliencyMap< int > computeRankedFormalSaliencyEdgeMap(const RegularGridAdjacency2D &adjacency) const
Computes a ranked formal extinction saliency edge map.
Real value_type
Scalar type used for input attributes and extinction values.
ExtinctionValues(const ValuedMorphologicalTree< T > &valuedTree, const Real *attr)
Computes extinction values from a valued tree and raw attribute buffer.
EdgeSaliencyMap< int > computeRankedFormalSaliencyEdgeMap() const
Computes a ranked formal extinction saliency edge map using stored adjacency.
std::vector< int > computeRankedExtinctionValueAttribute() const
Builds a dense integer extinction attribute from extinction levels.
ExtinctionValues(const AltitudeView &view, const Real *attr)
Computes extinction values from a valued-tree view and raw attribute buffer.
EdgeSaliencyMap< int > computeRankedMonotoneExtinctionProjection() const
Stored-adjacency overload of computeRankedMonotoneExtinctionProjection.
EdgeSaliencyMap< Real > computeFormalSaliencyEdgeMap() const
Computes the formal extinction saliency edge map using stored adjacency.
ExtinctionValues(const AltitudeView &view, const std::vector< Real > &attr)
Computes extinction values from a valued-tree view and vector attribute buffer.
ImagePtr< Real > contourMap(const ExtinctionSelectionPolicy< Real > &selection, ExtinctionContourScorePolicy scorePolicy) const
Builds a contour-valued image from selected extinction events.
const std::vector< Real > & getExtinctionValueAttribute() const
Returns extinction values extended from extrema to every hierarchy node.
EdgeSaliencyMap< Real > computeMonotoneExtinctionProjection() const
Stored-adjacency overload of computeMonotoneExtinctionProjection.
static EdgeSaliencyMap< Real > compute(const ValuedMorphologicalTreeView< T > &valuedTree, std::span< const Real > leafExtinction, const RegularGridAdjacency2D &adjacency)
Computes the full-graph extinction hierarchical-watershed saliency.
static EdgeSaliencyMap< int > computeRanked(const ValuedMorphologicalTreeView< T > &valuedTree, std::span< const Real > leafExtinction, const RegularGridAdjacency2D &adjacency)
Computes the canonical dense rank scale of compute.
static void validateHierarchyValuation(const MorphologicalTree &tree, std::span< const Value > valuation, HierarchyValuationPolicy policy=HierarchyValuationPolicy::AllowLevelCollapse, HierarchyValuationRangePolicy rangePolicy=HierarchyValuationRangePolicy::AllowAnyFinite, const char *context="HierarchySaliencyMapValidation::validateHierarchyValuation")
Validates that a node-indexed valuation is compatible with a hierarchy.
static std::vector< int > rankHierarchyValuation(const MorphologicalTree &tree, std::span< const Value > valuation, HierarchyValuationPolicy policy=HierarchyValuationPolicy::AllowLevelCollapse)
Converts a compatible valuation to dense non-negative integer levels.
static EdgeSaliencyMap< int > computeCanonicalRankedSaliencyEdgeMap(const MorphologicalTree &tree, const RegularGridAdjacency2D &adjacency, std::span< const Value > valuation, HierarchyValuationPolicy policy=HierarchyValuationPolicy::AllowLevelCollapse, HierarchyConnectivityPolicy connectivityPolicy=HierarchyConnectivityPolicy::ValidateConnected)
Computes the canonical dense integer saliency scale of a valuation.
static RegularGridAdjacency2D requireProjectionAdjacency(const MorphologicalTree &tree, const char *context="HierarchySaliencyMap::requireProjectionAdjacency")
Returns the unambiguous adjacency stored by a hierarchy.
static EdgeSaliencyMap< Value > computeSaliencyEdgeMap(const MorphologicalTree &tree, const RegularGridAdjacency2D &adjacency, std::span< const Value > valuation, HierarchyValuationPolicy policy=HierarchyValuationPolicy::AllowLevelCollapse, HierarchyLevelConvention levelConvention=HierarchyLevelConvention::EdgeSaliencyValue, HierarchyConnectivityPolicy connectivityPolicy=HierarchyConnectivityPolicy::ValidateConnected)
Computes the formal edge-indexed saliency map induced by a valuation.
static Ptr create(int rows, int columns)
Creates an owned image with uninitialised pixel values.
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.
PostOrderNodeRange postOrder() const
Returns a post-order traversal range rooted at the connected root.
void requireMutationVersion(std::size_t expectedVersion, const char *context) const
Rejects stale read-only views that captured an older mutation version.
bool isRoot(NodeId nodeId) const
Tests whether nodeId is the current root.
std::vector< NodeId > leaves() const
Returns all live leaf nodes in the current hierarchy.
int numColumns() const
Returns the number of columns in the regular 2D pixel domain.
NodeId root() const
Returns the current hierarchy root.
Immutable regular-grid 2D adjacency with allocation-free traversal.
static void requireGlobalMonotoneAltitudeOrder(const MorphologicalTree &tree, const char *context)
Requires a declared globally monotone altitude order.
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.
Edge-indexed saliency map induced by a morphological hierarchy.
Record describing one regional extremum and its extinction value.
RegionalExtremaNode(NodeId leaf, NodeId cutoffNode, Real extinction)
Builds one extinction-value record.
Real extinction
Attribute value at cutoffNode, or numeric_limits<Real>::max() for the dominant extremum that survives...
NodeId cutoffNode
Highest node retained before the extremum merges with a stronger branch.
NodeId leaf
Leaf node that represents the regional extremum in a max-tree/min-tree.