Fixing pseudo rgb
This commit is contained in:
@@ -32,8 +32,10 @@ def apply_ultralytics_16bit_tiff_patches(*, force: bool = False) -> None:
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import tifffile
|
||||
|
||||
# import tifffile
|
||||
import torch
|
||||
from src.utils.image import Image
|
||||
|
||||
from ultralytics.utils import patches as ul_patches
|
||||
|
||||
@@ -55,7 +57,7 @@ def apply_ultralytics_16bit_tiff_patches(*, force: bool = False) -> None:
|
||||
|
||||
ext = os.path.splitext(filename)[1].lower()
|
||||
if ext in (".tif", ".tiff"):
|
||||
arr = tifffile.imread(filename)
|
||||
arr = Image(filename).get_qt_rgb()[:, :, :3]
|
||||
|
||||
# Normalize common shapes:
|
||||
# - (H, W) -> (H, W, 1)
|
||||
@@ -71,35 +73,6 @@ def apply_ultralytics_16bit_tiff_patches(*, force: bool = False) -> None:
|
||||
if arr.ndim == 2:
|
||||
arr = arr[..., None]
|
||||
|
||||
# Ultralytics expects BGR ordering when `channels=3`.
|
||||
# For grayscale data we replicate channels (no scaling, no quantization).
|
||||
if flags != cv2.IMREAD_GRAYSCALE:
|
||||
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)
|
||||
elif arr.shape[2] >= 3:
|
||||
arr = arr[:, :, :3]
|
||||
|
||||
# Ensure contiguous array for downstream OpenCV ops.
|
||||
logger.info(f"Loading with monkey-patched imread: {filename}")
|
||||
return np.ascontiguousarray(arr)
|
||||
|
||||
Reference in New Issue
Block a user