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

import cv2 

import numpy as np 

 

 

class ThresholdingManager: 

""" 

The Thresholding manager is responsible for applying the different types of thresholding techniques. 

""" 

def __init__(self, thresholding_type): 

""" 

Initialise Thresholding manager. 

Authors(s): 

Nicolai van Niekerk, Stephan Nell 

Args: 

thresholding_type (str): Indicates the type of thresholding that 

should be applied. 

Raises: 

TypeError: If a parameter is passed that is not of type String. 

NameError: If the thresholding type is not Adaptive or Otsu. 

Returns: 

None 

 

""" 

if not isinstance(thresholding_type, str): 

raise TypeError( 

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

type(thresholding_type).__name__ 

) 

 

if thresholding_type == "adaptive": 

self.thresholding_type = thresholding_type 

elif thresholding_type == "otsu": 

self.thresholding_type = thresholding_type 

else: 

raise NameError('Invalid Thresholding Selection! Try "adaptive" or "otsu" thresholding types.') 

 

def apply(self, image): 

""" 

This performs the thresholding based on the predefined technique. 

Author(s): 

Nicolai van Niekerk, Stephan Nell 

Args: 

image: The image to which the thresholding must be applied. 

Raises: 

NameError: If invalid thresholding type is provided. i.e. Adaptive or Otsu. 

Returns: 

obj:'OpenCV image': The threshold image. 

""" 

if self.thresholding_type == "adaptive": 

return self.adaptiveThresholding(image) 

elif self.thresholding_type == "otsu": 

return self.otsuThresholding(image) 

else: 

raise NameError('Invalid Thresholding Selection! Could not Apply Thresholding') 

 

@staticmethod 

def adaptiveThresholding(image): 

""" 

This function applies a simple adaptive thresholding to the image passed. 

Author(s): 

Stephan Nell 

Args: 

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

Raises: 

TypeError: If a parameter is passed that is not of type Numpy array. 

Returns: 

obj:'OpenCV image': The Threshold image. 

""" 

if not isinstance(image, np.ndarray): 

raise TypeError( 

'Bad type for arg image - expected image in numpy array. Received type "%s".' % 

type(image).__name__ 

) 

 

return cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 25, 10) 

 

@staticmethod 

def otsuThresholding(image): 

""" 

This function applies a simple Binary Inverse Otso thresholding to the image passed. 

Author(s): 

Stephan Nell 

Args: 

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

Raises: 

TypeError: If a parameter is passed that is not of type Numpy array. 

Returns: 

obj:'OpenCV image': The Threshold image. 

""" 

if not isinstance(image, np.ndarray): 

raise TypeError( 

'Bad type for arg image - expected image in numpy array. Received type "%s".' % 

type(image).__name__ 

) 

 

(_, threshInv) = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) 

return threshInv