MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / binarize

Function binarize

computer_vision/haralick_descriptors.py:88–101  ·  view source on GitHub ↗

Binarizes a grayscale image based on a given threshold value, setting values to 1 or 0 accordingly. Examples: >>> binarize(np.array([[128, 255], [101, 156]])) array([[1, 1], [0, 1]]) >>> binarize(np.array([[0.07, 1], [0.51, 0.3]]), threshold=0.5)

(image: np.ndarray, threshold: float = 127.0)

Source from the content-addressed store, hash-verified

86
87
88def binarize(image: np.ndarray, threshold: float = 127.0) -> np.ndarray:
89 """
90 Binarizes a grayscale image based on a given threshold value,
91 setting values to 1 or 0 accordingly.
92
93 Examples:
94 >>> binarize(np.array([[128, 255], [101, 156]]))
95 array([[1, 1],
96 [0, 1]])
97 >>> binarize(np.array([[0.07, 1], [0.51, 0.3]]), threshold=0.5)
98 array([[0, 1],
99 [1, 0]])
100 """
101 return np.where(image > threshold, 1, 0)
102
103
104def transform(

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected