Bugfix: Resizing masks containing over 65,535 cells not supported by OpenCV
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 tofloat32, resize usingcv2, round and cast resulting matrix touint32. - Throw a warning if casting to
float32happens - 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
uint16and the proposed approach which casts tofloat32yield 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()
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
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
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
thank you @Tobiaspk !