image.photography\superres\super_resolution_enhancement.mbt

90.3% · 233 / 258 points · 711 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/// # 超分辨率增强 superres/super_resolution_enhancement
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像超分辨率增强(v23.0.0)
  7. 7// 图像超分辨率、插值算法、图像增强、质量评估
  8. 8
  9. 9///|
  10. 10/// 超分辨率算法类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum SuperResolutionAlgorithm {
  15. 15 NearestNeighbor
  16. 16 Bilinear
  17. 17 Bicubic
  18. 18 Lanczos
  19. 19 EdgeDirected
  20. 20 ExampleBased
  21. 21 DeepLearning
  22. 22 Unknown
  23. 23}
  24. 24
  25. 25///|
  26. 26pub extend SuperResolutionAlgorithm with @debug.Debug::{to_repr}
  27. 27
  28. 28///|
  29. 29pub extend SuperResolutionAlgorithm with Eq::{not_equal, equal}
  30. 30
  31. 31///|
  32. 32/// 超分辨率结果
  33. 33#derive(Eq, @debug.Debug)
  34. 34pub(all) struct SuperResolutionResult {
  35. 35 image : @types.Image
  36. 36 original_width : Int32
  37. 37 original_height : Int32
  38. 38 output_width : Int32
  39. 39 output_height : Int32
  40. 40 scale_factor : Double
  41. 41 algorithm : SuperResolutionAlgorithm
  42. 42 processing_time_ms : Double
  43. 43 psnr : Double
  44. 44 ssim : Double
  45. 45}
  46. 46
  47. 47///|
  48. 48pub extend SuperResolutionResult with Eq::{not_equal, equal}
  49. 49
  50. 50///|
  51. 51pub extend SuperResolutionResult with @debug.Debug::{to_repr}
  52. 52
  53. 53///|
  54. 54/// 插值配置
  55. 55#derive(Eq, @debug.Debug)
  56. 56pub(all) struct InterpolationConfig {
  57. 57 algorithm : SuperResolutionAlgorithm
  58. 58 scale_factor : Double
  59. 59 use_edge_preservation : Bool
  60. 60 sharpening_strength : Double
  61. 61 denoise_strength : Double
  62. 62}
  63. 63
  64. 64///|
  65. 65pub extend InterpolationConfig with Eq::{not_equal, equal}
  66. 66
  67. 67///|
  68. 68pub extend InterpolationConfig with @debug.Debug::{to_repr}
  69. 69
  70. 70///|
  71. 71/// 图像质量指标
  72. 72#derive(Eq, @debug.Debug)
  73. 73pub(all) struct ImageQualityMetrics {
  74. 74 psnr : Double
  75. 75 ssim : Double
  76. 76 mse : Double
  77. 77 mae : Double
  78. 78 correlation : Double
  79. 79 sharpness : Double
  80. 80 noise_level : Double
  81. 81 overall_quality : Double
  82. 82}
  83. 83
  84. 84///|
  85. 85pub extend ImageQualityMetrics with Eq::{not_equal, equal}
  86. 86
  87. 87///|
  88. 88pub extend ImageQualityMetrics with @debug.Debug::{to_repr}
  89. 89
  90. 90///|
  91. 91/// 图像增强配置
  92. 92#derive(Eq, @debug.Debug)
  93. 93pub(all) struct EnhancementConfig {
  94. 94 brightness : Double
  95. 95 contrast : Double
  96. 96 saturation : Double
  97. 97 sharpness : Double
  98. 98 denoise : Double
  99. 99 gamma : Double
  100. 100 use_clahe : Bool
  101. 101 clahe_clip_limit : Double
  102. 102 clahe_tile_size : Int32
  103. 103}
  104. 104
  105. 105///|
  106. 106pub extend EnhancementConfig with Eq::{not_equal, equal}
  107. 107
  108. 108///|
  109. 109pub extend EnhancementConfig with @debug.Debug::{to_repr}
  110. 110
  111. 111///|
  112. 112/// 创建默认插值配置
  113. 113pub fn create_default_interpolation_config() -> InterpolationConfig {
  114. 114● InterpolationConfig::{
  115. 115 algorithm: SuperResolutionAlgorithm::Bicubic,
  116. 116 scale_factor: 2.0,
  117. 117 use_edge_preservation: true,
  118. 118 sharpening_strength: 0.3,
  119. 119 denoise_strength: 0.1,
  120. 120 }
  121. 121}
  122. 122
  123. 123///|
  124. 124/// 创建默认增强配置
  125. 125pub fn create_default_enhancement_config() -> EnhancementConfig {
  126. 126● EnhancementConfig::{
  127. 127 brightness: 0.0,
  128. 128 contrast: 1.0,
  129. 129 saturation: 1.0,
  130. 130 sharpness: 0.0,
  131. 131 denoise: 0.0,
  132. 132 gamma: 1.0,
  133. 133 use_clahe: false,
  134. 134 clahe_clip_limit: 2.0,
  135. 135 clahe_tile_size: 8,
  136. 136 }
  137. 137}
  138. 138
  139. 139///|
  140. 140/// 最近邻插值
  141. 141pub fn nearest_neighbor_interpolate(
  142. 142 img : @types.Image,
  143. 143 scale_factor : Double,
  144. 144) -> @types.Image {
  145. 145● let w = img.width
  146. 146 let h = img.height
  147. 147● let new_w = (w.to_double() * scale_factor).to_int()
  148. 148● let new_h = (h.to_double() * scale_factor).to_int()
  149. 149 let out : Array[Byte] = []
  150. 150
  151. 151 for y in 0..<new_h {
  152. 152● for x in 0..<new_w {
  153. 153● let src_x = (x.to_double() / scale_factor).to_int()
  154. 154● let src_y = (y.to_double() / scale_factor).to_int()
  155. 155 let clamped_x = if src_x < 0 {
  156. 156○ 0
  157. 157● } else if src_x >= w {
  158. 158○ w - 1
  159. 159 } else {
  160. 160● src_x
  161. 161 }
  162. 162 let clamped_y = if src_y < 0 {
  163. 163○ 0
  164. 164● } else if src_y >= h {
  165. 165○ h - 1
  166. 166 } else {
  167. 167● src_y
  168. 168 }
  169. 169● out.push(img.data[clamped_y * w + clamped_x])
  170. 170 }
  171. 171 }
  172. 172
  173. 173 @types.Image::{
  174. 174 width: new_w,
  175. 175 height: new_h,
  176. 176 channels: img.channels,
  177. 177● data: Bytes::from_array(out),
  178. 178 }
  179. 179}
  180. 180
  181. 181///|
  182. 182/// 双线性插值
  183. 183pub fn bilinear_interpolate(
  184. 184 img : @types.Image,
  185. 185 scale_factor : Double,
  186. 186) -> @types.Image {
  187. 187● let w = img.width
  188. 188 let h = img.height
  189. 189● let new_w = (w.to_double() * scale_factor).to_int()
  190. 190● let new_h = (h.to_double() * scale_factor).to_int()
  191. 191 let out : Array[Byte] = []
  192. 192
  193. 193 for y in 0..<new_h {
  194. 194● for x in 0..<new_w {
  195. 195● let src_x = x.to_double() / scale_factor
  196. 196● let src_y = y.to_double() / scale_factor
  197. 197● let x0 = src_x.to_int()
  198. 198● let y0 = src_y.to_int()
  199. 199 let x1 = x0 + 1
  200. 200 let y1 = y0 + 1
  201. 201● let fx = src_x - x0.to_double()
  202. 202● let fy = src_y - y0.to_double()
  203. 203
  204. 204◐ let cx0 = if x0 < 0 { 0 } else if x0 >= w { w - 1 } else { x0 }
  205. 205◐ let cy0 = if y0 < 0 { 0 } else if y0 >= h { h - 1 } else { y0 }
  206. 206◐ let cx1 = if x1 < 0 { 0 } else if x1 >= w { w - 1 } else { x1 }
  207. 207◐ let cy1 = if y1 < 0 { 0 } else if y1 >= h { h - 1 } else { y1 }
  208. 208
  209. 209● let p00 = img.data[cy0 * w + cx0].to_double()
  210. 210● let p01 = img.data[cy0 * w + cx1].to_double()
  211. 211● let p10 = img.data[cy1 * w + cx0].to_double()
  212. 212● let p11 = img.data[cy1 * w + cx1].to_double()
  213. 213
  214. 214 let top = p00 * (1.0 - fx) + p01 * fx
  215. 215 let bottom = p10 * (1.0 - fx) + p11 * fx
  216. 216 let value = top * (1.0 - fy) + bottom * fy
  217. 217 let clamped = if value < 0.0 {
  218. 218○ 0.0
  219. 219● } else if value > 255.0 {
  220. 220○ 255.0
  221. 221 } else {
  222. 222● value
  223. 223 }
  224. 224● out.push(clamped.to_int().to_byte())
  225. 225 }
  226. 226 }
  227. 227
  228. 228 @types.Image::{
  229. 229 width: new_w,
  230. 230 height: new_h,
  231. 231 channels: img.channels,
  232. 232● data: Bytes::from_array(out),
  233. 233 }
  234. 234}
  235. 235
  236. 236///|
  237. 237/// 双三次插值(简化版)
  238. 238pub fn bicubic_interpolate(
  239. 239 img : @types.Image,
  240. 240 scale_factor : Double,
  241. 241) -> @types.Image {
  242. 242● let w = img.width
  243. 243 let h = img.height
  244. 244● let new_w = (w.to_double() * scale_factor).to_int()
  245. 245● let new_h = (h.to_double() * scale_factor).to_int()
  246. 246 let out : Array[Byte] = []
  247. 247
  248. 248 for y in 0..<new_h {
  249. 249● for x in 0..<new_w {
  250. 250● let src_x = x.to_double() / scale_factor
  251. 251● let src_y = y.to_double() / scale_factor
  252. 252● let x0 = src_x.to_int() - 1
  253. 253● let y0 = src_y.to_int() - 1
  254. 254● let fx = src_x - src_x.to_int().to_double()
  255. 255● let fy = src_y - src_y.to_int().to_double()
  256. 256
  257. 257 // 简化为双线性(双三次需要更多计算)
  258. 258◐ let cx0 = if x0 < 0 { 0 } else if x0 >= w { w - 1 } else { x0 }
  259. 259◐ let cy0 = if y0 < 0 { 0 } else if y0 >= h { h - 1 } else { y0 }
  260. 260● let cx1 = if cx0 + 1 >= w { w - 1 } else { cx0 + 1 }
  261. 261● let cy1 = if cy0 + 1 >= h { h - 1 } else { cy0 + 1 }
  262. 262
  263. 263● let p00 = img.data[cy0 * w + cx0].to_double()
  264. 264● let p01 = img.data[cy0 * w + cx1].to_double()
  265. 265● let p10 = img.data[cy1 * w + cx0].to_double()
  266. 266● let p11 = img.data[cy1 * w + cx1].to_double()
  267. 267
  268. 268 let top = p00 * (1.0 - fx) + p01 * fx
  269. 269 let bottom = p10 * (1.0 - fx) + p11 * fx
  270. 270 let value = top * (1.0 - fy) + bottom * fy
  271. 271 let clamped = if value < 0.0 {
  272. 272○ 0.0
  273. 273● } else if value > 255.0 {
  274. 274○ 255.0
  275. 275 } else {
  276. 276● value
  277. 277 }
  278. 278● out.push(clamped.to_int().to_byte())
  279. 279 }
  280. 280 }
  281. 281
  282. 282 @types.Image::{
  283. 283 width: new_w,
  284. 284 height: new_h,
  285. 285 channels: img.channels,
  286. 286● data: Bytes::from_array(out),
  287. 287 }
  288. 288}
  289. 289
  290. 290///|
  291. 291/// 图像锐化(USM 非锐化掩膜)
  292. 292pub fn sharpen_image(img : @types.Image, strength : Double) -> @types.Image {
  293. 293● let w = img.width
  294. 294 let h = img.height
  295. 295 let out : Array[Byte] = []
  296. 296
  297. 297 // 简化的锐化:中心加权
  298. 298 for y in 0..<h {
  299. 299● for x in 0..<w {
  300. 300● let center = img.data[y * w + x].to_double()
  301. 301 let mut sum = 0.0
  302. 302 let mut count = 0
  303. 303 for dy in -1..<2 {
  304. 304● for dx in -1..<2 {
  305. 305● if dx == 0 && dy == 0 {
  306. 306● continue
  307. 307 }
  308. 308 let nx = x + dx
  309. 309 let ny = y + dy
  310. 310 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  311. 311● sum += img.data[ny * w + nx].to_double()
  312. 312 count += 1
  313. 313 }
  314. 314 }
  315. 315 }
  316. 316◐ let avg = if count > 0 { sum / count.to_double() } else { center }
  317. 317 let sharpened = center + strength * (center - avg)
  318. 318 let clamped = if sharpened < 0.0 {
  319. 319● 0.0
  320. 320● } else if sharpened > 255.0 {
  321. 321○ 255.0
  322. 322 } else {
  323. 323● sharpened
  324. 324 }
  325. 325● out.push(clamped.to_int().to_byte())
  326. 326 }
  327. 327 }
  328. 328
  329. 329 @types.Image::{
  330. 330 width: w,
  331. 331 height: h,
  332. 332 channels: img.channels,
  333. 333● data: Bytes::from_array(out),
  334. 334 }
  335. 335}
  336. 336
  337. 337///|
  338. 338/// 图像去噪(均值滤波)
  339. 339pub fn denoise_image(img : @types.Image, strength : Double) -> @types.Image {
  340. 340● let w = img.width
  341. 341 let h = img.height
  342. 342 let out : Array[Byte] = []
  343. 343● let kernel_size = if strength > 0.5 { 3 } else { 1 }
  344. 344
  345. 345 for y in 0..<h {
  346. 346● for x in 0..<w {
  347. 347● if kernel_size <= 1 {
  348. 348● out.push(img.data[y * w + x])
  349. 349 } else {
  350. 350● let mut sum = 0.0
  351. 351 let mut count = 0
  352. 352 for dy in -1..<2 {
  353. 353● for dx in -1..<2 {
  354. 354● let nx = x + dx
  355. 355 let ny = y + dy
  356. 356 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  357. 357● sum += img.data[ny * w + nx].to_double()
  358. 358 count += 1
  359. 359 }
  360. 360 }
  361. 361 }
  362. 362 let avg = if count > 0 {
  363. 363● sum / count.to_double()
  364. 364 } else {
  365. 365○ img.data[y * w + x].to_double()
  366. 366 }
  367. 367● let original = img.data[y * w + x].to_double()
  368. 368 let blended = original * (1.0 - strength) + avg * strength
  369. 369● out.push(blended.to_int().to_byte())
  370. 370 }
  371. 371 }
  372. 372 }
  373. 373
  374. 374 @types.Image::{
  375. 375 width: w,
  376. 376 height: h,
  377. 377 channels: img.channels,
  378. 378● data: Bytes::from_array(out),
  379. 379 }
  380. 380}
  381. 381
  382. 382///|
  383. 383/// 超分辨率处理
  384. 384pub fn super_resolve(
  385. 385 img : @types.Image,
  386. 386 config : InterpolationConfig,
  387. 387) -> SuperResolutionResult {
  388. 388● let mut result = if config.algorithm ==
  389. 389 SuperResolutionAlgorithm::NearestNeighbor {
  390. 390● nearest_neighbor_interpolate(img, config.scale_factor)
  391. 391● } else if config.algorithm == SuperResolutionAlgorithm::Bilinear {
  392. 392● bilinear_interpolate(img, config.scale_factor)
  393. 393● } else if config.algorithm == SuperResolutionAlgorithm::Bicubic {
  394. 394● bicubic_interpolate(img, config.scale_factor)
  395. 395 } else {
  396. 396● bicubic_interpolate(img, config.scale_factor)
  397. 397 }
  398. 398
  399. 399 // 去噪
  400. 400 if config.denoise_strength > 0.0 {
  401. 401● result = denoise_image(result, config.denoise_strength)
  402. 402 }
  403. 403
  404. 404 // 锐化
  405. 405 if config.sharpening_strength > 0.0 {
  406. 406● result = sharpen_image(result, config.sharpening_strength)
  407. 407 }
  408. 408
  409. 409 SuperResolutionResult::{
  410. 410 image: result,
  411. 411 original_width: img.width,
  412. 412 original_height: img.height,
  413. 413 output_width: result.width,
  414. 414 output_height: result.height,
  415. 415 scale_factor: config.scale_factor,
  416. 416 algorithm: config.algorithm,
  417. 417 processing_time_ms: 0.0,
  418. 418 psnr: 0.0,
  419. 419 ssim: 0.0,
  420. 420 }
  421. 421}
  422. 422
  423. 423///|
  424. 424/// 计算 PSNR
  425. 425pub fn calculate_psnr_sr(
  426. 426 original : @types.Image,
  427. 427 processed : @types.Image,
  428. 428) -> Double {
  429. 429● let w = original.width
  430. 430 let h = original.height
  431. 431 let total = w * h
  432. 432 let mut mse = 0.0
  433. 433
  434. 434 for i in 0..<total {
  435. 435● let p1 = original.data[i].to_double()
  436. 436● let p2 = processed.data[i].to_double()
  437. 437 let diff = p1 - p2
  438. 438 mse += diff * diff
  439. 439 }
  440. 440● mse /= total.to_double()
  441. 441
  442. 442 if mse > 0.0 {
  443. 443● 10.0 * (65025.0 / mse) * 0.1
  444. 444 } else {
  445. 445● 100.0
  446. 446 }
  447. 447}
  448. 448
  449. 449///|
  450. 450/// 计算 SSIM(结构相似性,简化版)
  451. 451pub fn calculate_ssim_sr(
  452. 452 original : @types.Image,
  453. 453 processed : @types.Image,
  454. 454) -> Double {
  455. 455● let w = original.width
  456. 456 let h = original.height
  457. 457 let total = w * h
  458. 458 let mut sum1 = 0.0
  459. 459 let mut sum2 = 0.0
  460. 460 let mut sum12 = 0.0
  461. 461 let mut sum1_sq = 0.0
  462. 462 let mut sum2_sq = 0.0
  463. 463
  464. 464 for i in 0..<total {
  465. 465● let p1 = original.data[i].to_double()
  466. 466● let p2 = processed.data[i].to_double()
  467. 467 sum1 += p1
  468. 468 sum2 += p2
  469. 469 sum12 += p1 * p2
  470. 470 sum1_sq += p1 * p1
  471. 471 sum2_sq += p2 * p2
  472. 472 }
  473. 473
  474. 474● let mean1 = sum1 / total.to_double()
  475. 475● let mean2 = sum2 / total.to_double()
  476. 476● let var1 = sum1_sq / total.to_double() - mean1 * mean1
  477. 477● let var2 = sum2_sq / total.to_double() - mean2 * mean2
  478. 478● let cov12 = sum12 / total.to_double() - mean1 * mean2
  479. 479
  480. 480 let c1 = 6.5025
  481. 481 let c2 = 58.5225
  482. 482 let numerator = (2.0 * mean1 * mean2 + c1) * (2.0 * cov12 + c2)
  483. 483 let denominator = (mean1 * mean1 + mean2 * mean2 + c1) * (var1 + var2 + c2)
  484. 484
  485. 485 if denominator > 0.0 {
  486. 486● numerator / denominator
  487. 487 } else {
  488. 488○ 1.0
  489. 489 }
  490. 490}
  491. 491
  492. 492///|
  493. 493/// 计算图像质量指标
  494. 494pub fn calculate_image_quality_metrics(
  495. 495 original : @types.Image,
  496. 496 processed : @types.Image,
  497. 497) -> ImageQualityMetrics {
  498. 498● let psnr = calculate_psnr_sr(original, processed)
  499. 499● let ssim = calculate_ssim_sr(original, processed)
  500. 500
  501. 501 let w = original.width
  502. 502 let h = original.height
  503. 503 let total = w * h
  504. 504 let mut mse = 0.0
  505. 505 let mut mae = 0.0
  506. 506 let mut sum1 = 0.0
  507. 507 let mut sum2 = 0.0
  508. 508 let mut sum12 = 0.0
  509. 509
  510. 510 for i in 0..<total {
  511. 511● let p1 = original.data[i].to_double()
  512. 512● let p2 = processed.data[i].to_double()
  513. 513 let diff = p1 - p2
  514. 514 mse += diff * diff
  515. 515● mae += diff.abs()
  516. 516 sum1 += p1
  517. 517 sum2 += p2
  518. 518 sum12 += p1 * p2
  519. 519 }
  520. 520● mse /= total.to_double()
  521. 521● mae /= total.to_double()
  522. 522
  523. 523● let mean1 = sum1 / total.to_double()
  524. 524● let mean2 = sum2 / total.to_double()
  525. 525● let cov = sum12 / total.to_double() - mean1 * mean2
  526. 526 let var1 = if total > 0 {
  527. 527● let mut v = 0.0
  528. 528 for i in 0..<total {
  529. 529● let p = original.data[i].to_double()
  530. 530 v += (p - mean1) * (p - mean1)
  531. 531 }
  532. 532● v / total.to_double()
  533. 533 } else {
  534. 534○ 0.0
  535. 535 }
  536. 536 let var2 = if total > 0 {
  537. 537● let mut v = 0.0
  538. 538 for i in 0..<total {
  539. 539● let p = processed.data[i].to_double()
  540. 540 v += (p - mean2) * (p - mean2)
  541. 541 }
  542. 542● v / total.to_double()
  543. 543 } else {
  544. 544○ 0.0
  545. 545 }
  546. 546 let correlation = if var1 > 0.0 && var2 > 0.0 {
  547. 547● cov / (var1 * var2).sqrt()
  548. 548 } else {
  549. 549● 0.0
  550. 550 }
  551. 551
  552. 552 // 清晰度(基于梯度)
  553. 553 let mut sharpness = 0.0
  554. 554 for y in 1..<(h - 1) {
  555. 555● for x in 1..<(w - 1) {
  556. 556● let idx = y * w + x
  557. 557● let gx = processed.data[idx + 1].to_double() -
  558. 558● processed.data[idx - 1].to_double()
  559. 559● let gy = processed.data[idx + w].to_double() -
  560. 560● processed.data[idx - w].to_double()
  561. 561● sharpness += (gx * gx + gy * gy).sqrt()
  562. 562 }
  563. 563 }
  564. 564● sharpness /= total.to_double()
  565. 565
  566. 566● let noise_level = mse.sqrt()
  567. 567 let overall_quality = if psnr > 40.0 && ssim > 0.95 {
  568. 568● 5.0
  569. 569● } else if psnr > 30.0 && ssim > 0.9 {
  570. 570○ 4.0
  571. 571● } else if psnr > 20.0 && ssim > 0.8 {
  572. 572○ 3.0
  573. 573 } else {
  574. 574● 2.0
  575. 575 }
  576. 576
  577. 577 ImageQualityMetrics::{
  578. 578 psnr,
  579. 579 ssim,
  580. 580 mse,
  581. 581 mae,
  582. 582 correlation,
  583. 583 sharpness,
  584. 584 noise_level,
  585. 585 overall_quality,
  586. 586 }
  587. 587}
  588. 588
  589. 589///|
  590. 590/// 图像增强
  591. 591pub fn enhance_image(
  592. 592 img : @types.Image,
  593. 593 config : EnhancementConfig,
  594. 594) -> @types.Image {
  595. 595● let w = img.width
  596. 596 let h = img.height
  597. 597 let total = w * h
  598. 598 let out : Array[Byte] = []
  599. 599
  600. 600 for i in 0..<total {
  601. 601● let mut pixel = img.data[i].to_double()
  602. 602
  603. 603 // 亮度
  604. 604 pixel += config.brightness * 255.0
  605. 605
  606. 606 // 对比度
  607. 607 pixel = (pixel - 128.0) * config.contrast + 128.0
  608. 608
  609. 609 // Gamma
  610. 610 if config.gamma != 1.0 && config.gamma > 0.0 {
  611. 611● pixel = 255.0 * (pixel / 255.0) * (1.0 / config.gamma)
  612. 612 }
  613. 613
  614. 614 let clamped = if pixel < 0.0 {
  615. 615● 0.0
  616. 616● } else if pixel > 255.0 {
  617. 617● 255.0
  618. 618 } else {
  619. 619● pixel
  620. 620 }
  621. 621● out.push(clamped.to_int().to_byte())
  622. 622 }
  623. 623
  624. 624 let mut result = @types.Image::{
  625. 625 width: w,
  626. 626 height: h,
  627. 627 channels: img.channels,
  628. 628● data: Bytes::from_array(out),
  629. 629 }
  630. 630
  631. 631 // 去噪
  632. 632 if config.denoise > 0.0 {
  633. 633● result = denoise_image(result, config.denoise)
  634. 634 }
  635. 635
  636. 636 // 锐化
  637. 637 if config.sharpness > 0.0 {
  638. 638● result = sharpen_image(result, config.sharpness)
  639. 639 }
  640. 640
  641. 641 result
  642. 642}
  643. 643
  644. 644///|
  645. 645/// 生成超分辨率报告
  646. 646pub fn super_resolution_report_to_string(
  647. 647 result : SuperResolutionResult,
  648. 648 metrics : ImageQualityMetrics,
  649. 649) -> String {
  650. 650● let mut report = "## 图像超分辨率报告\n\n"
  651. 651 report = report + "### 处理信息\n\n"
  652. 652 report = report + "| 指标 | 值 |\n|------|------|\n"
  653. 653 report = report +
  654. 654 "| 原始尺寸 | " +
  655. 655● result.original_width.to_string() +
  656. 656 "x" +
  657. 657● result.original_height.to_string() +
  658. 658 " |\n"
  659. 659 report = report +
  660. 660 "| 输出尺寸 | " +
  661. 661● result.output_width.to_string() +
  662. 662 "x" +
  663. 663● result.output_height.to_string() +
  664. 664 " |\n"
  665. 665 report = report +
  666. 666 "| 缩放因子 | " +
  667. 667● result.scale_factor.to_string() +
  668. 668 "x |\n"
  669. 669 let algo_str = if result.algorithm ==
  670. 670 SuperResolutionAlgorithm::NearestNeighbor {
  671. 671● "最近邻"
  672. 672● } else if result.algorithm == SuperResolutionAlgorithm::Bilinear {
  673. 673● "双线性"
  674. 674● } else if result.algorithm == SuperResolutionAlgorithm::Bicubic {
  675. 675● "双三次"
  676. 676● } else if result.algorithm == SuperResolutionAlgorithm::Lanczos {
  677. 677● "Lanczos"
  678. 678● } else if result.algorithm == SuperResolutionAlgorithm::EdgeDirected {
  679. 679● "边缘导向"
  680. 680● } else if result.algorithm == SuperResolutionAlgorithm::ExampleBased {
  681. 681● "基于示例"
  682. 682● } else if result.algorithm == SuperResolutionAlgorithm::DeepLearning {
  683. 683● "深度学习"
  684. 684 } else {
  685. 685● "未知"
  686. 686 }
  687. 687 report = report + "| 算法 | " + algo_str + " |\n"
  688. 688 report = report +
  689. 689 "| 处理耗时 | " +
  690. 690● result.processing_time_ms.to_string() +
  691. 691 "ms |\n\n"
  692. 692
  693. 693 report = report + "### 质量评估\n\n"
  694. 694 report = report + "| 指标 | 值 |\n|------|------|\n"
  695. 695● report = report + "| PSNR | " + metrics.psnr.to_string() + " dB |\n"
  696. 696● report = report + "| SSIM | " + metrics.ssim.to_string() + " |\n"
  697. 697● report = report + "| MSE | " + metrics.mse.to_string() + " |\n"
  698. 698● report = report + "| MAE | " + metrics.mae.to_string() + " |\n"
  699. 699● report = report + "| 相关性 | " + metrics.correlation.to_string() + " |\n"
  700. 700● report = report + "| 清晰度 | " + metrics.sharpness.to_string() + " |\n"
  701. 701 report = report +
  702. 702 "| 噪声水平 | " +
  703. 703● metrics.noise_level.to_string() +
  704. 704 " |\n"
  705. 705 report = report +
  706. 706 "| 综合质量 | " +
  707. 707● metrics.overall_quality.to_string() +
  708. 708 "/5.0 |\n"
  709. 709
  710. 710 report
  711. 711}