22enum class CasfComponentTreesAttribute { Area, BoundingBoxWidth, BoundingBoxHeight, BoundingBoxDiagonal };
95 std::unique_ptr<attribute_computer_t> minAttributeComputer_;
97 std::unique_ptr<attribute_computer_t> maxAttributeComputer_;
99 std::unique_ptr<DualMinMaxTreeIncrementalFilter<T>> adjust_;
101 std::vector<double> minAttributeBuffer_;
103 std::vector<double> maxAttributeBuffer_;
105 std::vector<NodeId> pruneCandidateQueue_;
107 std::vector<NodeId> selectedPruneCandidates_;
109 CasfComponentTreesAttribute attribute_ = CasfComponentTreesAttribute::Area;
119 static std::unique_ptr<attribute_computer_t> makeAttributeComputer(CasfComponentTreesAttribute
attribute) {
121 case CasfComponentTreesAttribute::Area:
122 return std::make_unique<DynamicAreaAttributeComputer<T>>();
123 case CasfComponentTreesAttribute::BoundingBoxWidth:
124 return std::make_unique<DynamicBoundingBoxAttributeComputer<T>>(BoundingBoxMeasure::Width);
125 case CasfComponentTreesAttribute::BoundingBoxHeight:
126 return std::make_unique<DynamicBoundingBoxAttributeComputer<T>>(BoundingBoxMeasure::Height);
127 case CasfComponentTreesAttribute::BoundingBoxDiagonal:
128 return std::make_unique<DynamicBoundingBoxAttributeComputer<T>>(BoundingBoxMeasure::DiagonalLength);
130 throw std::runtime_error(
"Unknown CASF component-tree attribute.");
147 const std::vector<NodeId>& selectPruneCandidates(
const tree_t& tree,
const std::vector<double>&
attribute,
double threshold) {
148 pruneCandidateQueue_.clear();
149 selectedPruneCandidates_.clear();
154 return selectedPruneCandidates_;
157 pruneCandidateQueue_.push_back(root);
160 while (head < pruneCandidateQueue_.size()) {
161 const NodeId nodeId = pruneCandidateQueue_[head++];
162 if (!topology.
isAlive(nodeId)) {
166 const double nodeAttribute =
attribute[
static_cast<size_t>(nodeId)];
167 if (!topology.
isRoot(nodeId) && nodeAttribute <= threshold) {
168 selectedPruneCandidates_.push_back(nodeId);
173 if (topology.
isAlive(childId)) {
174 pruneCandidateQueue_.push_back(childId);
179 return selectedPruneCandidates_;
192 void applyFilterStep(
double threshold) {
193 const std::vector<NodeId> maxCandidates = selectPruneCandidates(maxTree_, maxAttributeBuffer_, threshold);
194 adjust_->pruneMaxTreeAndUpdateMinTree(maxCandidates);
196 const std::vector<NodeId> minCandidates = selectPruneCandidates(minTree_, minAttributeBuffer_, threshold);
197 adjust_->pruneMinTreeAndUpdateMaxTree(minCandidates);
208 : adjacency_(image ? image->getNumRows() : 0, image ? image->getNumColumns() : 0, radius),
210 minAttributeComputer_(makeAttributeComputer(
attribute)), maxAttributeComputer_(makeAttributeComputer(
attribute)), attribute_(
attribute) {
211 if (!image || image->getNumRows() <= 0 || image->getNumColumns() <= 0 || image->getSize() <= 0) {
212 throw std::invalid_argument(
"CasfComponentTrees requires a non-empty image.");
216 adjust_->
setAttributeComputer(*minAttributeComputer_, *maxAttributeComputer_, minAttributeBuffer_, maxAttributeBuffer_);
217 minAttributeComputer_->computeAttribute(minTree_, minAttributeBuffer_);
218 maxAttributeComputer_->computeAttribute(maxTree_, maxAttributeBuffer_);
220 const size_t maxNodes =
static_cast<size_t>(std::max(minTree_.topology().numInternalNodeSlots(), maxTree_.topology().numInternalNodeSlots()));
221 pruneCandidateQueue_.reserve(maxNodes);
222 selectedPruneCandidates_.reserve(maxNodes);
235 [[nodiscard]] image_ptr_t
filter(
const std::vector<double>& thresholds) {
236 for (
double threshold : thresholds) {
237 applyFilterStep(threshold);
239 return minTree_.reconstructFromNodeAltitudes();
247 [[nodiscard]]
const tree_t&
minTree() const noexcept {
return minTree_; }
254 [[nodiscard]]
const tree_t&
maxTree() const noexcept {
return maxTree_; }
261 [[nodiscard]] CasfComponentTreesAttribute
attribute() const noexcept {
return attribute_; }
271 [[nodiscard]] std::pair<std::vector<NodeId>, std::vector<T>>
exportMinTree()
const {
return minTree_.exportHigraHierarchy(); }
281 [[nodiscard]] std::pair<std::vector<NodeId>, std::vector<T>>
exportMaxTree()
const {
return maxTree_.exportHigraHierarchy(); }
void setAttributeComputer(const attribute_computer_t &computerMin, const attribute_computer_t &computerMax, std::vector< double > &bufferMin, std::vector< double > &bufferMax)
Registers incremental attribute computers and their external buffers.