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

1#!/usr/bin/env python 

2# vim: set fileencoding=utf-8 : 

3 

4"""Base utilities for post-filtering vein images""" 

5 

6 

7class Filter(object): 

8 """Objects of this class filter the input image""" 

9 

10 def __init__(self): 

11 pass 

12 

13 def __call__(self, image, mask): 

14 """Inputs image and mask and outputs a filtered version of the image 

15 

16 

17 Parameters: 

18 

19 image (numpy.ndarray): raw image to filter as 2D array of unsigned 

20 8-bit integers 

21 

22 mask (numpy.ndarray): mask to normalize as 2D array of booleans 

23 

24 

25 Returns: 

26 

27 numpy.ndarray: A 2D boolean array with the same shape and data type of 

28 the input image representing the filtered image. 

29 

30 """ 

31 

32 raise NotImplementedError("You must implement the __call__ slot") 

33 

34 

35class NoFilter(Filter): 

36 """Applies no filtering on the input image, returning it without changes""" 

37 

38 def __init__(self): 

39 pass 

40 

41 def __call__(self, image, mask): 

42 """Inputs image and mask and outputs the image, without changes 

43 

44 

45 Parameters: 

46 

47 image (numpy.ndarray): raw image to filter as 2D array of unsigned 

48 8-bit integers 

49 

50 mask (numpy.ndarray): mask to normalize as 2D array of booleans 

51 

52 

53 Returns: 

54 

55 numpy.ndarray: A 2D boolean array with the same shape and data type of 

56 the input image representing the filtered image. 

57 

58 """ 

59 

60 return image 

61 

62 

63class HistogramEqualization(Filter): 

64 """Applies histogram equalization on the input image inside the mask. 

65 

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 """ 

71 

72 def __init__(self): 

73 pass 

74 

75 def __call__(self, image, mask): 

76 """Applies histogram equalization on the input image, returns filtered 

77 

78 

79 Parameters: 

80 

81 image (numpy.ndarray): raw image to filter as 2D array of unsigned 

82 8-bit integers 

83 

84 mask (numpy.ndarray): mask to normalize as 2D array of booleans 

85 

86 

87 Returns: 

88 

89 numpy.ndarray: A 2D boolean array with the same shape and data type of 

90 the input image representing the filtered image. 

91 

92 """ 

93 

94 from skimage.exposure import equalize_hist, rescale_intensity 

95 

96 retval = rescale_intensity( 

97 equalize_hist(image, mask=mask), out_range=(0, 255) 

98 ) 

99 

100 # make the parts outside the mask totally black 

101 retval[~mask] = 0 

102 

103 return retval