-
Notifications
You must be signed in to change notification settings - Fork 1
Description
name: General Issue
about: Feature Request
title: Enforce valid closed polygons for freehand ROI selection (prevent self-intersections)
labels: enhancement, roi, geometry
assignees: Anirudh Kannan
Issue Type
- Bug Report
- Feature Request
- Installation Issue
- Question/Help
- Documentation
- Other
Description
The current freehand ROI selection tool allows the user to hold the mouse button and trace a region, which is then auto-closed to the start position unconditionally on mouse release.
This behavior allows:
- Self-intersecting polygons
- Accidental closure when the end point is far from the start
- Invalid ROIs that are geometrically ill-defined
These ROIs can silently propagate errors into downstream processing (mask generation, statistics, measurements).
This issue proposes enforcing valid simple polygons for freehand ROIs.
Environment (for bugs/installation issues)
- OS: Linux (Ubuntu)
- Python Version: 3.x
- Package Version: current main
- Installation Method: source
Steps to Reproduce (for bugs)
- Open a 2D image
- Select freehand ROI tool
- Draw a shape that crosses over itself or release the mouse far from the start point
- Observe that the ROI is automatically closed and accepted
Expected vs Actual Behavior (for bugs)
Expected:
- ROI is only closed when the end point is sufficiently close to the start point
- Self-intersecting ROIs are rejected or flagged
- Only valid simple polygons are accepted
Actual:
- ROI is always auto-closed
- Self-intersections are allowed
- Invalid ROIs are silently accepted
Error Messages/Logs (if applicable)
N/A
Proposed Implementation (Python / PyQt)
Implement a standard freehand-lasso polygon validation pipeline:
-
Stroke capture
- Sample mouse positions while button is held
- Drop points closer than ε pixels to reduce jitter
-
Polyline simplification
- Apply Ramer–Douglas–Peucker (RDP) with tolerance τ
-
Explicit closure check
- Only close polygon if:
$$\text{distance(start, end)} \leq \text{closure threshold}$$ - Otherwise reject or prompt the user
- Only close polygon if:
-
Polygon validation
- Minimum vertex count ≥ 3
- No self-intersections (segment–segment intersection test)
- Area ≥ minimum threshold
-
Acceptance
- Normalize winding order (CW/CCW)
- Convert to mask or vector ROI only after validation
Acceptance Criteria
- Self-intersecting ROIs cannot be created silently
- ROI closure requires explicit user intent (proximity-based)
- All accepted ROIs are valid simple polygons
- Concave shapes are preserved (no convex hull)
Additional Context
This behavior matches standard lasso tools used in ImageJ, ITK-SNAP, GIS editors, and medical imaging software. The goal is correctness and predictability rather than shape approximation.
Optional future enhancements:
- Visual preview of closing edge
- Highlight invalid segments during drawing
- Undo last segment while drawing