Skip to content

Commit

Permalink
improved image rendering, refactored conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
folterj committed Apr 17, 2024
1 parent 85a94c9 commit 3738c41
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
30 changes: 17 additions & 13 deletions OmeSliCC/OmeSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, ...
Expand Down
47 changes: 25 additions & 22 deletions OmeSliCC/image_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down

0 comments on commit 3738c41

Please sign in to comment.