Skip to content

ValueError when computing statistics on ROI extending beyond image boundaries #1

@PierreRaybaut

Description

@PierreRaybaut

GitHub Issue: ROI Statistics with Out-of-Bounds ROI

Title

Fix ValueError when computing statistics on ROI extending beyond image boundaries

Labels

  • bug
  • image processing
  • ROI

Description

Problem

When computing statistics (or any feature) on an image with a ROI that extends beyond the canvas boundaries, a ValueError is raised:

ValueError: zero-size array to reduction operation minimum which has no identity

Steps to Reproduce

  1. Create an image (e.g., 100x100 pixels)
  2. Define a ROI that extends beyond the image canvas (e.g., rectangle at x=80, y=80 with width=30, height=30, extending to x=110, y=110)
  3. Compute statistics on the image with this ROI
  4. Error is raised during statistics computation

Root Cause

The ImageObj.get_data() method did not clip ROI bounding box coordinates to image boundaries. When a ROI extended beyond the canvas, the array slice could result in an empty array (zero-size), causing numpy masked array functions to fail.

Solution

Modified ImageObj.get_data() method in sigima/objects/image/object.py to:

  • Clip bounding box coordinates to image boundaries using max(0, ...) and min(..., shape)
  • Handle completely out-of-bounds ROIs by returning a fully masked 1x1 array (containing NaN)
  • Properly handle partial overlap cases

Test Cases

Three scenarios tested:

  1. ROI partially extending beyond canvas (right/bottom) - ✅ Works
  2. ROI completely outside canvas - ✅ Returns NaN values
  3. ROI with negative coordinates (extends beyond top/left) - ✅ Works

Files Changed

  • sigima/objects/image/object.py - Fixed ImageObj.get_data() method
  • CHANGELOG.md - Documented the fix

Backward Compatibility

This fix maintains backward compatibility. Existing code will continue to work, and ROIs that were previously within bounds behave identically.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions