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)
| 86 | |
| 87 | |
| 88 | def 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 | |
| 104 | def transform( |
no outgoing calls
no test coverage detected