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