Image Filters Functional

adaptive_equalization(img, nbins=256, clip_limit=0.01)[source]

Increase image contrast using adaptive equalization.

Contrast in local region of input image (gray or RGB) is increased using adaptive equalization

Parameters
  • img (PIL.Image.Image) – Input image (gray or RGB)

  • nbins (int) – Number of histogram bins. Default is 256.

  • clip_limit (float, optional) – Clipping limit where higher value increases contrast. Default is 0.01

Returns

image with contrast enhanced by adaptive equalization.

Return type

PIL.Image.Image

blue_filter(img, red_thresh, green_thresh, blue_thresh)[source]

Filter out blueish colors in an RGB image.

Create a mask to filter out blueish colors, where the mask is based on a pixel being above a red channel threshold value, above a green channel threshold value, and below a blue channel threshold value.

Parameters
  • img (PIL.Image.Image) – Input RGB image

  • red_thresh (int) – Red channel lower threshold value.

  • green_thresh (int) – Green channel lower threshold value.

  • blue_thresh (int) – Blue channel upper threshold value.

Returns

Boolean NumPy array representing the mask.

Return type

np.ndarray

blue_pen_filter(img)[source]

Filter out blue pen marks from a diagnostic slide.

The resulting mask is a composition of green filters with different thresholds for the RGB channels.

Parameters

img (PIL.Image.Image) – Input RGB image

Returns

Input image with the blue pen marks filtered out.

Return type

PIL.Image.Image

canny_edges(img, sigma=1.0, low_threshold=0.0, high_threshold=25.0)[source]

Filter image based on Canny edge algorithm.

Note that input image must be 2D grayscale image

Parameters
  • img (PIL.Image.Image) – Input 2-dimensional image

  • sigma (float, optional) – Width (std dev) of Gaussian. Default is 1.0.

  • low_threshold (float, optional) – Low hysteresis threshold value. Default is 0.0.

  • high_threshold (float, optional) – High hysteresis threshold value. Default is 25.0.

Returns

Boolean NumPy array representing Canny edge map.

Return type

np.ndarray

dab_channel(img)[source]

Obtain DAB channel from RGB image.

Input image is first converted into HED space and the DAB channel is extracted via color deconvolution.

Parameters

img (PIL.Image.Image) – Input RGB image

Returns

RGB image with DAB staining separated.

Return type

PIL.Image.Image

eosin_channel(img)[source]

Obtain Eosin channel from RGB image.

Input image is first converted into HED space and the Eosin channel is extracted via color deconvolution.

Parameters

img (PIL.Image.Image) – Input RGB image

Returns

RGB image with Eosin staining separated.

Return type

PIL.Image.Image

filter_entropy(img, neighborhood=9, threshold=5.0, relate=<built-in function gt>)[source]

Filter image based on entropy (complexity).

The area of the image included in the local neighborhood is defined by a square neighborhood x neighborhood

Note that input must be 2D.

Parameters
  • img (PIL.Image.Image) – input 2-dimensional image

  • neighborhood (int, optional) – Neighborhood size (defines height and width of 2D array of 1’s). Default is 9.

  • threshold (float, optional) – Threshold value. Default is 5.0

  • relate (callable operator, optional) – Operator to be used to compute the mask from the threshold. Default is operator.lt

Returns

NumPy boolean array where True represent a measure of complexity.

Return type

np.ndarray

grays(img, tolerance=15)[source]

Filter out gray pixels in RGB image.

Gray pixels are those pixels where the red, green, and blue channel values are similar, i.e. under a specified tolerance.

Parameters
  • img (PIL.Image.Image) – Input image

  • tolerance (int, optional) – if difference between values is below this threshold, values are considered similar and thus filtered out. Default is 15.

Returns

Mask image where the grays values are masked out

Return type

PIL.Image.Image

green_channel_filter(img, green_thresh=200, avoid_overmask=True, overmask_thresh=90.0)[source]

Mask pixels in an RGB image with G-channel greater than a specified threshold.

Create a mask to filter out pixels with a green channel value greater than a particular threshold, since hematoxylin and eosin are purplish and pinkish, which do not have much green to them.

Parameters
  • img (PIL.Image.Image) – Input RGB image

  • green_thresh (int, optional) – Green channel threshold value (0 to 255). Default is 200. If value is greater than green_thresh, mask out pixel.

  • avoid_overmask (bool, optional) – If True, avoid masking above the overmask_thresh percentage. Default is True.

  • overmask_thresh (float, optional) – If avoid_overmask is True, avoid masking above this percentage value. Default is 90.0

Returns

Boolean mask where pixels above a particular green channel threshold have been masked out.

Return type

np.ndarray

green_filter(img, red_thresh, green_thresh, blue_thresh)[source]

Filter out greenish colors in an RGB image. The mask is based on a pixel being above a red channel threshold value, below a green channel threshold value, and below a blue channel threshold value.

