Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/histolab/filters/image_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ class EosinChannel(ImageFilter):
Example:
>>> from PIL import Image
>>> from histolab.filters.image_filters import EosinChannel
>>> from histolab.filters import image_filters_functional as imf
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> eosin_channel = EosinChannel()
>>> image_e = eosin_channel(image_rgb)
Expand All @@ -340,6 +339,36 @@ def __call__(self, img):
return eosin


class DABChannel(ImageFilter):
"""Obtain DAB channel from RGB image.

Input image is first converted into HED space and the DAB channel is
rescaled for increased contrast.

Parameters
----------
img : Image.Image
Input RGB image

Returns
-------
Image.Image
Grayscale image corresponding to input image with DAB channel enhanced.


Example:
>>> from PIL import Image
>>> from histolab.filters.image_filters import DABChannel
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> dab_channel = DABChannel()
>>> image_d = dab_channel(image_rgb)
""" # noqa

def __call__(self, img):
dab = F.dab_channel(img)
return dab


class RgbToHsv(ImageFilter):
"""Convert RGB channels to HSV channels.

Expand Down
23 changes: 23 additions & 0 deletions src/histolab/filters/image_filters_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ def blue_pen_filter(img: PIL.Image.Image) -> PIL.Image.Image:
return apply_mask_image(img, blue_pen_filter_img)


def dab_channel(img: PIL.Image.Image) -> PIL.Image.Image:
"""Obtain DAB channel from RGB image.

Input image is first converted into HED space and the DAB channel is
rescaled for increased contrast.

Parameters
----------
img : Image.Image
Input RGB image

Returns
-------
Image.Image
Grayscale image corresponding to input image with DAB channel enhanced.
"""
if img.mode not in ["RGB", "RGBA"]:
raise ValueError("Input image must be RGB/RGBA.")
dab = np.array(rgb_to_hed(img))[:, :, 2]
dab = sk_exposure.rescale_intensity(dab)
return np_to_pil(dab)


def eosin_channel(img: PIL.Image.Image) -> PIL.Image.Image:
"""Obtain Eosin channel from RGB image.

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions tests/integration/test_image_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1679,3 +1679,42 @@ def test_rgb_to_lab_raises_exception_on_gs_and_rgba_image(pil_image):

assert isinstance(err.value, Exception)
assert str(err.value) == "Input image must be RGB"


def test_dab_channel_filter_with_rgb_image():
img = RGB.TCGA_LUNG_RGB
expected_value = load_expectation(
"pil-images-rgb/tcga-lung-rgb-dab-channel", type_="png"
)

dab_img = imf.dab_channel(img)

np.testing.assert_array_almost_equal(np.array(dab_img), np.array(expected_value))
assert np.unique(np.array(ImageChops.difference(dab_img, expected_value)))[0] == 0


def test_dab_channel_filter_with_rgba_image():
img = RGBA.TCGA_LUNG
expected_value = load_expectation(
"pil-images-rgba/tcga-lung-dab-channel", type_="png"
)
expected_warning_regex = (
r"Input image must be RGB. NOTE: the image will be converted to RGB before"
r" HED conversion."
)

with pytest.warns(UserWarning, match=expected_warning_regex):
dab_img = imf.dab_channel(img)

np.testing.assert_array_almost_equal(np.array(dab_img), np.array(expected_value))
assert np.unique(np.array(ImageChops.difference(dab_img, expected_value)))[0] == 0


def test_dab_channel_raises_exception_on_gs_image():
gs_img = GS.DIAGNOSTIC_SLIDE_THUMB_GS

with pytest.raises(ValueError) as err:
imf.dab_channel(gs_img)

assert isinstance(err.value, Exception)
assert str(err.value) == "Input image must be RGB/RGBA."
13 changes: 13 additions & 0 deletions tests/unit/filters/test_image_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,16 @@ def it_calls_rgb_to_lab_functional(self, request):

F_rgb_to_lab.assert_called_once_with(image)
assert type(rgb_to_lab(image)) == PIL.Image.Image

def it_calls_dab_channel_functional(self, request):
image = PILIMG.RGBA_COLOR_500X500_155_249_240
F_dab_channel = function_mock(
request, "histolab.filters.image_filters_functional.dab_channel"
)
F_dab_channel.return_value = image
dab_channel = imf.DABChannel()

dab_channel(image)

F_dab_channel.assert_called_once_with(image)
assert type(dab_channel(image)) == PIL.Image.Image