Skip to content

Smarter ROI Selection #12

@akannan05

Description

@akannan05

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)

  1. Open a 2D image
  2. Select freehand ROI tool
  3. Draw a shape that crosses over itself or release the mouse far from the start point
  4. 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:

  1. Stroke capture

    • Sample mouse positions while button is held
    • Drop points closer than ε pixels to reduce jitter
  2. Polyline simplification

  3. Explicit closure check

    • Only close polygon if: $$\text{distance(start, end)} \leq \text{closure threshold}$$
    • Otherwise reject or prompt the user
  4. Polygon validation

    • Minimum vertex count ≥ 3
    • No self-intersections (segment–segment intersection test)
    • Area ≥ minimum threshold
  5. 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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions