Skip to content

Bugfix: Resizing masks containing over 65,535 cells not supported by OpenCV#938

Merged
carsen-stringer merged 2 commits intoMouseLand:mainfrom
dpeerlab:issue-937
Sep 7, 2024
Merged

Bugfix: Resizing masks containing over 65,535 cells not supported by OpenCV#938
carsen-stringer merged 2 commits intoMouseLand:mainfrom
dpeerlab:issue-937

Conversation

@Tobiaspk
Copy link
Copy Markdown
Contributor

Related to issue #937

When Cellpose detect over 65,535 it casts the masks vector to uint32. However, this format is not natively supported by OpenCV, causing its resize function to crash. See issue #937 for more information.

This PR proposes to use float32 instead, which is supported by OpenCV, and includes the following changes

  • If the datatype is uint32, first cast to float32, resize using cv2, round and cast resulting matrix to uint32.
  • Throw a warning if casting to float32 happens
  • Adding tests to confirm that previous and new function work as expected

This PR also considers three implications:

  • Time complexity: Casting datatypes is driving the increase in time complexity, causing a 5x to 30x increase. However, in this case it is almost neglectable. A 10,000 x 10,000 image on 32 cores then requires 0.47s compared to 0.02s.
  • Memory requirements: Float32 will increase memory requirements. The implications are not yet tested.
  • Numerical stability: Multiple tests have shown that uint16 and the proposed approach which casts to float32 yield identical results.

Reproducible examples

import cv2
import numpy as np

img16 = np.random.randint(0, 65535, size=(1000, 1000, 3)).astype("uint16")
img32 = img16.astype("uint32")

# UINT16: Succeeds
img16r = cv2.resize(img16, (300, 600))

# UINT32: Fails
img32r = cv2.resize(img32, (300, 600))

# FLOAT32: Succeeds (proposed approach)
img32r = cv2.resize(img32.astype("float32"), (300, 600)).round().astype("uint32")

# Identity
(img16r == img32r).mean()

@carsen-stringer carsen-stringer requested a review from mrariden May 19, 2024 13:12
@codecov
Copy link
Copy Markdown

codecov bot commented May 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 56.11%. Comparing base (21789ec) to head (1feef06).
Report is 65 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #938      +/-   ##
==========================================
+ Coverage   56.03%   56.11%   +0.07%     
==========================================
  Files          17       17              
  Lines        3876     3885       +9     
==========================================
+ Hits         2172     2180       +8     
- Misses       1704     1705       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@carsen-stringer
Copy link
Copy Markdown
Member

thank you @Tobiaspk !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants