Appling pseudo channels for RGB

This commit is contained in:
2025-12-18 12:52:13 +02:00
parent 5e9d3b1dc4
commit 268ed5175e

View File

@@ -44,7 +44,7 @@ def apply_ultralytics_16bit_tiff_patches(*, force: bool = False) -> None:
_original_imread = ul_patches.imread _original_imread = ul_patches.imread
def tifffile_imread( def tifffile_imread(
filename: str, flags: int = cv2.IMREAD_COLOR filename: str, flags: int = cv2.IMREAD_COLOR, pseudo_rgb: bool = False
) -> Optional[np.ndarray]: ) -> Optional[np.ndarray]:
"""Replacement for [`ultralytics.utils.patches.imread()`](venv/lib/python3.12/site-packages/ultralytics/utils/patches.py:20). """Replacement for [`ultralytics.utils.patches.imread()`](venv/lib/python3.12/site-packages/ultralytics/utils/patches.py:20).
@@ -75,6 +75,27 @@ def apply_ultralytics_16bit_tiff_patches(*, force: bool = False) -> None:
# For grayscale data we replicate channels (no scaling, no quantization). # For grayscale data we replicate channels (no scaling, no quantization).
if flags != cv2.IMREAD_GRAYSCALE: if flags != cv2.IMREAD_GRAYSCALE:
if arr.shape[2] == 1: if arr.shape[2] == 1:
if pseudo_rgb:
gamma = 0.3
a1 = arr.copy().astype(np.float32)
a1 -= np.percentile(a1, 2)
a1[a1 < 0] = 0
p98 = np.percentile(a1, 98)
a1[a1 > p98] = p98
a1 /= a1.max()
a2 = a1.copy()
a2 = a2**gamma
a2 /= a2.max()
a3 = a1.copy()
p90 = np.percentile(a3, 90)
a3[a3 > p90] = p90
a3 /= a3.max()
arr = np.concatenate([a1, a2, a3], axis=2)
else:
arr = np.repeat(arr, 3, axis=2) arr = np.repeat(arr, 3, axis=2)
elif arr.shape[2] >= 3: elif arr.shape[2] >= 3:
arr = arr[:, :, :3] arr = arr[:, :, :3]