Enh: add IncrementalWatershedCut for interactive seeded watershed#299
Open
lapertor wants to merge 2 commits into
Open
Enh: add IncrementalWatershedCut for interactive seeded watershed#299lapertor wants to merge 2 commits into
lapertor wants to merge 2 commits into
Conversation
PerretB
requested changes
Apr 1, 2026
956d3c3 to
03054ba
Compare
PerretB
requested changes
Apr 2, 2026
| } | ||
|
|
||
| // Local relabeling: BFS from v in its component | ||
| relabel_component_from_seed(v, l); |
Member
There was a problem hiding this comment.
Calling relabel immediately after having found the new cut edge is probably suboptimal as it can lead to relabelling the same vertices several time in the same batch.
| } | ||
|
|
||
| // Local relabeling: find merged component and relabel from remaining seeds | ||
| relabel_merged_component(v); |
| * (BFS from v respecting current cuts), reset labels to 0, then | ||
| * relabel from all remaining seeds in the component. | ||
| */ | ||
| void relabel_merged_component(index_t v) { |
Member
There was a problem hiding this comment.
This is sub-optimal. When a seed is removed we also know the edge of the mst that is reactivated which can be used to determine the label of the "other side" (except if the other side is also removed in the same batch, in which case relabelling will start somewhere else).
| * The algorithm maintains a canonical BPT and a visitCount array to identify | ||
| * watershed edges without recomputing from scratch at each interaction. | ||
| * The labeling is cached and updated locally when seeds change. | ||
| * |
Member
There was a problem hiding this comment.
Please Add a short usage example.
Also add a reference to the class in the global doc
Add `incremental_watershed_cut` C++ class and `IncrementalWatershedCut` Python class that maintain a cached BPT to update seeded watershed cuts incrementally, avoiding a full recomputation on each seed change. Based on the algorithm described in: Q. Lebon, J. Lefevre, J. Cousty, B. Perret. 'Interactive Segmentation With Incremental Watershed Cuts', CIARP 2023. https://hal.science/hal-04069187v1 Signed-off-by: lapertor <raphael.lapertot@gmail.com>
…ions, optimize BFS - Fix visitCount batch removal bug: walk-up loop now always decrements visitCount and breaks on 2->1 transition (matching Lebon's removeMarker), instead of stopping on visitCount==1 without decrementing. - Add hg_assert in unreachable else-branch of Pass 2a (both sides of de-cut with different seed labels should not happen). - Remove unused #include <unordered_set>. - Optimize component_seed_label: replace per-call std::vector allocation with m_visited generation-counter pattern (zero-cost reset). - Add trailing newline to watershed.rst. - Clean test_watershed.py diff (73 insertions, no whitespace changes). - Add 3 regression tests: batch remove equals sequential, both sides of edge, and interactive churn. - Add C++ test for batch remove sibling subtrees visitCount. Signed-off-by: lapertor <raphael.lapertot@gmail.com>
03054ba to
b1430a7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #298
Motivation
labelisation_seeded_watershedrecomputes the full watershed from scratch on every seed change, which is expensive in interactive segmentation scenarios. This PR introducesIncrementalWatershedCut, a stateful class that maintains a cached Binary Partition Tree (BPT) and updates only the affected regions when seeds are added or removed.The implementation follows the algorithm described in:
Algorithm
The key data structures are:
bpt_canonicalvisitCountarray on BPT nodes: tracking how many seed paths pass through each internal nodeis_cutboolean array on MST edges: an MST edge is a watershed edge when its corresponding BPT node hasvisitCount >= 2When a seed is added, the algorithm walks up the BPT from the seed leaf, incrementing
visitCountat each node; when it reaches 2, the edge is marked as a cut. When a seed is removed, the walk decrementsvisitCount; when it drops to 1, the edge is unmarked. A subsequent BFS re-labels the graph. The BFS correctly handles both splits (add seed) and merges (remove seed), which is why a union-find structure is not used here.Changes
include/higra/algo/watershed.hpp: newincremental_watershed_cutclasshigra/algo/py_watershed.cpp: pybind11 binding forIncrementalWatershedCuthigra/algo/watershed.py: Python wrapper classIncrementalWatershedCuttest/cpp/algo/test_watershed.cpp: 5 new C++ teststest/python/test_algo/test_watershed.py: 10 new Python tests in classTestIncrementalWatershedUsage
Performance
Benchmarks on a 500x500 image (20 interactions, 5 seeds each):
labelisation_seeded_watershedIncrementalWatershedCut(update only)On 1000x1000: ~11x speedup per interaction. The speedup is stable across
interactions regardless of the number of accumulated seeds.
Consistency
test_consistency_with_seeded_watershedverifies thatIncrementalWatershedCutproduces the same labeling aslabelisation_seeded_watershedgiven identical seeds, ensuring backward compatibility.Benchmark script
Benchmark output (Windows, Intel Core i7-14700KF, Python 3.13.12)