Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

import cv2 

 

 

class BlurManager: 

""" 

The blur is responsible for applying different blur techniques to the images passed. 

""" 

def __init__(self, blur_type, kernel_size): 

""" 

Initialise Blur Manager. 

Authors(s): 

Nicolai van Niekerk, Stephan Nell 

Args: 

blur_type (str): Indicates the type of blur operation that should be applied to the image. 

kernel_size (integer tuple): Indicates the kernel size for blurring operations. 

Raises: 

TypeError: If a none string value is passed for blur_type 

Returns: 

None 

 

""" 

if not isinstance(blur_type, str): 

raise TypeError( 

'Bad type for arg blur_type - expected string. Received type "%s".' % 

type(blur_type).__name__ 

) 

 

self.blur_type = blur_type 

self.kernel_size = kernel_size 

 

def apply(self, image): 

""" 

This performs the blurring. 

Author(s): 

Nicolai van Niekerk, Stephan Nell 

Args: 

image: The image to be blurred. 

Raises: 

NameError: If invalid blur type is provided i.e. Normal, Gaussian or Median. 

Returns: 

obj:'OpenCV image': The blurred image. 

 

""" 

if self.blur_type == "gaussian": 

return self.gaussianBlur(image, self.kernel_size) 

elif self.blur_type == "normal": 

return self.blur(image, self.kernel_size) 

elif self.blur_type == "median": 

return self.medianBlur(image, self.kernel_size) 

else: 

raise NameError('Invalid Blur Selection! Try "normal", "gaussian" or "median" thresholding types.') 

 

def blur(self, image, blur_kernel=[(3, 3)]): 

""" 

This function applies basic blurring to the image passed. 

Author(s): 

Stephan Nell 

Args: 

image (:obj:'OpenCV image'): Image to which basic blurring should be applied to. 

blur_kernel (Integer list): Represent the kernel dimension by which basic blurring should be applied to. 

Integer list: Represent the kernel dimension by which basic blurring should be applied to. 

Raises: 

ValueError: If a blur_kernel with an invalid length is provided. 

TypeError: If a blur_kernel is not of type list. 

Returns: 

obj:'OpenCV image': A modified copy of the image where basic blurring was applied to the image. 

""" 

if not (isinstance(blur_kernel, list)): 

raise TypeError('Invalid kernel type provided for normal blurring. Blur kernel supports list type') 

if not len(blur_kernel[0]) == 2: 

raise ValueError('Invalid kernel size - blur_kernel list can only contain 2 items.') 

for (kX, kY) in blur_kernel: 

blurred = cv2.blur(image, (kX, kY)) 

return blurred 

 

def gaussianBlur(self, image, blur_kernel=[(7, 7)]): 

""" 

This function applies Gaussian blurring to the image passed. 

Author(s): 

Stephan Nell 

Args: 

image (:obj:'OpenCV image'): Image to which Gaussian blurring should be applied to. 

blur_kernel (Integer list): Represent the kernel dimension by which basic blurring should be applied to. 

Integer list: Represent the kernel dimension by which basic blurring should be applied to. 

Raises: 

ValueError: If a blur_kernel with an invalid length is provided. 

TypeError: If a blur_kernel is not of type list. 

Returns: 

obj:'OpenCV image': A modified copy of the image where Gaussian blurring was applied to the image. 

""" 

if not (isinstance(blur_kernel, list)): 

raise TypeError('Invalid kernel type provided for gaussian blur. Blur kernel supports list type') 

if not len(blur_kernel[0]) == 2: 

raise ValueError('Invalid kernel size - blur_kernel list can only contain 2 items.') 

for (kX, kY) in blur_kernel: 

blurred = cv2.GaussianBlur(image, (kX, kY), 0) 

return blurred 

 

def medianBlur(self, image, blur_kernel=[3]): 

""" 

This function applies Median blurring to the image passed. 

Author(s): 

Stephan Nell 

Args: 

image (:obj:'OpenCV image'): Image to which Median blurring should be applied to. 

blur_kernel (Integer array): Represent the kernel dimension by which median blurring should be applied to. 

Integer array: Represent the kernel dimension by which median blurring should be applied to. 

Raises: 

TypeError: If a blur_kernel is not of type list. 

ValueError: If a blur_kernel with an invalid length is provided. 

Returns: 

obj:'OpenCV image': A modified copy of the image where Median blurring was applied to the image. 

""" 

if not (isinstance(blur_kernel[0], int)): 

raise TypeError('Invalid kernel type provided for median blur. Blur kernel supports list type') 

if not len(blur_kernel) == 1: 

raise ValueError('Invalid kernel size only one integer value should be provided') 

for k in blur_kernel: 

blurred = cv2.medianBlur(image, k) 

return blurred