Coverage for src/bob/bio/vein/preprocessor/filters.py: 89%
19 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-06 21:57 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-06 21:57 +0100
1#!/usr/bin/env python
2# vim: set fileencoding=utf-8 :
4"""Base utilities for post-filtering vein images"""
7class Filter(object):
8 """Objects of this class filter the input image"""
10 def __init__(self):
11 pass
13 def __call__(self, image, mask):
14 """Inputs image and mask and outputs a filtered version of the image
17 Parameters:
19 image (numpy.ndarray): raw image to filter as 2D array of unsigned
20 8-bit integers
22 mask (numpy.ndarray): mask to normalize as 2D array of booleans
25 Returns:
27 numpy.ndarray: A 2D boolean array with the same shape and data type of
28 the input image representing the filtered image.
30 """
32 raise NotImplementedError("You must implement the __call__ slot")
35class NoFilter(Filter):
36 """Applies no filtering on the input image, returning it without changes"""
38 def __init__(self):
39 pass
41 def __call__(self, image, mask):
42 """Inputs image and mask and outputs the image, without changes
45 Parameters:
47 image (numpy.ndarray): raw image to filter as 2D array of unsigned
48 8-bit integers
50 mask (numpy.ndarray): mask to normalize as 2D array of booleans
53 Returns:
55 numpy.ndarray: A 2D boolean array with the same shape and data type of
56 the input image representing the filtered image.
58 """
60 return image
63class HistogramEqualization(Filter):
64 """Applies histogram equalization on the input image inside the mask.
66 In this implementation, only the pixels that lie inside the mask will be
67 used to calculate the histogram equalization parameters. Because of this
68 particularity, we don't use Bob's implementation for histogram equalization
69 and have one based exclusively on scikit-image.
70 """
72 def __init__(self):
73 pass
75 def __call__(self, image, mask):
76 """Applies histogram equalization on the input image, returns filtered
79 Parameters:
81 image (numpy.ndarray): raw image to filter as 2D array of unsigned
82 8-bit integers
84 mask (numpy.ndarray): mask to normalize as 2D array of booleans
87 Returns:
89 numpy.ndarray: A 2D boolean array with the same shape and data type of
90 the input image representing the filtered image.
92 """
94 from skimage.exposure import equalize_hist, rescale_intensity
96 retval = rescale_intensity(
97 equalize_hist(image, mask=mask), out_range=(0, 255)
98 )
100 # make the parts outside the mask totally black
101 retval[~mask] = 0
103 return retval