From 3738c41a0e96f677dbe3338a3d8709c2858340f8 Mon Sep 17 00:00:00 2001 From: Joost de Folter Date: Wed, 17 Apr 2024 13:28:22 +0200 Subject: [PATCH] improved image rendering, refactored conversion functions --- OmeSliCC/OmeSource.py | 30 +++++++++++++++------------ OmeSliCC/image_util.py | 47 ++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/OmeSliCC/OmeSource.py b/OmeSliCC/OmeSource.py index 776e2de..2415bda 100644 --- a/OmeSliCC/OmeSource.py +++ b/OmeSliCC/OmeSource.py @@ -261,25 +261,25 @@ def render(self, image: np.ndarray, source_dimension_order: str = None, t: int = if source_dimension_order is None: source_dimension_order = self.get_dimension_order() image = redimension_data(image, source_dimension_order, 'yxc', t=t, z=z) - new_image = np.zeros(list(image.shape[:2]) + [3], dtype=np.float32) - tot_alpha = 0 + total_image = None n = len(self.channels) - is_rgb = (self.get_nchannels() in (3, 4) and (n <= 1 or n == 3)) - do_normalisation = (image.dtype.itemsize == 2) + needs_normalisation = (image.dtype.itemsize == 2) if not is_rgb: + tot_alpha = 0 for channeli, channel in enumerate(self.channels): if not channels or channeli in channels: if n == 1: channel_values = image else: channel_values = image[..., channeli] - if do_normalisation: + if needs_normalisation: window = self.get_channel_window(channeli) channel_values = normalise_values(channel_values, window['min'], window['max']) else: channel_values = int2float_image(channel_values) + new_channel_image = np.atleast_3d(channel_values) color = channel.get('color') if color: rgba = color @@ -289,17 +289,21 @@ def render(self, image: np.ndarray, source_dimension_order: str = None, t: int = alpha = rgba[3] if alpha == 0: alpha = 1 - alpha_color = np.multiply(color, alpha).astype(np.float32) - new_image = new_image + np.atleast_3d(channel_values) * alpha_color + new_channel_image = new_channel_image * np.multiply(color, alpha).astype(np.float32) + if total_image is None: + total_image = new_channel_image + else: + total_image += new_channel_image tot_alpha += alpha - new_image = float2int_image(new_image / tot_alpha) - elif do_normalisation: + if tot_alpha != 1: + total_image /= tot_alpha + final_image = float2int_image(total_image) + elif needs_normalisation: window = self.get_channel_window(0) - new_image = float2int_image(normalise_values(image, window['min'], window['max'])) + final_image = float2int_image(normalise_values(image, window['min'], window['max'])) else: - new_image = image - - return new_image + final_image = image + return final_image def asarray(self, pixel_size: list = [], **slicing) -> np.ndarray: # expects x0, x1, y0, y1, ... diff --git a/OmeSliCC/image_util.py b/OmeSliCC/image_util.py index 5d5e8b8..2b66d0f 100644 --- a/OmeSliCC/image_util.py +++ b/OmeSliCC/image_util.py @@ -37,18 +37,20 @@ def show_image_gray(image: np.ndarray): plt.show() -def int2float_image(image: np.ndarray) -> np.ndarray: - if image.dtype.kind != 'f': - maxval = 2 ** (8 * image.dtype.itemsize) - 1 +def int2float_image(image): + source_dtype = image.dtype + if not source_dtype.kind == 'f': + maxval = 2 ** (8 * source_dtype.itemsize) - 1 return image / np.float32(maxval) else: return image -def float2int_image(image: np.ndarray, dtype: np.dtype = np.dtype(np.uint8)) -> np.ndarray: - if not (image.dtype.kind == 'i' or image.dtype.kind == 'u') and not dtype.kind == 'f': - maxval = 2 ** (8 * dtype.itemsize) - 1 - return (image * maxval).astype(dtype) +def float2int_image(image, target_dtype=np.dtype(np.uint8)): + source_dtype = image.dtype + if source_dtype.kind not in ('i', 'u') and not target_dtype.kind == 'f': + maxval = 2 ** (8 * target_dtype.itemsize) - 1 + return (image * maxval).astype(target_dtype) else: return image @@ -60,28 +62,29 @@ def ensure_unsigned_type(dtype: np.dtype) -> np.dtype: return new_dtype -def ensure_unsigned_image(image0: np.ndarray) -> np.ndarray: - dtype0 = image0.dtype - dtype = ensure_unsigned_type(dtype0) - if dtype != dtype0: +def ensure_unsigned_image(image: np.ndarray) -> np.ndarray: + source_dtype = image.dtype + dtype = ensure_unsigned_type(source_dtype) + if dtype != source_dtype: # conversion without overhead offset = 2 ** (8 * dtype.itemsize - 1) - image = image0.astype(dtype) + offset + new_image = image.astype(dtype) + offset else: - image = image0 - return image + new_image = image + return new_image -def convert_image_sign_type(image0: np.ndarray, dtype: np.dtype) -> np.ndarray: - if image0.dtype.kind == dtype.kind: - image = image0 - elif image0.dtype.kind == 'i': - image = ensure_unsigned_image(image0) +def convert_image_sign_type(image: np.ndarray, target_dtype: np.dtype) -> np.ndarray: + source_dtype = image.dtype + if source_dtype.kind == target_dtype.kind: + new_image = image + elif source_dtype.kind == 'i': + new_image = ensure_unsigned_image(image) else: # conversion without overhead - offset = 2 ** (8 * dtype.itemsize - 1) - image = (image0 - offset).astype(dtype) - return image + offset = 2 ** (8 * target_dtype.itemsize - 1) + new_image = (image - offset).astype(target_dtype) + return new_image def redimension_data(data, old_order, new_order, **indices):