image.photography\computational_imaging\computational_imaging.mbt

90.3% · 168 / 186 points · 829 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/// # 计算成像 computational_imaging/computational_imaging
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 计算成像与光场成像(v37.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 ComputationalImagingAlgorithm {
  15. 15 LightField
  16. 16 Holography
  17. 17 ComputedTomography
  18. 18 PhaseRetrieval
  19. 19 CodedAperture
  20. 20 SinglePixel
  21. 21 GhostImaging
  22. 22 Ptychography
  23. 23 Unknown
  24. 24}
  25. 25
  26. 26///|
  27. 27pub extend ComputationalImagingAlgorithm with @debug.Debug::{to_repr}
  28. 28
  29. 29///|
  30. 30pub extend ComputationalImagingAlgorithm with Eq::{not_equal, equal}
  31. 31
  32. 32///|
  33. 33/// 光场表示类型
  34. 34/// `#non_exhaustive` 表示未来可能添加新构造器
  35. 35#derive(Eq, @debug.Debug)
  36. 36#non_exhaustive
  37. 37pub(all) enum LightFieldRepresentation {
  38. 38 TwoPlane
  39. 39 Lumigraph
  40. 40 FourierSlice
  41. 41 Convolutional
  42. 42 Unknown
  43. 43}
  44. 44
  45. 45///|
  46. 46pub extend LightFieldRepresentation with @debug.Debug::{to_repr}
  47. 47
  48. 48///|
  49. 49pub extend LightFieldRepresentation with Eq::{not_equal, equal}
  50. 50
  51. 51///|
  52. 52/// 全息图类型
  53. 53/// `#non_exhaustive` 表示未来可能添加新构造器
  54. 54#derive(Eq, @debug.Debug)
  55. 55#non_exhaustive
  56. 56pub(all) enum HologramType {
  57. 57 Fresnel
  58. 58 Fourier
  59. 59 ImagePlane
  60. 60 Digital
  61. 61 ComputerGenerated
  62. 62 Unknown
  63. 63}
  64. 64
  65. 65///|
  66. 66pub extend HologramType with @debug.Debug::{to_repr}
  67. 67
  68. 68///|
  69. 69pub extend HologramType with Eq::{not_equal, equal}
  70. 70
  71. 71///|
  72. 72/// 光场相机参数
  73. 73#derive(Eq, @debug.Debug)
  74. 74pub(all) struct LightFieldCameraParams {
  75. 75 num_views_x : Int32
  76. 76 num_views_y : Int32
  77. 77 micro_lens_pitch : Double
  78. 78 focal_length : Double
  79. 79 sensor_pitch : Double
  80. 80 num_aperture_samples : Int32
  81. 81}
  82. 82
  83. 83///|
  84. 84pub extend LightFieldCameraParams with Eq::{not_equal, equal}
  85. 85
  86. 86///|
  87. 87pub extend LightFieldCameraParams with @debug.Debug::{to_repr}
  88. 88
  89. 89///|
  90. 90/// 光场数据
  91. 91#derive(Eq, @debug.Debug)
  92. 92pub(all) struct LightFieldData {
  93. 93 views : Array[@types.Image]
  94. 94 num_views_x : Int32
  95. 95 num_views_y : Int32
  96. 96 total_views : Int32
  97. 97 representation : LightFieldRepresentation
  98. 98}
  99. 99
  100. 100///|
  101. 101pub extend LightFieldData with Eq::{not_equal, equal}
  102. 102
  103. 103///|
  104. 104pub extend LightFieldData with @debug.Debug::{to_repr}
  105. 105
  106. 106///|
  107. 107/// 全息图
  108. 108#derive(Eq, @debug.Debug)
  109. 109pub(all) struct Hologram {
  110. 110 intensity : @types.Image
  111. 111 phase : @types.Image
  112. 112 hologram_type : HologramType
  113. 113 wavelength : Double
  114. 114 distance : Double
  115. 115 pixel_size : Double
  116. 116}
  117. 117
  118. 118///|
  119. 119pub extend Hologram with Eq::{not_equal, equal}
  120. 120
  121. 121///|
  122. 122pub extend Hologram with @debug.Debug::{to_repr}
  123. 123
  124. 124///|
  125. 125/// CT 投影数据
  126. 126#derive(Eq, @debug.Debug)
  127. 127pub(all) struct CTProjectionData {
  128. 128 projections : Array[@types.Image]
  129. 129 num_angles : Int32
  130. 130 num_detectors : Int32
  131. 131 angular_range : Double
  132. 132 source_distance : Double
  133. 133 detector_distance : Double
  134. 134}
  135. 135
  136. 136///|
  137. 137pub extend CTProjectionData with Eq::{not_equal, equal}
  138. 138
  139. 139///|
  140. 140pub extend CTProjectionData with @debug.Debug::{to_repr}
  141. 141
  142. 142///|
  143. 143/// 相位恢复配置
  144. 144#derive(Eq, @debug.Debug)
  145. 145pub(all) struct PhaseRetrievalConfig {
  146. 146 num_iterations : Int32
  147. 147 algorithm : String
  148. 148 support_threshold : Double
  149. 149 use_hybrid_input_output : Bool
  150. 150 beta : Double
  151. 151}
  152. 152
  153. 153///|
  154. 154pub extend PhaseRetrievalConfig with Eq::{not_equal, equal}
  155. 155
  156. 156///|
  157. 157pub extend PhaseRetrievalConfig with @debug.Debug::{to_repr}
  158. 158
  159. 159///|
  160. 160/// 编码孔径模式
  161. 161#derive(Eq, @debug.Debug)
  162. 162pub(all) struct CodedAperturePattern {
  163. 163 pattern : @types.Image
  164. 164 pattern_size : Int32
  165. 165 code_type : String
  166. 166 num_elements : Int32
  167. 167}
  168. 168
  169. 169///|
  170. 170pub extend CodedAperturePattern with Eq::{not_equal, equal}
  171. 171
  172. 172///|
  173. 173pub extend CodedAperturePattern with @debug.Debug::{to_repr}
  174. 174
  175. 175///|
  176. 176/// 重聚焦结果
  177. 177#derive(Eq, @debug.Debug)
  178. 178pub(all) struct RefocusResult {
  179. 179 image : @types.Image
  180. 180 focus_depth : Double
  181. 181 aperture_size : Double
  182. 182 num_views_used : Int32
  183. 183}
  184. 184
  185. 185///|
  186. 186pub extend RefocusResult with Eq::{not_equal, equal}
  187. 187
  188. 188///|
  189. 189pub extend RefocusResult with @debug.Debug::{to_repr}
  190. 190
  191. 191///|
  192. 192/// 视角合成结果
  193. 193#derive(Eq, @debug.Debug)
  194. 194pub(all) struct ViewSynthesisResult {
  195. 195 image : @types.Image
  196. 196 view_position : Array[Double]
  197. 197 num_source_views : Int32
  198. 198 interpolation_method : String
  199. 199}
  200. 200
  201. 201///|
  202. 202pub extend ViewSynthesisResult with Eq::{not_equal, equal}
  203. 203
  204. 204///|
  205. 205pub extend ViewSynthesisResult with @debug.Debug::{to_repr}
  206. 206
  207. 207///|
  208. 208/// 重建结果
  209. 209#derive(Eq, @debug.Debug)
  210. 210pub(all) struct ImagingReconstructionResult {
  211. 211 image : @types.Image
  212. 212 algorithm : ComputationalImagingAlgorithm
  213. 213 num_iterations : Int32
  214. 214 convergence_error : Double
  215. 215 reconstruction_time_ms : Double
  216. 216 psnr : Double
  217. 217 ssim : Double
  218. 218}
  219. 219
  220. 220///|
  221. 221pub extend ImagingReconstructionResult with Eq::{not_equal, equal}
  222. 222
  223. 223///|
  224. 224pub extend ImagingReconstructionResult with @debug.Debug::{to_repr}
  225. 225
  226. 226///|
  227. 227/// 创建默认光场相机参数
  228. 228pub fn create_default_lightfield_camera() -> LightFieldCameraParams {
  229. 229● LightFieldCameraParams::{
  230. 230 num_views_x: 9,
  231. 231 num_views_y: 9,
  232. 232 micro_lens_pitch: 0.01,
  233. 233 focal_length: 50.0,
  234. 234 sensor_pitch: 0.005,
  235. 235 num_aperture_samples: 64,
  236. 236 }
  237. 237}
  238. 238
  239. 239///|
  240. 240/// 创建默认相位恢复配置
  241. 241pub fn create_default_phase_config() -> PhaseRetrievalConfig {
  242. 242● PhaseRetrievalConfig::{
  243. 243 num_iterations: 100,
  244. 244 algorithm: "Gerchberg-Saxton",
  245. 245 support_threshold: 0.1,
  246. 246 use_hybrid_input_output: true,
  247. 247 beta: 0.9,
  248. 248 }
  249. 249}
  250. 250
  251. 251///|
  252. 252/// 创建默认编码孔径模式
  253. 253pub fn create_default_coded_aperture(size : Int32) -> CodedAperturePattern {
  254. 254● let total = size * size
  255. 255 let out : Array[Byte] = []
  256. 256 let mut state = 42
  257. 257
  258. 258 for _ in 0..<total {
  259. 259● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  260. 260● let value = if state % 2 == 0 { 255 } else { 0 }
  261. 261● out.push(value.to_byte())
  262. 262 }
  263. 263
  264. 264 let pattern = @types.Image::{
  265. 265 width: size,
  266. 266 height: size,
  267. 267 channels: 1,
  268. 268● data: Bytes::from_array(out),
  269. 269 }
  270. 270
  271. 271 CodedAperturePattern::{
  272. 272 pattern,
  273. 273 pattern_size: size,
  274. 274 code_type: "random",
  275. 275 num_elements: total,
  276. 276 }
  277. 277}
  278. 278
  279. 279///|
  280. 280/// 生成示例光场数据
  281. 281pub fn generate_sample_lightfield(
  282. 282 base_image : @types.Image,
  283. 283 params : LightFieldCameraParams,
  284. 284) -> LightFieldData {
  285. 285● let views : Array[@types.Image] = []
  286. 286 let w = base_image.width
  287. 287 let h = base_image.height
  288. 288
  289. 289 for vy in 0..<params.num_views_y {
  290. 290● for vx in 0..<params.num_views_x {
  291. 291 // 简化的视角偏移
  292. 292● let offset_x = (vx - params.num_views_x / 2) * 2
  293. 293 let offset_y = (vy - params.num_views_y / 2) * 2
  294. 294 let out : Array[Byte] = []
  295. 295
  296. 296 for y in 0..<h {
  297. 297● for x in 0..<w {
  298. 298● let src_x = x + offset_x
  299. 299 let src_y = y + offset_y
  300. 300 let idx = if src_x >= 0 && src_x < w && src_y >= 0 && src_y < h {
  301. 301● src_y * w + src_x
  302. 302 } else {
  303. 303● y * w + x
  304. 304 }
  305. 305● out.push(base_image.data[idx])
  306. 306 }
  307. 307 }
  308. 308
  309. 309● views.push(@types.Image::{
  310. 310 width: w,
  311. 311 height: h,
  312. 312 channels: base_image.channels,
  313. 313● data: Bytes::from_array(out),
  314. 314 })
  315. 315 }
  316. 316 }
  317. 317
  318. 318 LightFieldData::{
  319. 319 views,
  320. 320 num_views_x: params.num_views_x,
  321. 321 num_views_y: params.num_views_y,
  322. 322 total_views: params.num_views_x * params.num_views_y,
  323. 323 representation: LightFieldRepresentation::TwoPlane,
  324. 324 }
  325. 325}
  326. 326
  327. 327///|
  328. 328/// 光场重聚焦
  329. 329pub fn lightfield_refocus(
  330. 330 lightfield : LightFieldData,
  331. 331 focus_depth : Double,
  332. 332 aperture_size : Double,
  333. 333) -> RefocusResult {
  334. 334● if lightfield.views.length() == 0 {
  335. 335○ return RefocusResult::{
  336. 336 image: @types.Image::{
  337. 337 width: 1,
  338. 338 height: 1,
  339. 339 channels: 1,
  340. 340○ data: Bytes::make(1, b'\x00'),
  341. 341 },
  342. 342 focus_depth,
  343. 343 aperture_size,
  344. 344 num_views_used: 0,
  345. 345 }
  346. 346 }
  347. 347
  348. 348 let first = lightfield.views[0]
  349. 349 let w = first.width
  350. 350 let h = first.height
  351. 351 let channels = first.channels
  352. 352 let total = w * h * channels
  353. 353 let accumulator : Array[Double] = []
  354. 354 for _ in 0..<total {
  355. 355● accumulator.push(0.0)
  356. 356 }
  357. 357
  358. 358 let mut num_views_used = 0
  359. 359 let center_x = lightfield.num_views_x / 2
  360. 360 let center_y = lightfield.num_views_y / 2
  361. 361
  362. 362 for vy in 0..<lightfield.num_views_y {
  363. 363● for vx in 0..<lightfield.num_views_x {
  364. 364● let dx = (vx - center_x).to_double()
  365. 365● let dy = (vy - center_y).to_double()
  366. 366● let dist = (dx * dx + dy * dy).sqrt()
  367. 367
  368. 368 if dist <= aperture_size {
  369. 369● let view_idx = vy * lightfield.num_views_x + vx
  370. 370 let view = lightfield.views[view_idx]
  371. 371
  372. 372 // 简化的重聚焦:根据深度偏移像素
  373. 373● let shift_x = (dx * focus_depth * 0.1).to_int()
  374. 374● let shift_y = (dy * focus_depth * 0.1).to_int()
  375. 375
  376. 376 for y in 0..<h {
  377. 377● for x in 0..<w {
  378. 378● let src_x = x + shift_x
  379. 379 let src_y = y + shift_y
  380. 380 if src_x >= 0 && src_x < w && src_y >= 0 && src_y < h {
  381. 381● for c in 0..<channels {
  382. 382● let dst_idx = (y * w + x) * channels + c
  383. 383 let src_idx = (src_y * w + src_x) * channels + c
  384. 384● accumulator[dst_idx] += view.data[src_idx].to_double()
  385. 385 }
  386. 386 }
  387. 387 }
  388. 388 }
  389. 389 num_views_used += 1
  390. 390 }
  391. 391 }
  392. 392 }
  393. 393
  394. 394 // 归一化
  395. 395 let out : Array[Byte] = []
  396. 396 for i in 0..<total {
  397. 397● let value = if num_views_used > 0 {
  398. 398● accumulator[i] / num_views_used.to_double()
  399. 399 } else {
  400. 400○ 0.0
  401. 401 }
  402. 402 let clamped = if value < 0.0 {
  403. 403○ 0.0
  404. 404● } else if value > 255.0 {
  405. 405○ 255.0
  406. 406 } else {
  407. 407● value
  408. 408 }
  409. 409● out.push(clamped.to_int().to_byte())
  410. 410 }
  411. 411
  412. 412 RefocusResult::{
  413. 413 image: @types.Image::{
  414. 414 width: w,
  415. 415 height: h,
  416. 416 channels,
  417. 417● data: Bytes::from_array(out),
  418. 418 },
  419. 419 focus_depth,
  420. 420 aperture_size,
  421. 421 num_views_used,
  422. 422 }
  423. 423}
  424. 424
  425. 425///|
  426. 426/// 视角合成
  427. 427pub fn synthesize_view(
  428. 428 lightfield : LightFieldData,
  429. 429 target_x : Double,
  430. 430 target_y : Double,
  431. 431) -> ViewSynthesisResult {
  432. 432● if lightfield.views.length() < 4 {
  433. 433○ return ViewSynthesisResult::{
  434. 434 image: @types.Image::{
  435. 435 width: 1,
  436. 436 height: 1,
  437. 437 channels: 1,
  438. 438○ data: Bytes::make(1, b'\x00'),
  439. 439 },
  440. 440 view_position: [target_x, target_y],
  441. 441 num_source_views: 0,
  442. 442 interpolation_method: "none",
  443. 443 }
  444. 444 }
  445. 445
  446. 446 let first = lightfield.views[0]
  447. 447 let w = first.width
  448. 448 let h = first.height
  449. 449 let channels = first.channels
  450. 450 let total = w * h * channels
  451. 451
  452. 452 // 找到最近的 4 个视角进行双线性插值
  453. 453● let fx = target_x * (lightfield.num_views_x - 1).to_double()
  454. 454● let fy = target_y * (lightfield.num_views_y - 1).to_double()
  455. 455● let x0 = fx.to_int()
  456. 456● let y0 = fy.to_int()
  457. 457◐ let x1 = if x0 + 1 < lightfield.num_views_x { x0 + 1 } else { x0 }
  458. 458◐ let y1 = if y0 + 1 < lightfield.num_views_y { y0 + 1 } else { y0 }
  459. 459● let wx = fx - x0.to_double()
  460. 460● let wy = fy - y0.to_double()
  461. 461
  462. 462 let idx00 = y0 * lightfield.num_views_x + x0
  463. 463 let idx01 = y0 * lightfield.num_views_x + x1
  464. 464 let idx10 = y1 * lightfield.num_views_x + x0
  465. 465 let idx11 = y1 * lightfield.num_views_x + x1
  466. 466
  467. 467 let v00 = lightfield.views[idx00]
  468. 468 let v01 = lightfield.views[idx01]
  469. 469 let v10 = lightfield.views[idx10]
  470. 470 let v11 = lightfield.views[idx11]
  471. 471
  472. 472 let out : Array[Byte] = []
  473. 473 for i in 0..<total {
  474. 474● let value = v00.data[i].to_double() * (1.0 - wx) * (1.0 - wy) +
  475. 475● v01.data[i].to_double() * wx * (1.0 - wy) +
  476. 476● v10.data[i].to_double() * (1.0 - wx) * wy +
  477. 477● v11.data[i].to_double() * wx * wy
  478. 478 let clamped = if value < 0.0 {
  479. 479○ 0.0
  480. 480● } else if value > 255.0 {
  481. 481○ 255.0
  482. 482 } else {
  483. 483● value
  484. 484 }
  485. 485● out.push(clamped.to_int().to_byte())
  486. 486 }
  487. 487
  488. 488 ViewSynthesisResult::{
  489. 489 image: @types.Image::{
  490. 490 width: w,
  491. 491 height: h,
  492. 492 channels,
  493. 493● data: Bytes::from_array(out),
  494. 494 },
  495. 495 view_position: [target_x, target_y],
  496. 496 num_source_views: 4,
  497. 497 interpolation_method: "bilinear",
  498. 498 }
  499. 499}
  500. 500
  501. 501///|
  502. 502/// 生成计算全息图(简化版)
  503. 503pub fn generate_hologram(
  504. 504 image : @types.Image,
  505. 505 wavelength : Double,
  506. 506 distance : Double,
  507. 507 pixel_size : Double,
  508. 508) -> Hologram {
  509. 509● let w = image.width
  510. 510 let h = image.height
  511. 511 let _ = w * h
  512. 512 let intensity_out : Array[Byte] = []
  513. 513 let phase_out : Array[Byte] = []
  514. 514
  515. 515 // 简化的菲涅尔全息图生成
  516. 516 for y in 0..<h {
  517. 517● for x in 0..<w {
  518. 518● let idx = y * w + x
  519. 519● let gray = image.data[idx].to_double() / 255.0
  520. 520
  521. 521 // 简化的相位计算
  522. 522 let phase = (
  523. 523● x.to_double() * x.to_double() + y.to_double() * y.to_double()
  524. 524 ) *
  525. 525 3.14159 /
  526. 526 (wavelength * distance)
  527. 527 let phase_norm = phase % 6.28318 / 6.28318 * 255.0
  528. 528
  529. 529 // 干涉图样
  530. 530 let interference = gray * 127.0 +
  531. 531● 128.0 * (0.5 + 0.5 * cos_approx(phase_norm))
  532. 532
  533. 533● intensity_out.push(interference.to_int().to_byte())
  534. 534● phase_out.push(phase_norm.to_int().to_byte())
  535. 535 }
  536. 536 }
  537. 537
  538. 538 Hologram::{
  539. 539 intensity: @types.Image::{
  540. 540 width: w,
  541. 541 height: h,
  542. 542 channels: 1,
  543. 543● data: Bytes::from_array(intensity_out),
  544. 544 },
  545. 545 phase: @types.Image::{
  546. 546 width: w,
  547. 547 height: h,
  548. 548 channels: 1,
  549. 549● data: Bytes::from_array(phase_out),
  550. 550 },
  551. 551 hologram_type: HologramType::Fresnel,
  552. 552 wavelength,
  553. 553 distance,
  554. 554 pixel_size,
  555. 555 }
  556. 556}
  557. 557
  558. 558///|
  559. 559/// 全息图重建(简化版)
  560. 560pub fn reconstruct_hologram(hologram : Hologram) -> ImagingReconstructionResult {
  561. 561● let w = hologram.intensity.width
  562. 562 let h = hologram.intensity.height
  563. 563 let _ = w * h
  564. 564 let out : Array[Byte] = []
  565. 565
  566. 566 // 简化的重建:直接使用强度图的逆变换
  567. 567 for y in 0..<h {
  568. 568● for x in 0..<w {
  569. 569● let idx = y * w + x
  570. 570● let value = hologram.intensity.data[idx].to_double()
  571. 571 // 简化的重建:对比度增强
  572. 572 let reconstructed = (value - 128.0) * 1.5 + 128.0
  573. 573 let clamped = if reconstructed < 0.0 {
  574. 574● 0.0
  575. 575● } else if reconstructed > 255.0 {
  576. 576○ 255.0
  577. 577 } else {
  578. 578● reconstructed
  579. 579 }
  580. 580● out.push(clamped.to_int().to_byte())
  581. 581 }
  582. 582 }
  583. 583
  584. 584 ImagingReconstructionResult::{
  585. 585 image: @types.Image::{
  586. 586 width: w,
  587. 587 height: h,
  588. 588 channels: 1,
  589. 589● data: Bytes::from_array(out),
  590. 590 },
  591. 591 algorithm: ComputationalImagingAlgorithm::Holography,
  592. 592 num_iterations: 1,
  593. 593 convergence_error: 0.0,
  594. 594 reconstruction_time_ms: 0.0,
  595. 595 psnr: 0.0,
  596. 596 ssim: 0.0,
  597. 597 }
  598. 598}
  599. 599
  600. 600///|
  601. 601/// CT 滤波反投影重建(简化版)
  602. 602pub fn ct_reconstruct_fbp(
  603. 603 projections : CTProjectionData,
  604. 604 output_size : Int32,
  605. 605) -> ImagingReconstructionResult {
  606. 606● let total = output_size * output_size
  607. 607 let out : Array[Byte] = []
  608. 608 for _ in 0..<total {
  609. 609● out.push(b'\x00')
  610. 610 }
  611. 611
  612. 612 // 简化的 FBP 重建
  613. 613● if projections.projections.length() > 0 {
  614. 614● let first = projections.projections[0]
  615. 615 let num_detectors = first.width
  616. 616
  617. 617 for angle_idx in 0..<projections.num_angles {
  618. 618● if angle_idx < projections.projections.length() {
  619. 619● let proj = projections.projections[angle_idx]
  620. 620● let angle = angle_idx.to_double() *
  621. 621 projections.angular_range /
  622. 622● projections.num_angles.to_double()
  623. 623● let cos_a = cos_approx(angle)
  624. 624● let sin_a = sin_approx(angle)
  625. 625
  626. 626 for y in 0..<output_size {
  627. 627● for x in 0..<output_size {
  628. 628● let cx = x.to_double() - output_size.to_double() / 2.0
  629. 629● let cy = y.to_double() - output_size.to_double() / 2.0
  630. 630 let detector_pos = cx * cos_a + cy * sin_a
  631. 631● let detector_idx = (detector_pos + num_detectors.to_double() / 2.0).to_int()
  632. 632
  633. 633 if detector_idx >= 0 && detector_idx < num_detectors {
  634. 634● let idx = y * output_size + x
  635. 635● let value = out[idx].to_double() +
  636. 636● proj.data[detector_idx].to_double() * 0.1
  637. 637◐ let clamped = if value > 255.0 { 255.0 } else { value }
  638. 638● out[idx] = clamped.to_int().to_byte()
  639. 639 }
  640. 640 }
  641. 641 }
  642. 642 }
  643. 643 }
  644. 644 }
  645. 645
  646. 646 ImagingReconstructionResult::{
  647. 647 image: @types.Image::{
  648. 648 width: output_size,
  649. 649 height: output_size,
  650. 650 channels: 1,
  651. 651● data: Bytes::from_array(out),
  652. 652 },
  653. 653 algorithm: ComputationalImagingAlgorithm::ComputedTomography,
  654. 654 num_iterations: 1,
  655. 655 convergence_error: 0.0,
  656. 656 reconstruction_time_ms: 0.0,
  657. 657 psnr: 0.0,
  658. 658 ssim: 0.0,
  659. 659 }
  660. 660}
  661. 661
  662. 662///|
  663. 663/// 相位恢复(简化版 Gerchberg-Saxton)
  664. 664pub fn phase_retrieval(
  665. 665 magnitude : @types.Image,
  666. 666 config : PhaseRetrievalConfig,
  667. 667) -> ImagingReconstructionResult {
  668. 668● let w = magnitude.width
  669. 669 let h = magnitude.height
  670. 670 let total = w * h
  671. 671
  672. 672 // 简化的相位恢复:迭代优化
  673. 673 let phase : Array[Double] = []
  674. 674 for _ in 0..<total {
  675. 675● phase.push(0.0)
  676. 676 }
  677. 677
  678. 678 let mut error = 0.0
  679. 679 for iter in 0..<config.num_iterations {
  680. 680 // 简化的迭代:随机扰动相位
  681. 681● let mut state = iter * 11035 + 12345
  682. 682 for i in 0..<total {
  683. 683● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  684. 684● let perturbation = (state % 1000).to_double() / 1000.0 - 0.5
  685. 685 phase[i] += perturbation * 0.01
  686. 686 }
  687. 687● error = 1.0 / (iter + 1).to_double()
  688. 688 }
  689. 689
  690. 690 // 生成重建图像
  691. 691 let out : Array[Byte] = []
  692. 692 for i in 0..<total {
  693. 693● let value = magnitude.data[i].to_double() *
  694. 694● (0.5 + 0.5 * cos_approx(phase[i]))
  695. 695 let clamped = if value < 0.0 {
  696. 696○ 0.0
  697. 697● } else if value > 255.0 {
  698. 698○ 255.0
  699. 699 } else {
  700. 700● value
  701. 701 }
  702. 702● out.push(clamped.to_int().to_byte())
  703. 703 }
  704. 704
  705. 705 ImagingReconstructionResult::{
  706. 706 image: @types.Image::{
  707. 707 width: w,
  708. 708 height: h,
  709. 709 channels: 1,
  710. 710● data: Bytes::from_array(out),
  711. 711 },
  712. 712 algorithm: ComputationalImagingAlgorithm::PhaseRetrieval,
  713. 713 num_iterations: config.num_iterations,
  714. 714 convergence_error: error,
  715. 715 reconstruction_time_ms: 0.0,
  716. 716 psnr: 0.0,
  717. 717 ssim: 0.0,
  718. 718 }
  719. 719}
  720. 720
  721. 721///|
  722. 722/// 编码孔径成像重建(简化版)
  723. 723pub fn coded_aperture_reconstruct(
  724. 724 measurement : @types.Image,
  725. 725 pattern : CodedAperturePattern,
  726. 726) -> ImagingReconstructionResult {
  727. 727● let w = measurement.width
  728. 728 let h = measurement.height
  729. 729 let _ = w * h
  730. 730 let out : Array[Byte] = []
  731. 731
  732. 732 // 简化的反卷积重建
  733. 733 for y in 0..<h {
  734. 734● for x in 0..<w {
  735. 735● let _ = y * w + x
  736. 736 let mut sum = 0.0
  737. 737 let mut count = 0
  738. 738
  739. 739 // 局部反卷积
  740. 740 for dy in -2..<3 {
  741. 741● for dx in -2..<3 {
  742. 742● let src_x = x + dx
  743. 743 let src_y = y + dy
  744. 744 if src_x >= 0 && src_x < w && src_y >= 0 && src_y < h {
  745. 745● let pat_idx = src_y % pattern.pattern_size * pattern.pattern_size +
  746. 746 src_x % pattern.pattern_size
  747. 747● if pattern.pattern.data[pat_idx].to_int() > 128 {
  748. 748● sum += measurement.data[src_y * w + src_x].to_double()
  749. 749 count += 1
  750. 750 }
  751. 751 }
  752. 752 }
  753. 753 }
  754. 754
  755. 755◐ let value = if count > 0 { sum / count.to_double() } else { 0.0 }
  756. 756 let clamped = if value < 0.0 {
  757. 757○ 0.0
  758. 758● } else if value > 255.0 {
  759. 759○ 255.0
  760. 760 } else {
  761. 761● value
  762. 762 }
  763. 763● out.push(clamped.to_int().to_byte())
  764. 764 }
  765. 765 }
  766. 766
  767. 767 ImagingReconstructionResult::{
  768. 768 image: @types.Image::{
  769. 769 width: w,
  770. 770 height: h,
  771. 771 channels: 1,
  772. 772● data: Bytes::from_array(out),
  773. 773 },
  774. 774 algorithm: ComputationalImagingAlgorithm::CodedAperture,
  775. 775 num_iterations: 1,
  776. 776 convergence_error: 0.0,
  777. 777 reconstruction_time_ms: 0.0,
  778. 778 psnr: 0.0,
  779. 779 ssim: 0.0,
  780. 780 }
  781. 781}
  782. 782
  783. 783///|
  784. 784/// 生成计算成像报告
  785. 785pub fn computational_imaging_report_to_string(
  786. 786 result : ImagingReconstructionResult,
  787. 787 algorithm_name : String,
  788. 788) -> String {
  789. 789● let mut report = "## 计算成像报告\n\n"
  790. 790 report = report + "### 重建信息\n\n"
  791. 791 report = report + "| 指标 | 值 |\n|------|------|\n"
  792. 792 report = report + "| 算法 | " + algorithm_name + " |\n"
  793. 793 report = report +
  794. 794 "| 迭代次数 | " +
  795. 795● result.num_iterations.to_string() +
  796. 796 " |\n"
  797. 797 report = report +
  798. 798 "| 收敛误差 | " +
  799. 799● result.convergence_error.to_string() +
  800. 800 " |\n"
  801. 801 report = report +
  802. 802 "| 重建时间 | " +
  803. 803● result.reconstruction_time_ms.to_string() +
  804. 804 " ms |\n"
  805. 805● report = report + "| PSNR | " + result.psnr.to_string() + " dB |\n"
  806. 806● report = report + "| SSIM | " + result.ssim.to_string() + " |\n"
  807. 807 report = report +
  808. 808 "| 图像尺寸 | " +
  809. 809● result.image.width.to_string() +
  810. 810 "x" +
  811. 811● result.image.height.to_string() +
  812. 812 " |\n"
  813. 813
  814. 814 report
  815. 815}
  816. 816
  817. 817// 辅助函数:Double 的近似三角函数
  818. 818
  819. 819///|
  820. 820fn cos_approx(x : Double) -> Double {
  821. 821● let x2 = x * x
  822. 822 1.0 - x2 / 2.0 + x2 * x2 / 24.0
  823. 823}
  824. 824
  825. 825///|
  826. 826fn sin_approx(x : Double) -> Double {
  827. 827● let x2 = x * x
  828. 828 x - x * x2 / 6.0 + x * x2 * x2 / 120.0
  829. 829}