Skip to content

Commit

Permalink
use skimage for intensity resc., accept float im
Browse files Browse the repository at this point in the history
  • Loading branch information
guiwitz committed Oct 2, 2021
1 parent 0c70219 commit 090aacc
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions microfilm/colorify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import matplotlib
from matplotlib.colors import ListedColormap
from skimage.color import hsv2rgb
from skimage.exposure import rescale_intensity

def cmaps_def(cmap_name, num_colors=256, flip_map=False):
"""
Expand Down Expand Up @@ -229,30 +230,18 @@ def rescale_image(image, rescale_type='min_max', limits=None):
"""

if not np.issubdtype(image.dtype, np.unsignedinteger):
raise Exception(f"Image should be unsigned integer but yours is {image.dtype}")

max_of_dtype = np.iinfo(image.dtype).max
image = image.astype(np.float64)

if not np.any(image > 0):#blank image
image_rescaled = image
elif image.min() == image.max():#all pixels have same value
image_rescaled = np.ones_like(image)
elif rescale_type == 'min_max':
min_val = np.min(image)#np.min(image[image>0])
image_rescaled = (image-min_val)/(image.max()-min_val)
image_rescaled[image_rescaled<0] = 0
if image.min() == image.max():#all pixels have same value
image_rescaled = np.ones(image.shape, dtype=np.float64)
if rescale_type == 'min_max':
image_rescaled = rescale_intensity(image, in_range='image', out_range=(0,1))
elif rescale_type == 'dtype':
image_rescaled = image / max_of_dtype
image_rescaled = rescale_intensity(image, in_range='dtype', out_range=(0,1))
elif rescale_type == 'zero_max':
image_rescaled = image / image.max()
image_rescaled = rescale_intensity(image, in_range=(0, image.max()), out_range=(0,1))
elif rescale_type == 'limits':
if limits is None:
raise Exception(f"You need to provide explicit intensity limits of the form [min, max]")
image_rescaled = (image - limits[0]) / (limits[1] - limits[0])
image_rescaled[image_rescaled<0] = 0
image_rescaled[image_rescaled>1] = 1
image_rescaled = rescale_intensity(image, in_range=(limits[0], limits[1]), out_range=(0,1))

return image_rescaled

Expand Down

0 comments on commit 090aacc

Please sign in to comment.