image.photography\photo_enhance\photo_enhancement.mbt

93.4% · 155 / 166 points · 409 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 照片增强 photo_enhance/photo_enhancement
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 计算摄影增强(v12.15.0)
  7. 7// 低光增强、去运动模糊、色彩增强、自适应直方图均衡化
  8. 8
  9. 9///|
  10. 10/// 低光增强配置
  11. 11#derive(Eq, @debug.Debug)
  12. 12pub(all) struct LowLightConfig {
  13. 13 method_type : String // "gamma" / "log" / "retinex"
  14. 14 gamma : Double // Gamma 校正参数
  15. 15 strength : Double // 增强强度
  16. 16}
  17. 17
  18. 18///|
  19. 19pub extend LowLightConfig with Eq::{not_equal, equal}
  20. 20
  21. 21///|
  22. 22pub extend LowLightConfig with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25/// 创建默认低光增强配置
  26. 26pub fn low_light_default_config() -> LowLightConfig {
  27. 27● LowLightConfig::{ method_type: "gamma", gamma: 0.5, strength: 1.0, }
  28. 28}
  29. 29
  30. 30///|
  31. 31/// Gamma 校正低光增强
  32. 32fn gamma_enhance(img : @types.Image, gamma : Double) -> @types.Image {
  33. 33● let w = img.width
  34. 34 let h = img.height
  35. 35 let channels = img.channels
  36. 36● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  37. 37 // 预计算 Gamma 查找表
  38. 38 let lut : Array[Byte] = []
  39. 39 for i in 0..<256 {
  40. 40● let normalized = i.to_double() / 255.0
  41. 41● let enhanced = @math.pow(normalized, gamma)
  42. 42● let val = (enhanced * 255.0).to_int().min(255).max(0)
  43. 43● lut.push(val.to_byte())
  44. 44 }
  45. 45 for i in 0..<(w * h * channels) {
  46. 46● out_data[i] = lut[img.data[i].to_int()]
  47. 47 }
  48. 48 @types.Image::{
  49. 49 width: w,
  50. 50 height: h,
  51. 51 channels,
  52. 52● data: Bytes::from_array(out_data),
  53. 53 }
  54. 54}
  55. 55
  56. 56///|
  57. 57/// 对数变换低光增强
  58. 58fn log_enhance(img : @types.Image, strength : Double) -> @types.Image {
  59. 59● let w = img.width
  60. 60 let h = img.height
  61. 61 let channels = img.channels
  62. 62● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  63. 63● let c = 255.0 / @math.ln(1.0 + 255.0)
  64. 64 for i in 0..<(w * h * channels) {
  65. 65● let val = img.data[i].to_double()
  66. 66● let enhanced = c * @math.ln(1.0 + val) * strength
  67. 67● out_data[i] = enhanced.min(255.0).max(0.0).to_int().to_byte()
  68. 68 }
  69. 69 @types.Image::{
  70. 70 width: w,
  71. 71 height: h,
  72. 72 channels,
  73. 73● data: Bytes::from_array(out_data),
  74. 74 }
  75. 75}
  76. 76
  77. 77///|
  78. 78/// 简化版 Retinex 低光增强(单尺度 Retinex)
  79. 79fn retinex_enhance(img : @types.Image, sigma~ : Double = 50.0) -> @types.Image {
  80. 80● let _ = sigma
  81. 81 let w = img.width
  82. 82 let h = img.height
  83. 83 let channels = img.channels
  84. 84 // 简化版:使用高斯模糊作为光照估计
  85. 85 // 这里用简单的均值模糊代替(避免依赖外部高斯模糊)
  86. 86 let kernel_size = 15
  87. 87 let half = kernel_size / 2
  88. 88● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  89. 89 for y in 0..<h {
  90. 90● for x in 0..<w {
  91. 91● let idx = (y * w + x) * channels
  92. 92 for c in 0..<channels {
  93. 93 // 计算局部均值(光照估计)
  94. 94● let mut sum = 0.0
  95. 95 let mut count = 0
  96. 96 for ky in -half..<=half {
  97. 97● for kx in -half..<=half {
  98. 98● let ny = y + ky
  99. 99 let nx = x + kx
  100. 100 if ny >= 0 && ny < h && nx >= 0 && nx < w {
  101. 101● let nidx = (ny * w + nx) * channels
  102. 102● sum += img.data[nidx + c].to_double()
  103. 103 count += 1
  104. 104 }
  105. 105 }
  106. 106 }
  107. 107◐ let illumination = if count > 0 { sum / count.to_double() } else { 0.0 }
  108. 108 // Retinex: R = log(I) - log(L) = log(I/L)
  109. 109● let original = img.data[idx + c].to_double() + 1.0
  110. 110 let light = illumination + 1.0
  111. 111 let ratio = original / light
  112. 112● let reflectance = @math.ln(ratio)
  113. 113 // 映射回 0-255
  114. 114 let enhanced = (reflectance + 3.0) / 6.0 * 255.0
  115. 115● out_data[idx + c] = enhanced.min(255.0).max(0.0).to_int().to_byte()
  116. 116 }
  117. 117 }
  118. 118 }
  119. 119 @types.Image::{
  120. 120 width: w,
  121. 121 height: h,
  122. 122 channels,
  123. 123● data: Bytes::from_array(out_data),
  124. 124 }
  125. 125}
  126. 126
  127. 127///|
  128. 128/// 低光增强(统一接口)
  129. 129pub fn low_light_enhance(
  130. 130 img : @types.Image,
  131. 131 config? : LowLightConfig,
  132. 132) -> @types.Image {
  133. 133● let cfg = match config {
  134. 134● Some(v) => v
  135. 135● None => low_light_default_config()
  136. 136 }
  137. 137● match cfg.method_type {
  138. 138● "gamma" => gamma_enhance(img, cfg.gamma)
  139. 139● "log" => log_enhance(img, cfg.strength)
  140. 140● "retinex" => retinex_enhance(img, sigma=50.0)
  141. 141○ _ => gamma_enhance(img, cfg.gamma)
  142. 142 }
  143. 143}
  144. 144
  145. 145///|
  146. 146/// 去运动模糊(简化版维纳滤波)
  147. 147pub fn deblur_motion(
  148. 148 img : @types.Image,
  149. 149 kernel_size~ : Int32 = 15,
  150. 150 angle~ : Double = 0.0,
  151. 151) -> @types.Image {
  152. 152● let ks = kernel_size
  153. 153 let _ = angle
  154. 154 let w = img.width
  155. 155 let h = img.height
  156. 156 let channels = img.channels
  157. 157 // 简化版:使用非锐化掩模(Unsharp Mask)作为去模糊近似
  158. 158 let amount = 1.5
  159. 159 let threshold = 0
  160. 160● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  161. 161 let blur_radius = ks / 2
  162. 162 for y in 0..<h {
  163. 163● for x in 0..<w {
  164. 164● let idx = (y * w + x) * channels
  165. 165 for c in 0..<channels {
  166. 166 // 计算局部均值(模糊版本)
  167. 167● let mut sum = 0.0
  168. 168 let mut count = 0
  169. 169 for ky in -blur_radius..<=blur_radius {
  170. 170● for kx in -blur_radius..<=blur_radius {
  171. 171● let ny = y + ky
  172. 172 let nx = x + kx
  173. 173 if ny >= 0 && ny < h && nx >= 0 && nx < w {
  174. 174● let nidx = (ny * w + nx) * channels
  175. 175● sum += img.data[nidx + c].to_double()
  176. 176 count += 1
  177. 177 }
  178. 178 }
  179. 179 }
  180. 180◐ let blurred = if count > 0 { sum / count.to_double() } else { 0.0 }
  181. 181● let original = img.data[idx + c].to_double()
  182. 182 // 非锐化掩模:sharpened = original + amount * (original - blurred)
  183. 183 let diff = original - blurred
  184. 184● let sharpened = if diff.abs() > threshold.to_double() {
  185. 185● original + amount * diff
  186. 186 } else {
  187. 187● original
  188. 188 }
  189. 189● out_data[idx + c] = sharpened.min(255.0).max(0.0).to_int().to_byte()
  190. 190 }
  191. 191 }
  192. 192 }
  193. 193 @types.Image::{
  194. 194 width: w,
  195. 195 height: h,
  196. 196 channels,
  197. 197● data: Bytes::from_array(out_data),
  198. 198 }
  199. 199}
  200. 200
  201. 201///|
  202. 202/// 色彩增强(饱和度和对比度增强)
  203. 203pub fn color_enhance(
  204. 204 img : @types.Image,
  205. 205 saturation~ : Double = 1.3,
  206. 206 contrast~ : Double = 1.1,
  207. 207) -> @types.Image {
  208. 208● let sat = saturation
  209. 209 let con = contrast
  210. 210 let w = img.width
  211. 211 let h = img.height
  212. 212 let channels = img.channels
  213. 213 if channels < 3 {
  214. 214○ return img
  215. 215 }
  216. 216● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  217. 217 for y in 0..<h {
  218. 218● for x in 0..<w {
  219. 219● let idx = (y * w + x) * channels
  220. 220● let r = img.data[idx].to_double()
  221. 221● let g = img.data[idx + 1].to_double()
  222. 222● let b = img.data[idx + 2].to_double()
  223. 223 // 计算灰度值
  224. 224 let gray = 0.299 * r + 0.587 * g + 0.114 * b
  225. 225 // 饱和度增强
  226. 226 let new_r = gray + (r - gray) * sat
  227. 227 let new_g = gray + (g - gray) * sat
  228. 228 let new_b = gray + (b - gray) * sat
  229. 229 // 对比度增强
  230. 230 let final_r = (new_r - 128.0) * con + 128.0
  231. 231 let final_g = (new_g - 128.0) * con + 128.0
  232. 232 let final_b = (new_b - 128.0) * con + 128.0
  233. 233● out_data[idx] = final_r.min(255.0).max(0.0).to_int().to_byte()
  234. 234● out_data[idx + 1] = final_g.min(255.0).max(0.0).to_int().to_byte()
  235. 235● out_data[idx + 2] = final_b.min(255.0).max(0.0).to_int().to_byte()
  236. 236 // 复制其他通道
  237. 237 for c in 3..<channels {
  238. 238○ out_data[idx + c] = img.data[idx + c]
  239. 239 }
  240. 240 }
  241. 241 }
  242. 242 @types.Image::{
  243. 243 width: w,
  244. 244 height: h,
  245. 245 channels,
  246. 246● data: Bytes::from_array(out_data),
  247. 247 }
  248. 248}
  249. 249
  250. 250///|
  251. 251/// 自适应直方图均衡化(CLAHE 简化版)
  252. 252pub fn clahe_enhance(
  253. 253 img : @types.Image,
  254. 254 clip_limit~ : Double = 2.0,
  255. 255 tile_size~ : Int32 = 8,
  256. 256) -> @types.Image {
  257. 257● let clip = clip_limit
  258. 258 let _ = tile_size
  259. 259 let w = img.width
  260. 260 let h = img.height
  261. 261 let channels = img.channels
  262. 262 // 简化版:全局直方图均衡化
  263. 263● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  264. 264 for c in 0..<channels {
  265. 265 // 计算直方图
  266. 266● let histogram : Array[Int32] = []
  267. 267 for _ in 0..<256 {
  268. 268● histogram.push(0)
  269. 269 }
  270. 270 for y in 0..<h {
  271. 271● for x in 0..<w {
  272. 272● let idx = (y * w + x) * channels
  273. 273● histogram[img.data[idx + c].to_int()] += 1
  274. 274 }
  275. 275 }
  276. 276 // 裁剪直方图
  277. 277● let total_pixels = (w * h).to_double()
  278. 278● let max_count = (clip * total_pixels / 256.0).to_int()
  279. 279 let mut excess = 0
  280. 280 for i in 0..<256 {
  281. 281● if histogram[i] > max_count {
  282. 282● excess += histogram[i] - max_count
  283. 283 histogram[i] = max_count
  284. 284 }
  285. 285 }
  286. 286 // 重新分配超额像素
  287. 287 let redistribute = excess / 256
  288. 288 for i in 0..<256 {
  289. 289● histogram[i] += redistribute
  290. 290 }
  291. 291 // 计算累积分布函数(CDF)
  292. 292 let cdf : Array[Int32] = []
  293. 293 let mut cumsum = 0
  294. 294 for i in 0..<256 {
  295. 295● cumsum += histogram[i]
  296. 296● cdf.push(cumsum)
  297. 297 }
  298. 298 // 查找表
  299. 299 let lut : Array[Byte] = []
  300. 300 let cdf_min = cdf[0]
  301. 301● let denominator = (w * h - cdf_min).max(1)
  302. 302 for i in 0..<256 {
  303. 303● let val = ((cdf[i] - cdf_min).to_double() /
  304. 304● denominator.to_double() *
  305. 305 255.0)
  306. 306● .to_int()
  307. 307● .min(255)
  308. 308● .max(0)
  309. 309● lut.push(val.to_byte())
  310. 310 }
  311. 311 // 应用查找表
  312. 312 for y in 0..<h {
  313. 313● for x in 0..<w {
  314. 314● let idx = (y * w + x) * channels
  315. 315● out_data[idx + c] = lut[img.data[idx + c].to_int()]
  316. 316 }
  317. 317 }
  318. 318 }
  319. 319 @types.Image::{
  320. 320 width: w,
  321. 321 height: h,
  322. 322 channels,
  323. 323● data: Bytes::from_array(out_data),
  324. 324 }
  325. 325}
  326. 326
  327. 327///|
  328. 328/// 白平衡校正(灰度世界算法)
  329. 329pub fn white_balance(img : @types.Image) -> @types.Image {
  330. 330● let w = img.width
  331. 331 let h = img.height
  332. 332 let channels = img.channels
  333. 333 if channels < 3 {
  334. 334○ return img
  335. 335 }
  336. 336 // 计算各通道平均值
  337. 337 let mut sum_r = 0.0
  338. 338 let mut sum_g = 0.0
  339. 339 let mut sum_b = 0.0
  340. 340● let total = (w * h).to_double()
  341. 341 for y in 0..<h {
  342. 342● for x in 0..<w {
  343. 343● let idx = (y * w + x) * channels
  344. 344● sum_r += img.data[idx].to_double()
  345. 345● sum_g += img.data[idx + 1].to_double()
  346. 346● sum_b += img.data[idx + 2].to_double()
  347. 347 }
  348. 348 }
  349. 349 let avg_r = sum_r / total
  350. 350 let avg_g = sum_g / total
  351. 351 let avg_b = sum_b / total
  352. 352 let avg_gray = (avg_r + avg_g + avg_b) / 3.0
  353. 353 // 计算增益
  354. 354● let gain_r = avg_gray / avg_r.max(1.0)
  355. 355● let gain_g = avg_gray / avg_g.max(1.0)
  356. 356● let gain_b = avg_gray / avg_b.max(1.0)
  357. 357 // 应用增益
  358. 358● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  359. 359 for y in 0..<h {
  360. 360● for x in 0..<w {
  361. 361● let idx = (y * w + x) * channels
  362. 362● out_data[idx] = (img.data[idx].to_double() * gain_r)
  363. 363● .min(255.0)
  364. 364● .max(0.0)
  365. 365● .to_int()
  366. 366● .to_byte()
  367. 367● out_data[idx + 1] = (img.data[idx + 1].to_double() * gain_g)
  368. 368● .min(255.0)
  369. 369● .max(0.0)
  370. 370● .to_int()
  371. 371● .to_byte()
  372. 372● out_data[idx + 2] = (img.data[idx + 2].to_double() * gain_b)
  373. 373● .min(255.0)
  374. 374● .max(0.0)
  375. 375● .to_int()
  376. 376● .to_byte()
  377. 377 for c in 3..<channels {
  378. 378○ out_data[idx + c] = img.data[idx + c]
  379. 379 }
  380. 380 }
  381. 381 }
  382. 382 @types.Image::{
  383. 383 width: w,
  384. 384 height: h,
  385. 385 channels,
  386. 386● data: Bytes::from_array(out_data),
  387. 387 }
  388. 388}
  389. 389
  390. 390///|
  391. 391/// 批量计算摄影增强
  392. 392pub fn batch_photo_enhance(
  393. 393 images : Array[@types.Image],
  394. 394 method_type : String,
  395. 395) -> Array[@types.Image] {
  396. 396● let result : Array[@types.Image] = []
  397. 397 for img in images {
  398. 398● let enhanced = match method_type {
  399. 399● "low_light" => low_light_enhance(img)
  400. 400○ "deblur" => deblur_motion(img, kernel_size=15, angle=0.0)
  401. 401○ "color" => color_enhance(img, saturation=1.3, contrast=1.1)
  402. 402● "clahe" => clahe_enhance(img, clip_limit=2.0, tile_size=8)
  403. 403○ "white_balance" => white_balance(img)
  404. 404○ _ => img
  405. 405 }
  406. 406● result.push(enhanced)
  407. 407 }
  408. 408 result
  409. 409}