cellpose icon indicating copy to clipboard operation
cellpose copied to clipboard

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

Open Tobiaspk opened this issue 1 year ago • 1 comments

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()

Tobiaspk avatar May 14 '24 14:05 Tobiaspk

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.

codecov[bot] avatar May 21 '24 15:05 codecov[bot]

thank you @Tobiaspk !

carsen-stringer avatar Sep 07 '24 15:09 carsen-stringer