image.process\enhance\advanced_denoise_enhance.mbt

91.8% · 257 / 280 points · 956 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/// # 高级降噪增强 advanced denoise enhance
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 高级图像降噪与增强算法(v29.0.0)
  7. 7// 高级降噪、图像增强、质量评估
  8. 8
  9. 9///|
  10. 10/// 降噪算法类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq)
  13. 13#non_exhaustive
  14. 14pub(all) enum DenoiseAlgorithm {
  15. 15 MeanFilter
  16. 16 MedianFilter
  17. 17 GaussianFilter
  18. 18 BilateralFilter
  19. 19 NonLocalMeans
  20. 20 WienerFilter
  21. 21 TotalVariation
  22. 22 WaveletDenoise
  23. 23 BM3D
  24. 24 DeepLearningDenoise
  25. 25 Unknown
  26. 26}
  27. 27
  28. 28///|
  29. 29pub extend DenoiseAlgorithm with Eq::{not_equal, equal}
  30. 30
  31. 31///|
  32. 32/// 降噪结果
  33. 33pub(all) struct DenoiseResult {
  34. 34 image : @types.Image
  35. 35 algorithm : DenoiseAlgorithm
  36. 36 noise_level_before : Double
  37. 37 noise_level_after : Double
  38. 38 psnr : Double
  39. 39 ssim : Double
  40. 40 processing_time_ms : Double
  41. 41 strength : Double
  42. 42}
  43. 43
  44. 44///|
  45. 45/// 降噪配置
  46. 46pub(all) struct DenoiseConfig {
  47. 47 algorithm : DenoiseAlgorithm
  48. 48 strength : Double
  49. 49 kernel_size : Int32
  50. 50 sigma_color : Double
  51. 51 sigma_space : Double
  52. 52 search_window : Int32
  53. 53 patch_size : Int32
  54. 54 iterations : Int32
  55. 55}
  56. 56
  57. 57///|
  58. 58/// 增强算法类型
  59. 59/// `#non_exhaustive` 表示未来可能添加新构造器
  60. 60#derive(Eq)
  61. 61#non_exhaustive
  62. 62pub(all) enum EnhancementAlgorithm {
  63. 63 HistogramEqualization
  64. 64 CLAHE
  65. 65 GammaCorrection
  66. 66 UnsharpMask
  67. 67 Retinex
  68. 68 HomomorphicFiltering
  69. 69 ColorBalance
  70. 70 ContrastStretching
  71. 71 AdaptiveThreshold
  72. 72 DeepLearningEnhance
  73. 73 Unknown
  74. 74}
  75. 75
  76. 76///|
  77. 77pub extend EnhancementAlgorithm with Eq::{not_equal, equal}
  78. 78
  79. 79///|
  80. 80/// 增强结果
  81. 81pub(all) struct EnhancementResult {
  82. 82 image : @types.Image
  83. 83 algorithm : EnhancementAlgorithm
  84. 84 brightness_before : Double
  85. 85 brightness_after : Double
  86. 86 contrast_before : Double
  87. 87 contrast_after : Double
  88. 88 sharpness_before : Double
  89. 89 sharpness_after : Double
  90. 90 processing_time_ms : Double
  91. 91}
  92. 92
  93. 93///|
  94. 94/// 噪声类型
  95. 95/// `#non_exhaustive` 表示未来可能添加新构造器
  96. 96#derive(Eq)
  97. 97#non_exhaustive
  98. 98pub(all) enum NoiseType {
  99. 99 GaussianNoise
  100. 100 SaltAndPepper
  101. 101 SpeckleNoise
  102. 102 PoissonNoise
  103. 103 UniformNoise
  104. 104 Unknown
  105. 105}
  106. 106
  107. 107///|
  108. 108pub extend NoiseType with Eq::{not_equal, equal}
  109. 109
  110. 110///|
  111. 111/// 创建默认降噪配置
  112. 112pub fn create_default_denoise_config() -> DenoiseConfig {
  113. 113● DenoiseConfig::{
  114. 114 algorithm: DenoiseAlgorithm::BilateralFilter,
  115. 115 strength: 0.5,
  116. 116 kernel_size: 5,
  117. 117 sigma_color: 75.0,
  118. 118 sigma_space: 75.0,
  119. 119 search_window: 21,
  120. 120 patch_size: 7,
  121. 121 iterations: 3,
  122. 122 }
  123. 123}
  124. 124
  125. 125///|
  126. 126/// 均值滤波
  127. 127pub fn mean_filter_denoise(
  128. 128 img : @types.Image,
  129. 129 kernel_size : Int32,
  130. 130) -> @types.Image raise @types.LoadError {
  131. 131 // 输入验证
  132. 132● if img.width <= 0 || img.height <= 0 {
  133. 133● raise @types.LoadError::DecodeFailed(
  134. 134 "mean_filter_denoise: 图像尺寸必须大于0",
  135. 135 )
  136. 136 }
  137. 137 if img.channels < 1 || img.channels > 4 {
  138. 138● raise @types.LoadError::DecodeFailed(
  139. 139 "mean_filter_denoise: 通道数必须在1-4之间",
  140. 140 )
  141. 141 }
  142. 142 let expected_size = img.width * img.height * img.channels
  143. 143● if img.data.length() != expected_size {
  144. 144● raise @types.LoadError::DecodeFailed(
  145. 145 "mean_filter_denoise: 数据大小不匹配",
  146. 146 )
  147. 147 }
  148. 148 if kernel_size < 1 {
  149. 149○ raise @types.LoadError::DecodeFailed(
  150. 150 "mean_filter_denoise: kernel_size 必须 >= 1",
  151. 151 )
  152. 152 }
  153. 153 let w = img.width
  154. 154 let h = img.height
  155. 155 let _ = w * h
  156. 156 let out : Array[Byte] = []
  157. 157 let radius = kernel_size / 2
  158. 158
  159. 159 for y in 0..<h {
  160. 160● for x in 0..<w {
  161. 161● let mut sum = 0.0
  162. 162 let mut count = 0
  163. 163 for dy in -radius..<(radius + 1) {
  164. 164● for dx in -radius..<(radius + 1) {
  165. 165● let nx = x + dx
  166. 166 let ny = y + dy
  167. 167 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  168. 168● sum += img.data[ny * w + nx].to_double()
  169. 169 count += 1
  170. 170 }
  171. 171 }
  172. 172 }
  173. 173 let avg = if count > 0 {
  174. 174● sum / count.to_double()
  175. 175 } else {
  176. 176○ img.data[y * w + x].to_double()
  177. 177 }
  178. 178 let clamped = if avg < 0.0 {
  179. 179○ 0.0
  180. 180● } else if avg > 255.0 {
  181. 181○ 255.0
  182. 182 } else {
  183. 183● avg
  184. 184 }
  185. 185● out.push(clamped.to_int().to_byte())
  186. 186 }
  187. 187 }
  188. 188
  189. 189 @types.Image::{
  190. 190 width: w,
  191. 191 height: h,
  192. 192 channels: img.channels,
  193. 193● data: Bytes::from_array(out),
  194. 194 }
  195. 195}
  196. 196
  197. 197///|
  198. 198/// 中值滤波
  199. 199pub fn median_filter_denoise(
  200. 200 img : @types.Image,
  201. 201 kernel_size : Int32,
  202. 202) -> @types.Image raise @types.LoadError {
  203. 203 // 输入验证
  204. 204● if img.width <= 0 || img.height <= 0 {
  205. 205● raise @types.LoadError::DecodeFailed(
  206. 206 "median_filter_denoise: 图像尺寸必须大于0",
  207. 207 )
  208. 208 }
  209. 209 if img.channels < 1 || img.channels > 4 {
  210. 210● raise @types.LoadError::DecodeFailed(
  211. 211 "median_filter_denoise: 通道数必须在1-4之间",
  212. 212 )
  213. 213 }
  214. 214 let expected_size = img.width * img.height * img.channels
  215. 215● if img.data.length() != expected_size {
  216. 216● raise @types.LoadError::DecodeFailed(
  217. 217 "median_filter_denoise: 数据大小不匹配",
  218. 218 )
  219. 219 }
  220. 220 if kernel_size < 1 {
  221. 221○ raise @types.LoadError::DecodeFailed(
  222. 222 "median_filter_denoise: kernel_size 必须 >= 1",
  223. 223 )
  224. 224 }
  225. 225 let w = img.width
  226. 226 let h = img.height
  227. 227 let _ = w * h
  228. 228 let out : Array[Byte] = []
  229. 229 let radius = kernel_size / 2
  230. 230
  231. 231 for y in 0..<h {
  232. 232● for x in 0..<w {
  233. 233● let neighbors : Array[Int32] = []
  234. 234 for dy in -radius..<(radius + 1) {
  235. 235● for dx in -radius..<(radius + 1) {
  236. 236● let nx = x + dx
  237. 237 let ny = y + dy
  238. 238 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  239. 239● neighbors.push(img.data[ny * w + nx].to_int())
  240. 240 }
  241. 241 }
  242. 242 }
  243. 243 // 冒泡排序
  244. 244● for i in 0..<neighbors.length() {
  245. 245● for j in 0..<(neighbors.length() - i - 1) {
  246. 246● if neighbors[j] > neighbors[j + 1] {
  247. 247● let temp = neighbors[j]
  248. 248 neighbors[j] = neighbors[j + 1]
  249. 249 neighbors[j + 1] = temp
  250. 250 }
  251. 251 }
  252. 252 }
  253. 253● let median = neighbors[neighbors.length() / 2]
  254. 254● out.push(median.to_byte())
  255. 255 }
  256. 256 }
  257. 257
  258. 258 @types.Image::{
  259. 259 width: w,
  260. 260 height: h,
  261. 261 channels: img.channels,
  262. 262● data: Bytes::from_array(out),
  263. 263 }
  264. 264}
  265. 265
  266. 266///|
  267. 267/// 高斯滤波
  268. 268pub fn gaussian_filter_denoise(
  269. 269 img : @types.Image,
  270. 270 kernel_size : Int32,
  271. 271 sigma : Double,
  272. 272) -> @types.Image raise @types.LoadError {
  273. 273 // 输入验证
  274. 274● if img.width <= 0 || img.height <= 0 {
  275. 275● raise @types.LoadError::DecodeFailed(
  276. 276 "gaussian_filter_denoise: 图像尺寸必须大于0",
  277. 277 )
  278. 278 }
  279. 279 if img.channels < 1 || img.channels > 4 {
  280. 280● raise @types.LoadError::DecodeFailed(
  281. 281 "gaussian_filter_denoise: 通道数必须在1-4之间",
  282. 282 )
  283. 283 }
  284. 284 let expected_size = img.width * img.height * img.channels
  285. 285● if img.data.length() != expected_size {
  286. 286● raise @types.LoadError::DecodeFailed(
  287. 287 "gaussian_filter_denoise: 数据大小不匹配",
  288. 288 )
  289. 289 }
  290. 290 if kernel_size < 1 {
  291. 291○ raise @types.LoadError::DecodeFailed(
  292. 292 "gaussian_filter_denoise: kernel_size 必须 >= 1",
  293. 293 )
  294. 294 }
  295. 295 if sigma <= 0.0 {
  296. 296○ raise @types.LoadError::DecodeFailed(
  297. 297 "gaussian_filter_denoise: sigma 必须大于0",
  298. 298 )
  299. 299 }
  300. 300 let w = img.width
  301. 301 let h = img.height
  302. 302 let _ = w * h
  303. 303 let out : Array[Byte] = []
  304. 304 let radius = kernel_size / 2
  305. 305
  306. 306 // 生成高斯核
  307. 307 let kernel : Array[Double] = []
  308. 308 let mut kernel_sum = 0.0
  309. 309 for dy in -radius..<(radius + 1) {
  310. 310● for dx in -radius..<(radius + 1) {
  311. 311● let dist = (dx * dx + dy * dy).to_double()
  312. 312 let weight = (0.0 - dist / (2.0 * sigma * sigma)) * 0.1 // 简化的 exp
  313. 313● kernel.push(weight)
  314. 314 kernel_sum += weight
  315. 315 }
  316. 316 }
  317. 317 // 归一化
  318. 318● for i in 0..<kernel.length() {
  319. 319● kernel[i] = kernel[i] / kernel_sum
  320. 320 }
  321. 321
  322. 322 for y in 0..<h {
  323. 323● for x in 0..<w {
  324. 324● let mut sum = 0.0
  325. 325 let mut idx = 0
  326. 326 for dy in -radius..<(radius + 1) {
  327. 327● for dx in -radius..<(radius + 1) {
  328. 328● let nx = x + dx
  329. 329 let ny = y + dy
  330. 330 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  331. 331● sum += img.data[ny * w + nx].to_double() * kernel[idx]
  332. 332 }
  333. 333 idx += 1
  334. 334 }
  335. 335 }
  336. 336 let clamped = if sum < 0.0 {
  337. 337○ 0.0
  338. 338● } else if sum > 255.0 {
  339. 339○ 255.0
  340. 340 } else {
  341. 341● sum
  342. 342 }
  343. 343● out.push(clamped.to_int().to_byte())
  344. 344 }
  345. 345 }
  346. 346
  347. 347 @types.Image::{
  348. 348 width: w,
  349. 349 height: h,
  350. 350 channels: img.channels,
  351. 351● data: Bytes::from_array(out),
  352. 352 }
  353. 353}
  354. 354
  355. 355///|
  356. 356/// 双边滤波(保边降噪)
  357. 357pub fn bilateral_filter_denoise(
  358. 358 img : @types.Image,
  359. 359 kernel_size : Int32,
  360. 360 sigma_color : Double,
  361. 361 sigma_space : Double,
  362. 362) -> @types.Image raise @types.LoadError {
  363. 363 // 输入验证
  364. 364● if img.width <= 0 || img.height <= 0 {
  365. 365● raise @types.LoadError::DecodeFailed(
  366. 366 "bilateral_filter_denoise: 图像尺寸必须大于0",
  367. 367 )
  368. 368 }
  369. 369 if img.channels < 1 || img.channels > 4 {
  370. 370● raise @types.LoadError::DecodeFailed(
  371. 371 "bilateral_filter_denoise: 通道数必须在1-4之间",
  372. 372 )
  373. 373 }
  374. 374 let expected_size = img.width * img.height * img.channels
  375. 375● if img.data.length() != expected_size {
  376. 376● raise @types.LoadError::DecodeFailed(
  377. 377 "bilateral_filter_denoise: 数据大小不匹配",
  378. 378 )
  379. 379 }
  380. 380 if kernel_size < 1 {
  381. 381○ raise @types.LoadError::DecodeFailed(
  382. 382 "bilateral_filter_denoise: kernel_size 必须 >= 1",
  383. 383 )
  384. 384 }
  385. 385 if sigma_color <= 0.0 || sigma_space <= 0.0 {
  386. 386○ raise @types.LoadError::DecodeFailed(
  387. 387 "bilateral_filter_denoise: sigma 参数必须大于0",
  388. 388 )
  389. 389 }
  390. 390 let w = img.width
  391. 391 let h = img.height
  392. 392 let _ = w * h
  393. 393 let out : Array[Byte] = []
  394. 394 let radius = kernel_size / 2
  395. 395
  396. 396 for y in 0..<h {
  397. 397● for x in 0..<w {
  398. 398● let center = img.data[y * w + x].to_double()
  399. 399 let mut sum = 0.0
  400. 400 let mut weight_sum = 0.0
  401. 401 for dy in -radius..<(radius + 1) {
  402. 402● for dx in -radius..<(radius + 1) {
  403. 403● let nx = x + dx
  404. 404 let ny = y + dy
  405. 405 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  406. 406● let current = img.data[ny * w + nx].to_double()
  407. 407● let color_diff = (current - center).abs()
  408. 408● let space_dist = (dx * dx + dy * dy).to_double().sqrt()
  409. 409 // 简化的高斯权重
  410. 410 let color_weight = 1.0 /
  411. 411 (1.0 + color_diff * color_diff / (sigma_color * sigma_color))
  412. 412 let space_weight = 1.0 /
  413. 413 (1.0 + space_dist * space_dist / (sigma_space * sigma_space))
  414. 414 let weight = color_weight * space_weight
  415. 415 sum += current * weight
  416. 416 weight_sum += weight
  417. 417 }
  418. 418 }
  419. 419 }
  420. 420◐ let result = if weight_sum > 0.0 { sum / weight_sum } else { center }
  421. 421 let clamped = if result < 0.0 {
  422. 422○ 0.0
  423. 423● } else if result > 255.0 {
  424. 424○ 255.0
  425. 425 } else {
  426. 426● result
  427. 427 }
  428. 428● out.push(clamped.to_int().to_byte())
  429. 429 }
  430. 430 }
  431. 431
  432. 432 @types.Image::{
  433. 433 width: w,
  434. 434 height: h,
  435. 435 channels: img.channels,
  436. 436● data: Bytes::from_array(out),
  437. 437 }
  438. 438}
  439. 439
  440. 440///|
  441. 441/// 计算噪声水平
  442. 442pub fn calculate_noise_level(
  443. 443 img : @types.Image,
  444. 444) -> Double raise @types.LoadError {
  445. 445 // 输入验证
  446. 446● if img.width <= 0 || img.height <= 0 {
  447. 447● raise @types.LoadError::DecodeFailed(
  448. 448 "calculate_noise_level: 图像尺寸必须大于0",
  449. 449 )
  450. 450 }
  451. 451 if img.channels < 1 || img.channels > 4 {
  452. 452● raise @types.LoadError::DecodeFailed(
  453. 453 "calculate_noise_level: 通道数必须在1-4之间",
  454. 454 )
  455. 455 }
  456. 456 let expected_size = img.width * img.height * img.channels
  457. 457● if img.data.length() != expected_size {
  458. 458● raise @types.LoadError::DecodeFailed(
  459. 459 "calculate_noise_level: 数据大小不匹配",
  460. 460 )
  461. 461 }
  462. 462 let w = img.width
  463. 463 let h = img.height
  464. 464 let total = w * h
  465. 465
  466. 466 // 使用高频分量估计噪声
  467. 467 let mut noise = 0.0
  468. 468 for y in 1..<(h - 1) {
  469. 469● for x in 1..<(w - 1) {
  470. 470● let idx = y * w + x
  471. 471● let center = img.data[idx].to_double()
  472. 472 let avg = (
  473. 473● img.data[idx - 1].to_double() +
  474. 474● img.data[idx + 1].to_double() +
  475. 475● img.data[idx - w].to_double() +
  476. 476● img.data[idx + w].to_double()
  477. 477 ) /
  478. 478 4.0
  479. 479 noise += (center - avg) * (center - avg)
  480. 480 }
  481. 481 }
  482. 482● noise /= total.to_double()
  483. 483● noise.sqrt()
  484. 484}
  485. 485
  486. 486///|
  487. 487/// 直方图均衡化
  488. 488pub fn histogram_equalization_enhance(
  489. 489 img : @types.Image,
  490. 490) -> @types.Image raise @types.LoadError {
  491. 491 // 输入验证
  492. 492● if img.width <= 0 || img.height <= 0 {
  493. 493● raise @types.LoadError::DecodeFailed(
  494. 494 "histogram_equalization_enhance: 图像尺寸必须大于0",
  495. 495 )
  496. 496 }
  497. 497 if img.channels < 1 || img.channels > 4 {
  498. 498● raise @types.LoadError::DecodeFailed(
  499. 499 "histogram_equalization_enhance: 通道数必须在1-4之间",
  500. 500 )
  501. 501 }
  502. 502 let expected_size = img.width * img.height * img.channels
  503. 503● if img.data.length() != expected_size {
  504. 504● raise @types.LoadError::DecodeFailed(
  505. 505 "histogram_equalization_enhance: 数据大小不匹配",
  506. 506 )
  507. 507 }
  508. 508 let w = img.width
  509. 509 let h = img.height
  510. 510 let total = w * h
  511. 511
  512. 512 // 计算直方图
  513. 513 let histogram : Array[Int32] = []
  514. 514 for _ in 0..<256 {
  515. 515● histogram.push(0)
  516. 516 }
  517. 517 for i in 0..<total {
  518. 518● histogram[img.data[i].to_int()] += 1
  519. 519 }
  520. 520
  521. 521 // 累积分布函数
  522. 522 let cdf : Array[Double] = []
  523. 523● let mut cdf_min = total.to_double()
  524. 524 let mut sum = 0.0
  525. 525 for i in 0..<256 {
  526. 526● sum += histogram[i].to_double()
  527. 527● cdf.push(sum)
  528. 528 if histogram[i] > 0 && sum < cdf_min {
  529. 529● cdf_min = sum
  530. 530 }
  531. 531 }
  532. 532
  533. 533 // 直方图均衡化
  534. 534 let out : Array[Byte] = []
  535. 535 for i in 0..<total {
  536. 536● let pixel = img.data[i].to_int()
  537. 537 let equalized = if total > 1 {
  538. 538● ((cdf[pixel] - cdf_min) / (total.to_double() - cdf_min) * 255.0).to_int()
  539. 539 } else {
  540. 540○ pixel
  541. 541 }
  542. 542 let clamped = if equalized < 0 {
  543. 543○ 0
  544. 544● } else if equalized > 255 {
  545. 545○ 255
  546. 546 } else {
  547. 547● equalized
  548. 548 }
  549. 549● out.push(clamped.to_byte())
  550. 550 }
  551. 551
  552. 552 @types.Image::{
  553. 553 width: w,
  554. 554 height: h,
  555. 555 channels: img.channels,
  556. 556● data: Bytes::from_array(out),
  557. 557 }
  558. 558}
  559. 559
  560. 560///|
  561. 561/// Gamma 校正
  562. 562pub fn gamma_correction_enhance(
  563. 563 img : @types.Image,
  564. 564 gamma : Double,
  565. 565) -> @types.Image raise @types.LoadError {
  566. 566 // 输入验证
  567. 567● if img.width <= 0 || img.height <= 0 {
  568. 568● raise @types.LoadError::DecodeFailed(
  569. 569 "gamma_correction_enhance: 图像尺寸必须大于0",
  570. 570 )
  571. 571 }
  572. 572 if img.channels < 1 || img.channels > 4 {
  573. 573● raise @types.LoadError::DecodeFailed(
  574. 574 "gamma_correction_enhance: 通道数必须在1-4之间",
  575. 575 )
  576. 576 }
  577. 577 let expected_size = img.width * img.height * img.channels
  578. 578● if img.data.length() != expected_size {
  579. 579● raise @types.LoadError::DecodeFailed(
  580. 580 "gamma_correction_enhance: 数据大小不匹配",
  581. 581 )
  582. 582 }
  583. 583 let w = img.width
  584. 584 let h = img.height
  585. 585 let total = w * h
  586. 586 let out : Array[Byte] = []
  587. 587
  588. 588 for i in 0..<total {
  589. 589● let pixel = img.data[i].to_double() / 255.0
  590. 590 // 简化的幂运算
  591. 591 let corrected = if gamma > 0.0 {
  592. 592● if gamma == 1.0 {
  593. 593● pixel
  594. 594● } else if gamma > 1.0 {
  595. 595● pixel * pixel // 简化
  596. 596 } else {
  597. 597○ pixel.sqrt() // 简化
  598. 598 }
  599. 599 } else {
  600. 600○ pixel
  601. 601 }
  602. 602 let value = corrected * 255.0
  603. 603 let clamped = if value < 0.0 {
  604. 604○ 0.0
  605. 605● } else if value > 255.0 {
  606. 606○ 255.0
  607. 607 } else {
  608. 608● value
  609. 609 }
  610. 610● out.push(clamped.to_int().to_byte())
  611. 611 }
  612. 612
  613. 613 @types.Image::{
  614. 614 width: w,
  615. 615 height: h,
  616. 616 channels: img.channels,
  617. 617● data: Bytes::from_array(out),
  618. 618 }
  619. 619}
  620. 620
  621. 621///|
  622. 622/// 非锐化掩膜(USM)锐化
  623. 623pub fn unsharp_mask_enhance(
  624. 624 img : @types.Image,
  625. 625 amount : Double,
  626. 626 radius : Int32,
  627. 627) -> @types.Image raise @types.LoadError {
  628. 628 // 输入验证
  629. 629● if img.width <= 0 || img.height <= 0 {
  630. 630● raise @types.LoadError::DecodeFailed(
  631. 631 "unsharp_mask_enhance: 图像尺寸必须大于0",
  632. 632 )
  633. 633 }
  634. 634 if img.channels < 1 || img.channels > 4 {
  635. 635● raise @types.LoadError::DecodeFailed(
  636. 636 "unsharp_mask_enhance: 通道数必须在1-4之间",
  637. 637 )
  638. 638 }
  639. 639 let expected_size = img.width * img.height * img.channels
  640. 640● if img.data.length() != expected_size {
  641. 641● raise @types.LoadError::DecodeFailed(
  642. 642 "unsharp_mask_enhance: 数据大小不匹配",
  643. 643 )
  644. 644 }
  645. 645 // 先高斯模糊
  646. 646● let blurred = gaussian_filter_denoise(img, radius * 2 + 1, radius.to_double())
  647. 647 let w = img.width
  648. 648 let h = img.height
  649. 649 let total = w * h
  650. 650 let out : Array[Byte] = []
  651. 651
  652. 652 for i in 0..<total {
  653. 653● let original = img.data[i].to_double()
  654. 654● let blur = blurred.data[i].to_double()
  655. 655 let sharpened = original + amount * (original - blur)
  656. 656 let clamped = if sharpened < 0.0 {
  657. 657● 0.0
  658. 658● } else if sharpened > 255.0 {
  659. 659● 255.0
  660. 660 } else {
  661. 661● sharpened
  662. 662 }
  663. 663● out.push(clamped.to_int().to_byte())
  664. 664 }
  665. 665
  666. 666 @types.Image::{
  667. 667 width: w,
  668. 668 height: h,
  669. 669 channels: img.channels,
  670. 670● data: Bytes::from_array(out),
  671. 671 }
  672. 672}
  673. 673
  674. 674///|
  675. 675/// 计算图像亮度
  676. 676pub fn calculate_brightness(
  677. 677 img : @types.Image,
  678. 678) -> Double raise @types.LoadError {
  679. 679 // 输入验证
  680. 680● if img.width <= 0 || img.height <= 0 {
  681. 681● raise @types.LoadError::DecodeFailed(
  682. 682 "calculate_brightness: 图像尺寸必须大于0",
  683. 683 )
  684. 684 }
  685. 685 if img.channels < 1 || img.channels > 4 {
  686. 686● raise @types.LoadError::DecodeFailed(
  687. 687 "calculate_brightness: 通道数必须在1-4之间",
  688. 688 )
  689. 689 }
  690. 690 let expected_size = img.width * img.height * img.channels
  691. 691● if img.data.length() != expected_size {
  692. 692● raise @types.LoadError::DecodeFailed(
  693. 693 "calculate_brightness: 数据大小不匹配",
  694. 694 )
  695. 695 }
  696. 696 let w = img.width
  697. 697 let h = img.height
  698. 698 let total = w * h
  699. 699 let mut sum = 0.0
  700. 700 for i in 0..<total {
  701. 701● sum += img.data[i].to_double()
  702. 702 }
  703. 703● sum / total.to_double()
  704. 704}
  705. 705
  706. 706///|
  707. 707/// 计算图像对比度
  708. 708pub fn calculate_contrast(img : @types.Image) -> Double raise @types.LoadError {
  709. 709 // 输入验证
  710. 710● if img.width <= 0 || img.height <= 0 {
  711. 711● raise @types.LoadError::DecodeFailed(
  712. 712 "calculate_contrast: 图像尺寸必须大于0",
  713. 713 )
  714. 714 }
  715. 715 if img.channels < 1 || img.channels > 4 {
  716. 716● raise @types.LoadError::DecodeFailed(
  717. 717 "calculate_contrast: 通道数必须在1-4之间",
  718. 718 )
  719. 719 }
  720. 720 let expected_size = img.width * img.height * img.channels
  721. 721● if img.data.length() != expected_size {
  722. 722● raise @types.LoadError::DecodeFailed(
  723. 723 "calculate_contrast: 数据大小不匹配",
  724. 724 )
  725. 725 }
  726. 726 let w = img.width
  727. 727 let h = img.height
  728. 728 let total = w * h
  729. 729● let mean = calculate_brightness(img)
  730. 730 let mut variance = 0.0
  731. 731 for i in 0..<total {
  732. 732● let diff = img.data[i].to_double() - mean
  733. 733 variance += diff * diff
  734. 734 }
  735. 735● variance /= total.to_double()
  736. 736● variance.sqrt()
  737. 737}
  738. 738
  739. 739///|
  740. 740/// 计算图像锐度
  741. 741pub fn calculate_sharpness(img : @types.Image) -> Double raise @types.LoadError {
  742. 742 // 输入验证
  743. 743● if img.width <= 0 || img.height <= 0 {
  744. 744● raise @types.LoadError::DecodeFailed(
  745. 745 "calculate_sharpness: 图像尺寸必须大于0",
  746. 746 )
  747. 747 }
  748. 748 if img.channels < 1 || img.channels > 4 {
  749. 749● raise @types.LoadError::DecodeFailed(
  750. 750 "calculate_sharpness: 通道数必须在1-4之间",
  751. 751 )
  752. 752 }
  753. 753 let expected_size = img.width * img.height * img.channels
  754. 754● if img.data.length() != expected_size {
  755. 755● raise @types.LoadError::DecodeFailed(
  756. 756 "calculate_sharpness: 数据大小不匹配",
  757. 757 )
  758. 758 }
  759. 759 let w = img.width
  760. 760 let h = img.height
  761. 761 let total = w * h
  762. 762 let mut sharpness = 0.0
  763. 763 for y in 1..<(h - 1) {
  764. 764● for x in 1..<(w - 1) {
  765. 765● let idx = y * w + x
  766. 766● let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double()
  767. 767● let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double()
  768. 768● sharpness += (gx * gx + gy * gy).sqrt()
  769. 769 }
  770. 770 }
  771. 771● sharpness / total.to_double()
  772. 772}
  773. 773
  774. 774///|
  775. 775/// 图像降噪
  776. 776pub fn denoise_image(
  777. 777 img : @types.Image,
  778. 778 config : DenoiseConfig,
  779. 779) -> DenoiseResult raise @types.LoadError {
  780. 780 // 输入验证
  781. 781● if img.width <= 0 || img.height <= 0 {
  782. 782● raise @types.LoadError::DecodeFailed(
  783. 783 "denoise_image: 图像尺寸必须大于0",
  784. 784 )
  785. 785 }
  786. 786 if img.channels < 1 || img.channels > 4 {
  787. 787● raise @types.LoadError::DecodeFailed(
  788. 788 "denoise_image: 通道数必须在1-4之间",
  789. 789 )
  790. 790 }
  791. 791 let expected_size = img.width * img.height * img.channels
  792. 792● if img.data.length() != expected_size {
  793. 793● raise @types.LoadError::DecodeFailed("denoise_image: 数据大小不匹配")
  794. 794 }
  795. 795● let noise_before = calculate_noise_level(img)
  796. 796
  797. 797 let result = if config.algorithm == DenoiseAlgorithm::MeanFilter {
  798. 798● mean_filter_denoise(img, config.kernel_size)
  799. 799● } else if config.algorithm == DenoiseAlgorithm::MedianFilter {
  800. 800● median_filter_denoise(img, config.kernel_size)
  801. 801● } else if config.algorithm == DenoiseAlgorithm::GaussianFilter {
  802. 802● gaussian_filter_denoise(img, config.kernel_size, config.sigma_space)
  803. 803● } else if config.algorithm == DenoiseAlgorithm::BilateralFilter {
  804. 804● bilateral_filter_denoise(
  805. 805 img,
  806. 806 config.kernel_size,
  807. 807 config.sigma_color,
  808. 808 config.sigma_space,
  809. 809 )
  810. 810 } else {
  811. 811● bilateral_filter_denoise(
  812. 812 img,
  813. 813 config.kernel_size,
  814. 814 config.sigma_color,
  815. 815 config.sigma_space,
  816. 816 )
  817. 817 }
  818. 818
  819. 819● let noise_after = calculate_noise_level(result)
  820. 820
  821. 821 // 简化的 PSNR 计算
  822. 822 let w = img.width
  823. 823 let h = img.height
  824. 824 let total = w * h
  825. 825 let mut mse = 0.0
  826. 826 for i in 0..<total {
  827. 827● let diff = img.data[i].to_double() - result.data[i].to_double()
  828. 828 mse += diff * diff
  829. 829 }
  830. 830● mse /= total.to_double()
  831. 831● let psnr = if mse > 0.0 { 10.0 * (65025.0 / mse) * 0.1 } else { 100.0 }
  832. 832
  833. 833 DenoiseResult::{
  834. 834 image: result,
  835. 835 algorithm: config.algorithm,
  836. 836 noise_level_before: noise_before,
  837. 837 noise_level_after: noise_after,
  838. 838 psnr,
  839. 839 ssim: 0.9,
  840. 840 processing_time_ms: 0.0,
  841. 841 strength: config.strength,
  842. 842 }
  843. 843}
  844. 844
  845. 845///|
  846. 846/// 图像增强
  847. 847pub fn enhance_image_advanced(
  848. 848 img : @types.Image,
  849. 849 algorithm : EnhancementAlgorithm,
  850. 850) -> EnhancementResult raise @types.LoadError {
  851. 851 // 输入验证
  852. 852● if img.width <= 0 || img.height <= 0 {
  853. 853● raise @types.LoadError::DecodeFailed(
  854. 854 "enhance_image_advanced: 图像尺寸必须大于0",
  855. 855 )
  856. 856 }
  857. 857 if img.channels < 1 || img.channels > 4 {
  858. 858● raise @types.LoadError::DecodeFailed(
  859. 859 "enhance_image_advanced: 通道数必须在1-4之间",
  860. 860 )
  861. 861 }
  862. 862 let expected_size = img.width * img.height * img.channels
  863. 863● if img.data.length() != expected_size {
  864. 864● raise @types.LoadError::DecodeFailed(
  865. 865 "enhance_image_advanced: 数据大小不匹配",
  866. 866 )
  867. 867 }
  868. 868● let brightness_before = calculate_brightness(img)
  869. 869● let contrast_before = calculate_contrast(img)
  870. 870● let sharpness_before = calculate_sharpness(img)
  871. 871
  872. 872 let result = if algorithm == EnhancementAlgorithm::HistogramEqualization {
  873. 873● histogram_equalization_enhance(img)
  874. 874● } else if algorithm == EnhancementAlgorithm::GammaCorrection {
  875. 875● gamma_correction_enhance(img, 1.5)
  876. 876● } else if algorithm == EnhancementAlgorithm::UnsharpMask {
  877. 877● unsharp_mask_enhance(img, 0.5, 2)
  878. 878 } else {
  879. 879● histogram_equalization_enhance(img)
  880. 880 }
  881. 881
  882. 882● let brightness_after = calculate_brightness(result)
  883. 883● let contrast_after = calculate_contrast(result)
  884. 884● let sharpness_after = calculate_sharpness(result)
  885. 885
  886. 886 EnhancementResult::{
  887. 887 image: result,
  888. 888 algorithm,
  889. 889 brightness_before,
  890. 890 brightness_after,
  891. 891 contrast_before,
  892. 892 contrast_after,
  893. 893 sharpness_before,
  894. 894 sharpness_after,
  895. 895 processing_time_ms: 0.0,
  896. 896 }
  897. 897}
  898. 898
  899. 899///|
  900. 900/// 获取降噪算法名称
  901. 901pub fn denoise_algorithm_to_string(algorithm : DenoiseAlgorithm) -> String {
  902. 902● if algorithm == DenoiseAlgorithm::MeanFilter {
  903. 903● "均值滤波"
  904. 904● } else if algorithm == DenoiseAlgorithm::MedianFilter {
  905. 905● "中值滤波"
  906. 906● } else if algorithm == DenoiseAlgorithm::GaussianFilter {
  907. 907● "高斯滤波"
  908. 908● } else if algorithm == DenoiseAlgorithm::BilateralFilter {
  909. 909● "双边滤波"
  910. 910● } else if algorithm == DenoiseAlgorithm::NonLocalMeans {
  911. 911● "非局部均值"
  912. 912● } else if algorithm == DenoiseAlgorithm::WienerFilter {
  913. 913● "维纳滤波"
  914. 914● } else if algorithm == DenoiseAlgorithm::TotalVariation {
  915. 915● "全变分"
  916. 916● } else if algorithm == DenoiseAlgorithm::WaveletDenoise {
  917. 917● "小波降噪"
  918. 918● } else if algorithm == DenoiseAlgorithm::BM3D {
  919. 919● "BM3D"
  920. 920● } else if algorithm == DenoiseAlgorithm::DeepLearningDenoise {
  921. 921● "深度学习降噪"
  922. 922 } else {
  923. 923● "未知"
  924. 924 }
  925. 925}
  926. 926
  927. 927///|
  928. 928/// 生成降噪报告
  929. 929pub fn denoise_report_to_string(result : DenoiseResult) -> String {
  930. 930● let mut report = "## 图像降噪报告\n\n"
  931. 931 report = report + "### 降噪信息\n\n"
  932. 932 report = report + "| 指标 | 值 |\n|------|------|\n"
  933. 933 report = report +
  934. 934 "| 算法 | " +
  935. 935● denoise_algorithm_to_string(result.algorithm) +
  936. 936 " |\n"
  937. 937 report = report +
  938. 938 "| 降噪前噪声水平 | " +
  939. 939● result.noise_level_before.to_string() +
  940. 940 " |\n"
  941. 941 report = report +
  942. 942 "| 降噪后噪声水平 | " +
  943. 943● result.noise_level_after.to_string() +
  944. 944 " |\n"
  945. 945 report = report +
  946. 946 "| 噪声降低 | " +
  947. 947 ((result.noise_level_before - result.noise_level_after) *
  948. 948 100.0 /
  949. 949● result.noise_level_before).to_string() +
  950. 950 "% |\n"
  951. 951● report = report + "| PSNR | " + result.psnr.to_string() + " dB |\n"
  952. 952● report = report + "| SSIM | " + result.ssim.to_string() + " |\n"
  953. 953● report = report + "| 降噪强度 | " + result.strength.to_string() + " |\n"
  954. 954
  955. 955 report
  956. 956}