1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| """ 低光照场景增强方案
使用红外补光和图像增强 """
import cv2 import numpy as np
class LowLightEnhancer: """ 低光照图像增强器 方法: 1. 红外补光 2. 直方图均衡化 3. Retinex 算法 4. 深度学习增强 """ def __init__(self, method: str = 'retinex'): self.method = method def enhance( self, image: np.ndarray ) -> np.ndarray: """ 增强低光照图像 Args: image: 输入图像 Returns: enhanced: 增强后的图像 """ if self.method == 'histogram': return self._histogram_equalization(image) elif self.method == 'retinex': return self._retinex(image) elif self.method == 'gamma': return self._gamma_correction(image) else: return image def _histogram_equalization( self, image: np.ndarray ) -> np.ndarray: """直方图均衡化""" if len(image.shape) == 3: ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb) ycrcb[:, :, 0] = cv2.equalizeHist(ycrcb[:, :, 0]) return cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR) else: return cv2.equalizeHist(image) def _retinex( self, image: np.ndarray, sigma: float = 30 ) -> np.ndarray: """ Single-Scale Retinex R = log(I) - log(I * G) 其中 G 是高斯核 """ if len(image.shape) == 3: result = np.zeros_like(image, dtype=np.float32) for i in range(3): result[:, :, i] = self._ssr(image[:, :, i], sigma) else: result = self._ssr(image, sigma) result = cv2.normalize(result, None, 0, 255, cv2.NORM_MINMAX) return result.astype(np.uint8) def _ssr( self, channel: np.ndarray, sigma: float ) -> np.ndarray: """Single-Scale Retinex for single channel""" channel = channel.astype(np.float32) + 1.0 blur = cv2.GaussianBlur(channel, (0, 0), sigma) retinex = np.log(channel) - np.log(blur + 1) return retinex def _gamma_correction( self, image: np.ndarray, gamma: float = 1.5 ) -> np.ndarray: """伽马校正""" mean_brightness = np.mean(image) gamma = np.log(0.5) / np.log(mean_brightness / 255.0) gamma = np.clip(gamma, 0.5, 2.5) table = np.array([ ((i / 255.0) ** gamma) * 255 for i in range(256) ]).astype(np.uint8) return cv2.LUT(image, table)
class IR illuminationConfig: """ 红外补光配置 硬件要求: - 940nm IR LED - 峰值波长:940nm - 辐射强度:≥500mW/sr - 数量:4-8 个 """ def __init__( self, wavelength: int = 940, num_leds: int = 6, peak_power: float = 1000, beam_angle: float = 30 ): self.wavelength = wavelength self.num_leds = num_leds self.peak_power = peak_power self.beam_angle = beam_angle def get_recommended_config( self, cabin_size: Tuple[float, float, float] = (1.5, 1.5, 1.0) ) -> Dict: """ 获取推荐配置 Args: cabin_size: 车厢尺寸 (长, 宽, 高) 米 Returns: config: 配置参数 """ volume = cabin_size[0] * cabin_size[1] * cabin_size[2] total_power = volume * 200 num_leds = int(np.ceil(total_power / self.peak_power)) num_leds = max(4, min(num_leds, 8)) return { 'wavelength': self.wavelength, 'num_leds': num_leds, 'power_per_led': total_power / num_leds, 'placement': self._get_placement(num_leds) } def _get_placement(self, num_leds: int) -> List[Tuple[float, float]]: """获取 LED 布置位置""" placements = { 4: [(0.3, 0.5), (0.7, 0.5), (0.2, 0.3), (0.8, 0.3)], 6: [(0.3, 0.5), (0.7, 0.5), (0.2, 0.3), (0.8, 0.3), (0.5, 0.2), (0.5, 0.8)], 8: [(0.2, 0.5), (0.8, 0.5), (0.1, 0.3), (0.9, 0.3), (0.3, 0.2), (0.7, 0.2), (0.3, 0.8), (0.7, 0.8)] } return placements.get(num_leds, placements[6])
|