Note that for the green ink, the green and blue channels tend to track together, so for blue channel we use a lower threshold rather than an upper threshold value.

Parameters
  • img (PIL.Image.Image) – RGB input image.

  • red_thresh (int) – Red channel upper threshold value.

  • green_thresh (int) – Green channel lower threshold value.

  • blue_thresh (int) – Blue channel lower threshold value.

Returns

Boolean NumPy array representing the mask.

Return type

np.ndarray

green_pen_filter(img)[source]

Filter out green pen marks from a diagnostic slide.

The resulting mask is a composition of green filters with different thresholds for the RGB channels.

Parameters

img (PIL.Image.Image) – Input RGB image

Returns

Input image with the green pen marks filtered out.

Return type

PIL.Image.Image

hed_to_rgb(img_arr)[source]

Convert HED channels to RGB channels.

Parameters

img_arr (np.ndarray) – Array representation of the image in HED color space

Returns

Image in RGB space

Return type

PIL.Image.Image

hematoxylin_channel(img)[source]

Obtain Hematoxylin channel from RGB image.

Input image is first converted into HED space and the hematoxylin channel is extracted via color deconvolution.

Parameters

img (Image.Image) – Input RGB image

Returns

RGB image with Hematoxylin staining separated.

Return type

PIL.Image.Image

histogram_equalization(img, nbins=256)[source]

Increase image contrast using histogram equalization.

The input image (gray or RGB) is filterd using histogram equalization to increase contrast.

Parameters
  • img (PIL.Image.Image) – Input image.

  • nbins (int. optional) – Number of histogram bins. Default is 256.

Returns

Image with contrast enhanced by histogram equalization.

Return type

PIL.Image.Image

hysteresis_threshold(img, low=50, high=100)[source]

Apply two-level (hysteresis) threshold to an image.

Parameters
  • img (PIL.Image.Image) – Input image

  • low (int, optional) – low threshold. Default is 50.

  • high (int, optional) – high threshold. Default is 100.

Returns

Image with the hysteresis threshold applied

Return type

PIL.Image.Image

hysteresis_threshold_mask(img, low=50, high=100)[source]

Mask an image using hysteresis threshold

Compute the Hysteresis threshold on the complement of a grayscale image, and return boolean mask based on pixels above this threshold.

Parameters
  • img (PIL.Image.Image) – Input image.

  • low (int, optional) – low threshold. Default is 50.

  • high (int, optional) – high threshold. Default is 100.

Returns

Boolean NumPy array where True represents a pixel above Otsu threshold.

Return type

np.ndarray

invert(img)[source]

Invert an image, i.e. take the complement of the correspondent array.

Parameters

img (PIL.Image.Image) – Input image

Returns

Inverted image

Return type

PIL.Image.Image

kmeans_segmentation(img, n_segments=800, compactness=10.0)[source]

Segment an image with K-means segmentation

By using K-means segmentation (color/space proximity) each segment is colored based on the average color for that segment.

Parameters
  • img (PIL.Image.Image) – Input image

  • n_segments (int, optional) – The number of segments. Default is 800.

  • compactness (float, optional) – Color proximity versus space proximity factor. Default is 10.0.

Returns

RGB image where each segment has been colored based on the average color for that segment.

Return type

PIL.Image.Image

Raises

ValueError – If img mode is RGBA.

lab_to_rgb(img, illuminant='D65', observer='2')[source]

Lab to RGB color space conversion.

