Skip to content

Commit

Permalink
Merge pull request #29 from haesleinhuepf/fix_rgb
Browse files Browse the repository at this point in the history
Fix rgb
  • Loading branch information
haesleinhuepf authored Oct 19, 2023
2 parents 98b5c5b + 5f238e9 commit 60c8d20
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 100 deletions.
252 changes: 159 additions & 93 deletions docs/demo.ipynb

Large diffs are not rendered by default.

Binary file added docs/images/biapol.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
203 changes: 203 additions & 0 deletions docs/rgb_images.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stackview/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.6.4"
__version__ = "0.7.0"

from ._static_view import jupyter_displayable_output, insight
from ._utilities import merge_rgb
Expand Down
6 changes: 3 additions & 3 deletions stackview/_image_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ImageWidget(Canvas):
def __init__(self, image, zoom_factor:float=1.0, zoom_spline_order:int=0, colormap:str=None, display_min:float=None, display_max:float=None):
if not ((len(image.shape) == 2) or (len(image.shape) == 3 and image.shape[-1] == 3)):
if not ((len(image.shape) == 2) or (len(image.shape) in [3, 4] and image.shape[-1] == 3)):
raise NotImplementedError("Only 2D images are supported" + str(image.shape))
height = image.shape[0] * zoom_factor
width = image.shape[1] * zoom_factor
Expand Down Expand Up @@ -43,7 +43,7 @@ def _update_image(self):
self.put_image_data(_img_to_rgb(zoomed, colormap=self.colormap, display_min=self.display_min, display_max=self.display_max), 0, 0)

def _zoom(self, data):
if len(data.shape) == 3:
if len(data.shape) > 2 and data.shape[-1] == 3:
# handle RGB images
return np.asarray([self._zoom(data[:,:,i]) for i in range(data.shape[2])]).swapaxes(0, 2).swapaxes(1, 0)

Expand Down Expand Up @@ -72,7 +72,7 @@ def _img_to_rgb(image,
display_max=None):
from ._colormaps import _labels_lut, create_colormap

if len(image.shape) == 3 and image.shape[2] == 3:
if len(image.shape) > 2 and image.shape[-1] == 3:
return image

if image.dtype == bool:
Expand Down
7 changes: 6 additions & 1 deletion stackview/_slice_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self,
if slice_number is None:
slice_number = int(image.shape[axis] / 2)

if len(self.image.shape) == 3 and self.image.shape[-1] != 3:
if len(self.image.shape) > 2: # and self.image.shape[-1] != 3:
sliced_image = np.take(image, slice_number, axis=axis)
else:
sliced_image = image
Expand All @@ -49,6 +49,11 @@ def configuration_updated(event=None):
if len(self.image.shape) == 3 and self.image.shape[-1] != 3:
self.slice_slider.layout.display = None
self.view.data = np.take(self.image, self.slice_slider.value, axis=axis)
elif len(self.image.shape) == 4 and self.image.shape[-1] == 3:
self.slice_slider.layout.display = None
self.view.data = np.take(self.image, self.slice_slider.value, axis=axis)
elif len(self.image.shape) == 4:
raise NotImplementedError("Only 2D and 3D images are supported" + str(image.shape))
else:
self.view.data = self.image
self.slice_slider.layout.display = 'none'
Expand Down
8 changes: 8 additions & 0 deletions stackview/_static_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ def _imshow(image, title: str = None, labels: bool = False, min_display_intensit
"""
import numpy as np

if len(image.shape) == 3 and image.shape[2] == 3: # RGB image
import matplotlib.pyplot as plt
plt.imshow(image, vmin=min_display_intensity, vmax=max_display_intensity,
interpolation='nearest', alpha=alpha)
if not continue_drawing:
plt.show()
return

if len(image.shape) == 3:
image = np.asarray(image).max(axis=0)

Expand Down
8 changes: 6 additions & 2 deletions stackview/_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ def switch(images,

if toggleable:
def display_(buttons, images, colormap, display_min, display_max):
display_image = np.zeros([images[0].shape[0], images[0].shape[1], 3])
display_image = np.zeros(list(images[0].shape) + [3])
for button, image, colormap_, display_min_, display_max_ in zip(buttons, images, colormap,
display_min, display_max):
if button.value:
display_image_to_add = _img_to_rgb(image, display_min=display_min_, display_max=display_max_, colormap=colormap_)
if len(image.shape) == 3 and image.shape[-1] != 3:
display_image_to_add = np.asarray([_img_to_rgb(i, display_min=display_min_, display_max=display_max_, colormap=colormap_) for i in image])
else:
display_image_to_add = _img_to_rgb(image, display_min=display_min_, display_max=display_max_, colormap=colormap_)

if display_image is None:
display_image = display_image_to_add
else:
Expand Down

0 comments on commit 60c8d20

Please sign in to comment.