标准化图像像素值 Args: image: 输入图像 target_mean: 目标均值 target_std: 目标标准差 Returns: 标准化后的numpy数组 Raises: ImageProcessError: 当处理失败时
(image: Union[Image.Image, np.ndarray],
target_mean: float = 0.0, target_std: float = 1.0)
| 80 | |
| 81 | @staticmethod |
| 82 | def normalize_image(image: Union[Image.Image, np.ndarray], |
| 83 | target_mean: float = 0.0, target_std: float = 1.0) -> np.ndarray: |
| 84 | """ |
| 85 | 标准化图像像素值 |
| 86 | |
| 87 | Args: |
| 88 | image: 输入图像 |
| 89 | target_mean: 目标均值 |
| 90 | target_std: 目标标准差 |
| 91 | |
| 92 | Returns: |
| 93 | 标准化后的numpy数组 |
| 94 | |
| 95 | Raises: |
| 96 | ImageProcessError: 当处理失败时 |
| 97 | """ |
| 98 | try: |
| 99 | if isinstance(image, Image.Image): |
| 100 | img_array = image_to_numpy(image) |
| 101 | else: |
| 102 | img_array = image.copy() |
| 103 | |
| 104 | # 转换为float32并归一化到[0,1] |
| 105 | img_array = img_array.astype(np.float32) / 255.0 |
| 106 | |
| 107 | # 计算当前均值和标准差 |
| 108 | current_mean = np.mean(img_array) |
| 109 | current_std = np.std(img_array) |
| 110 | |
| 111 | # 避免除零 |
| 112 | if current_std == 0: |
| 113 | current_std = 1.0 |
| 114 | |
| 115 | # 标准化 |
| 116 | normalized = (img_array - current_mean) / current_std |
| 117 | normalized = normalized * target_std + target_mean |
| 118 | |
| 119 | return normalized |
| 120 | |
| 121 | except Exception as e: |
| 122 | raise ImageProcessError(f"图像标准化失败: {str(e)}") from e |
| 123 | |
| 124 | @staticmethod |
| 125 | def enhance_contrast(image: Image.Image, factor: float = 1.5) -> Image.Image: |
nothing calls this directly
no test coverage detected