Skip to content

Instantly share code, notes, and snippets.

@luistelmocosta
Created December 2, 2019 14:29
Show Gist options
  • Save luistelmocosta/f8c86b0008f1e8f668617f66e9dccd24 to your computer and use it in GitHub Desktop.
Save luistelmocosta/f8c86b0008f1e8f668617f66e9dccd24 to your computer and use it in GitHub Desktop.
def filter_rgb_to_hed(np_img, output_type="uint8"):
"""
Filter RGB channels to HED (Hematoxylin - Eosin - Diaminobenzidine) channels.
Args:
np_img: RGB image as a NumPy array.
output_type: Type of array to return (float or uint8).
Returns:
NumPy array (float or uint8) with HED channels.
"""
#t = Time()
hed = sk_color.rgb2hed(np_img)
if output_type == "float":
hed = sk_exposure.rescale_intensity(hed, out_range=(0.0, 1.0))
else:
hed = (sk_exposure.rescale_intensity(
hed, out_range=(0, 255))).astype("uint8")
#util.np_info(hed, "RGB to HED", t.elapsed())
return hed
def filter_hed_to_hematoxylin(np_img, output_type="uint8"):
"""
Obtain Hematoxylin channel from HED NumPy array and rescale it (for example, to 0 to 255 for uint8) for increased
contrast.
Args:
np_img: HED image as a NumPy array.
output_type: Type of array to return (float or uint8).
Returns:
NumPy array for Hematoxylin channel.
"""
#t = Time()
hema = np_img[:, :, 0]
if output_type == "float":
hema = sk_exposure.rescale_intensity(hema, out_range=(0.0, 1.0))
else:
hema = (sk_exposure.rescale_intensity(
hema, out_range=(0, 255))).astype("uint8")
#util.np_info(hema, "HED to Hematoxylin", t.elapsed())
return hema
def np_to_pil(np_img):
"""
Convert a NumPy array to a PIL Image.
Args:
np_img: The image represented as a NumPy array.
Returns:
The NumPy array converted to a PIL Image.
"""
if np_img.dtype == "bool":
np_img = np_img.astype("uint8") * 255
elif np_img.dtype == "float64":
np_img = (np_img * 255).astype("uint8")
return Image.fromarray(np_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment