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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| """ 热成像 CPD 检测预处理算法
核心方法: 1. 温度归一化 2. 背景去除(座椅温度) 3. 热噪声滤波 """
import numpy as np from scipy.ndimage import median_filter
def thermal_image_preprocessing( thermal_image: np.ndarray, ambient_temp: float = 25.0 ) -> np.ndarray: """ 热图像预处理 Args: thermal_image: 原始热图像, shape=(H, W), 单位:摄氏度 ambient_temp: 环境温度(座椅背景温度) Returns: normalized: 归一化热图像,背景已去除 CPD 应用: 去除座椅背景温度,突出人体热辐射 """ background_removed = thermal_image - ambient_temp min_temp = 0 max_temp = 10 normalized = np.clip(background_removed, min_temp, max_temp) normalized = (normalized - min_temp) / (max_temp - min_temp) denoised = median_filter(normalized, size=3) return denoised
def detect_breathing_thermal( thermal_sequence: np.ndarray, fps: int = 30, threshold_temp: float = 0.5 ) -> Tuple[bool, float]: """ 从热图像序列检测呼吸 Args: thermal_sequence: 热图像序列, shape=(N, H, W) fps: 帧率 threshold_temp: 呼吸温差阈值(°C) Returns: is_breathing: 是否检测到呼吸 breathing_rate: 呼吸频率(次/分钟) CPD 应用: 儿童呼吸特征: - 呼吸温差:0.3-1.0°C(鼻腔/胸口) - 呼吸频率:20-40 次/分钟 """ roi_sequence = thermal_sequence[:, 100:200, 150:250] temp_time_series = np.mean(roi_sequence, axis=(1, 2)) temp_variation = np.max(temp_time_series) - np.min(temp_time_series) is_breathing = temp_variation > threshold_temp if is_breathing: temp_ac = temp_time_series - np.mean(temp_time_series) fft_result = np.fft.fft(temp_ac) freqs = np.fft.fftfreq(len(temp_ac), 1.0/fps) breathing_band = (freqs >= 0.1) & (freqs <= 0.5) breathing_spectrum = np.abs(fft_result[breathing_band]) peak_idx = np.argmax(breathing_spectrum) breathing_freq = freqs[breathing_band][peak_idx] breathing_rate = abs(breathing_freq) * 60 else: breathing_rate = 0 return is_breathing, breathing_rate
def detect_child_presence_thermal( thermal_image: np.ndarray, ambient_temp: float = 25.0, min_area: int = 500 ) -> Tuple[bool, Dict]: """ 检测儿童存在 Args: thermal_image: 热图像 ambient_temp: 环境温度 min_area: 最小人体区域面积(像素) Returns: is_child: 是否检测到儿童 info: { 'body_temp': 人体温度, 'body_area': 人体面积, 'position': 位置坐标 } CPD 应用: 儿童判定逻辑: 1. 检测到人体温度(32-37°C) 2. 人体面积小于成人(体型判断) 3. 检测到呼吸温差 """ preprocessed = thermal_image_preprocessing(thermal_image, ambient_temp) body_temp_min = 32.0 - ambient_temp body_temp_max = 38.0 - ambient_temp body_mask = (thermal_image >= ambient_temp + body_temp_min) & \ (thermal_image <= ambient_temp + body_temp_max) from scipy.ndimage import label labeled, num_features = label(body_mask) if num_features > 0: region_sizes = np.bincount(labeled.ravel())[1:] largest_region_label = np.argmax(region_sizes) + 1 body_area = region_sizes[largest_region_label - 1] region_mask = labeled == largest_region_label body_temp = np.mean(thermal_image[region_mask]) y_coords, x_coords = np.where(region_mask) position = (int(np.mean(x_coords)), int(np.mean(y_coords))) is_child = body_area > min_area and body_area < 3000 return is_child, { 'body_temp': body_temp, 'body_area': body_area, 'position': position } return False, {}
if __name__ == "__main__": H, W = 320, 240 background = np.ones((H, W)) * 25.0 child_thermal = background.copy() child_thermal[100:150, 120:160] = 35.0 noise = np.random.randn(H, W) * 0.2 child_thermal += noise is_child, info = detect_child_presence_thermal(child_thermal) print("="*60) print("热成像 CPD 儿童检测测试") print("="*60) print(f"检测到儿童: {is_child}") print(f"人体温度: {info.get('body_temp', 0):.1f}°C") print(f"人体面积: {info.get('body_area', 0):.0f} 像素") print(f"位置: {info.get('position', (0, 0))}")
|