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