Normalizes image in Numpy 2D array format, between ranges 0-cap, as to fit uint8 type. Args: image: 2D numpy array representing image as matrix, with values in any range cap: Maximum cap amount for normalization data_type: numpy data type to set output variable
(
image: np.ndarray, cap: float = 255.0, data_type: np.dtype = np.uint8
)
| 23 | |
| 24 | |
| 25 | def normalize_image( |
| 26 | image: np.ndarray, cap: float = 255.0, data_type: np.dtype = np.uint8 |
| 27 | ) -> np.ndarray: |
| 28 | """ |
| 29 | Normalizes image in Numpy 2D array format, between ranges 0-cap, |
| 30 | as to fit uint8 type. |
| 31 | |
| 32 | Args: |
| 33 | image: 2D numpy array representing image as matrix, with values in any range |
| 34 | cap: Maximum cap amount for normalization |
| 35 | data_type: numpy data type to set output variable to |
| 36 | Returns: |
| 37 | return 2D numpy array of type uint8, corresponding to limited range matrix |
| 38 | |
| 39 | Examples: |
| 40 | >>> normalize_image(np.array([[1, 2, 3], [4, 5, 10]]), |
| 41 | ... cap=1.0, data_type=np.float64) |
| 42 | array([[0. , 0.11111111, 0.22222222], |
| 43 | [0.33333333, 0.44444444, 1. ]]) |
| 44 | >>> normalize_image(np.array([[4, 4, 3], [1, 7, 2]])) |
| 45 | array([[127, 127, 85], |
| 46 | [ 0, 255, 42]], dtype=uint8) |
| 47 | """ |
| 48 | normalized = (image - np.min(image)) / (np.max(image) - np.min(image)) * cap |
| 49 | return normalized.astype(data_type) |
| 50 | |
| 51 | |
| 52 | def normalize_array(array: np.ndarray, cap: float = 1) -> np.ndarray: |
nothing calls this directly
no outgoing calls
no test coverage detected