MouseLand / cellpose

a generalist algorithm for cellular segmentation with human-in-the-loop capabilities
https://www.cellpose.org/
BSD 3-Clause "New" or "Revised" License
1.24k stars 359 forks source link

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

Open Tobiaspk opened 1 month ago

Tobiaspk commented 1 month ago

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

This PR also considers three implications:

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[bot] commented 1 month ago

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 9 commits behind head on main.

Additional details and impacted files ```diff @@ 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.