Converts a NumPy array to a PIL image. Automatically: - Squeezes dimensions of size 1. - Moves the channel dimension (if it has size 3) to the last axis. Args: array (np.ndarray): Input array. Expected shape can be [C, H, W], [H, W, C], or [H, W]. Retur
(array)
| 122 | |
| 123 | |
| 124 | def array_to_pil(array): |
| 125 | """ |
| 126 | Converts a NumPy array to a PIL image. Automatically: |
| 127 | - Squeezes dimensions of size 1. |
| 128 | - Moves the channel dimension (if it has size 3) to the last axis. |
| 129 | |
| 130 | Args: |
| 131 | array (np.ndarray): Input array. Expected shape can be [C, H, W], [H, W, C], or [H, W]. |
| 132 | |
| 133 | Returns: |
| 134 | PIL.Image: The converted PIL image. |
| 135 | """ |
| 136 | # Remove singleton dimensions |
| 137 | array = np.squeeze(array) |
| 138 | |
| 139 | # Ensure the array has the channel dimension as the last axis |
| 140 | if array.ndim == 3 and array.shape[0] == 3: # If the channel is the first axis |
| 141 | array = np.transpose(array, (1, 2, 0)) # Move channel to the last axis |
| 142 | |
| 143 | # Handle single-channel grayscale images |
| 144 | if array.ndim == 2: # [H, W] |
| 145 | return Image.fromarray((array * 255).astype(np.uint8), mode="L") |
| 146 | elif array.ndim == 3 and array.shape[2] == 3: # [H, W, C] with 3 channels |
| 147 | return Image.fromarray((array * 255).astype(np.uint8), mode="RGB") |
| 148 | else: |
| 149 | raise ValueError(f"Unsupported array shape for PIL conversion: {array.shape}") |
| 150 | |
| 151 | |
| 152 | def find_best_alignment(gt_normal, pred_normal): |