Parameters
  • img (PIL.Image.Image) – Input image in Lab space.

  • illuminant ({"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional) – The name of the illuminant (the function is NOT case sensitive). Default is “D65”.

  • observer ({"2", "10", "R"}, optional) – The aperture angle of the observer. Default is “2”.

Returns

Image in RGB space.

Return type

PIL.Image.Image

local_equalization(img, disk_size=50)[source]

Filter gray image using local equalization.

Local equalization method uses local histograms based on a disk structuring element.

Parameters
  • img (PIL.Image.Image) – Input image. Notice that it must be 2D

  • disk_size (int, optional) – Radius of the disk structuring element used for the local histograms. Default is 50.

Returns

2D image with contrast enhanced using local equalization.

Return type

PIL.Image.Image

local_otsu_threshold(img, disk_size=3.0)[source]

Mask image based on local Otsu threshold.

Compute local Otsu threshold for each pixel and return boolean mask based on pixels being less than the local Otsu threshold.

Note that the input image must be 2D.

Parameters
  • img (PIL.Image.Image) – Input 2-dimensional image

  • disk_size (float, optional) – Radius of the disk structuring element used to compute the Otsu threshold for each pixel. Default is 3.0.

Returns

Resulting image where local Otsu threshold values have been applied to original image.

Return type

PIL.Image.Image

otsu_threshold(img, relate=<built-in function lt>)[source]

Mask image based on pixel above Otsu threshold.

Compute Otsu threshold on image and return boolean mask based on pixels above this threshold.

Note that Otsu threshold is expected to work correctly only for grayscale images.

Parameters
  • img (PIL.Image.Image) – Input image.

  • relate (operator, optional) – Operator to be used to compute the mask from the threshold. Default is operator.lt

Returns

Boolean NumPy array where True represents a pixel above Otsu threshold.

Return type

np.ndarray

rag_threshold(img, n_segments=800, compactness=10.0, threshold=9, mask=None, return_labels=False)[source]

Combine similar K-means segmented regions based on threshold value.

Segment an image with K-means, build region adjacency graph based on the segments, combine similar regions based on threshold value, and then output these resulting region segments.

Parameters
  • img (PIL.Image.Image) – Input image

  • n_segments (int, optional) – The number of segments. Default is 800.

  • compactness (float, optional) – Color proximity versus space proximity factor. Default is 10.0.

  • threshold (int, optional) – Threshold value for combining regions. Default is 9.

  • mask (np.ndarray, optional) – If provided, superpixels are computed only where mask is True, and seed points are homogeneously distributed over the mask using a K-means clustering strategy (See skimage). Must be the same size as img.

  • return_labels (bool, optional) – If True, returns a labeled array where the value denotes segment membership. Otherwise, returns a PIL image where each segment is colored by the average color in it. Default is False.

Returns

  • PIL.Image.Image, if not return_labels – Each segment has been colored based on the average color for that segment (and similar segments have been combined).

  • np.ndarray, if return_labels – Value denotes segment membership.

Raises

ValueError – If img mode is RGBA.

Return type

Union[PIL.Image.Image, numpy.ndarray]

red_filter(img, red_thresh, green_thresh, blue_thresh)[source]

Mask reddish colors in an RGB image.

Create a mask to filter out reddish colors, where the mask is based on a pixel being above a red channel threshold value, below a green channel threshold value, and below a blue channel threshold value.

Parameters
  • img (PIL.Image.Image) – Input RGB image

  • red_thresh (int) – Red channel lower threshold value.

  • green_thresh (int) – Green channel upper threshold value.

  • blue_thresh (int) – Blue channel upper threshold value.

Returns

Boolean NumPy array representing the mask.

Return type

np.ndarray

red_pen_filter(img)[source]

Filter out red pen marks on diagnostic slides.

The resulting mask is a composition of red filters with different thresholds for the RGB channels.

Parameters

img (PIL.Image.Image) – Input RGB image.

Returns

Input image with the pen marks filtered out.

Return type

PIL.Image.Image

rgb_to_hed(img)[source]

Convert RGB channels to HED channels.

Image color space (RGB) is converted to Hematoxylin-Eosin-Diaminobenzidine space.

Parameters

img (PIL.Image.Image) – Input image

Returns

Array representation of the image in HED space

Return type

np.ndarray

rgb_to_hsv(img)[source]

Convert RGB channels to HSV channels.

Image color space (RGB) is converted to Hue - Saturation - Value (HSV) space.

Parameters

img (PIL.Image.Image) – Input image

Returns

Array representation of the image in HSV space

Return type

np.ndarray

Raises

Exception – If the image mode is not RGB

rgb_to_lab(img, illuminant='D65', observer='2')[source]

Convert from the sRGB color space to the CIE Lab colorspace.

sRGB color space reference: IEC 61966-2-1:1999

Parameters
  • img (PIL.Image.Image) – Input image

  • illuminant ({"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional) – The name of the illuminant (the function is NOT case sensitive). Default is “D65”.

  • observer ({"2", "10", "R"}, optional) – The aperture angle of the observer. Default is “2”.

Returns

Array representation of the image in LAB space

Return type

np.ndarray

Raises

Exception – If the image mode is not RGB

rgb_to_od(img)[source]

Convert from RGB to optical density (OD_RGB) space.

Parameters

img (PIL.Image.Image) – Input image

Returns

Array representation of the image in OD space

Return type

np.ndarray

stretch_contrast(img, low=40, high=60)[source]

Increase image contrast.

Th contrast in image is increased based on intensities in a specified range

Parameters
  • img (PIL.Image.Image) – Input image

  • low (int) – Range low value (0 to 255). Default is 40.

  • high (int) – Range high value (0 to 255). Default is 60.

Returns

Image with contrast enhanced.

Return type

PIL.Image.Image

yen_threshold(img, relate=<built-in function lt>)[source]

Mask image based on pixel below Yen’s threshold.

Compute Yen threshold on image and return boolean mask based on pixels below this threshold.

Parameters
  • img (PIL.Image.Image) – Input image.

  • relate (operator, optional) – Operator to be used to compute the mask from the threshold. Default is operator.lt

Returns

Boolean NumPy array where True represents a pixel below Yen’s threshold.

Return type

np.ndarray