image.photography\fusion\fusion_registration_enhancement.mbt
93.8% · 196 / 209 points · 677 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 融合配准增强 fusion/fusion_registration_enhancement - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像融合与配准增强(v26.0.0) - 7
// 多尺度图像融合、图像配准、全景拼接、HDR融合 - 8
- 9
///| - 10
/// 融合算法类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum FusionAlgorithm { - 15
WeightedAverage - 16
LaplacianPyramid - 17
GaussianPyramid - 18
Wavelet - 19
PCA - 20
IHS - 21
Brovey - 22
SimpleMean - 23
Maximum - 24
Minimum - 25
Unknown - 26
} - 27
- 28
///| - 29
pub extend FusionAlgorithm with @debug.Debug::{to_repr} - 30
- 31
///| - 32
pub extend FusionAlgorithm with Eq::{not_equal, equal} - 33
- 34
///| - 35
/// 融合结果 - 36
#derive(Eq, @debug.Debug) - 37
pub(all) struct FusionResult { - 38
image : @types.Image - 39
algorithm : FusionAlgorithm - 40
num_images : Int32 - 41
processing_time_ms : Double - 42
fusion_quality : Double - 43
} - 44
- 45
///| - 46
pub extend FusionResult with Eq::{not_equal, equal} - 47
- 48
///| - 49
pub extend FusionResult with @debug.Debug::{to_repr} - 50
- 51
///| - 52
/// 融合配置 - 53
#derive(Eq, @debug.Debug) - 54
pub(all) struct FusionEnhancedConfig { - 55
algorithm : FusionAlgorithm - 56
weights : Array[Double] - 57
pyramid_levels : Int32 - 58
use_color_preservation : Bool - 59
} - 60
- 61
///| - 62
pub extend FusionEnhancedConfig with Eq::{not_equal, equal} - 63
- 64
///| - 65
pub extend FusionEnhancedConfig with @debug.Debug::{to_repr} - 66
- 67
///| - 68
/// 配准算法类型 - 69
/// `#non_exhaustive` 表示未来可能添加新构造器 - 70
#derive(Eq, @debug.Debug) - 71
#non_exhaustive - 72
pub(all) enum RegistrationAlgorithm { - 73
PhaseCorrelation - 74
FeatureBased - 75
AreaBased - 76
MutualInformation - 77
OpticalFlow - 78
Unknown - 79
} - 80
- 81
///| - 82
pub extend RegistrationAlgorithm with @debug.Debug::{to_repr} - 83
- 84
///| - 85
pub extend RegistrationAlgorithm with Eq::{not_equal, equal} - 86
- 87
///| - 88
/// 配准结果 - 89
#derive(Eq, @debug.Debug) - 90
pub(all) struct RegistrationResult { - 91
image : @types.Image - 92
algorithm : RegistrationAlgorithm - 93
translation_x : Double - 94
translation_y : Double - 95
rotation_angle : Double - 96
scale_factor : Double - 97
similarity : Double - 98
processing_time_ms : Double - 99
} - 100
- 101
///| - 102
pub extend RegistrationResult with Eq::{not_equal, equal} - 103
- 104
///| - 105
pub extend RegistrationResult with @debug.Debug::{to_repr} - 106
- 107
///| - 108
/// 配准配置 - 109
#derive(Eq, @debug.Debug) - 110
pub(all) struct RegistrationAdvancedConfig { - 111
algorithm : RegistrationAlgorithm - 112
max_iterations : Int32 - 113
tolerance : Double - 114
use_multiscale : Bool - 115
} - 116
- 117
///| - 118
pub extend RegistrationAdvancedConfig with Eq::{not_equal, equal} - 119
- 120
///| - 121
pub extend RegistrationAdvancedConfig with @debug.Debug::{to_repr} - 122
- 123
///| - 124
/// 变换矩阵 - 125
#derive(Eq, @debug.Debug) - 126
pub(all) struct TransformMatrix { - 127
a : Double - 128
b : Double - 129
c : Double - 130
d : Double - 131
e : Double - 132
f : Double - 133
} - 134
- 135
///| - 136
pub extend TransformMatrix with Eq::{not_equal, equal} - 137
- 138
///| - 139
pub extend TransformMatrix with @debug.Debug::{to_repr} - 140
- 141
///| - 142
/// 创建默认融合配置 - 143
pub fn create_default_fusion_enhanced_config() -> FusionEnhancedConfig { - 144●
FusionEnhancedConfig::{ - 145
algorithm: FusionAlgorithm::WeightedAverage, - 146
weights: [0.5, 0.5], - 147
pyramid_levels: 4, - 148
use_color_preservation: true, - 149
} - 150
} - 151
- 152
///| - 153
/// 创建默认配准配置 - 154
pub fn create_default_registration_advanced_config() -> RegistrationAdvancedConfig { - 155●
RegistrationAdvancedConfig::{ - 156
algorithm: RegistrationAlgorithm::PhaseCorrelation, - 157
max_iterations: 100, - 158
tolerance: 0.001, - 159
use_multiscale: true, - 160
} - 161
} - 162
- 163
///| - 164
/// 加权平均融合 - 165
pub fn fuse_weighted_average( - 166
images : Array[@types.Image], - 167
weights : Array[Double], - 168
) -> @types.Image { - 169●
if images.length() == 0 { - 170●
return @types.Image::{ - 171
width: 0, - 172
height: 0, - 173
channels: 1, - 174●
data: Bytes::from_array([]), - 175
} - 176
} - 177
- 178
let w = images[0].width - 179
let h = images[0].height - 180
let total = w * h - 181
let out : Array[Byte] = [] - 182
- 183
for i in 0..<total { - 184●
let mut sum = 0.0 - 185
let mut weight_sum = 0.0 - 186●
for j in 0..<images.length() { - 187●
let weight = if j < weights.length() { weights[j] } else { 1.0 } - 188●
sum += images[j].data[i].to_double() * weight - 189
weight_sum += weight - 190
} - 191●
let avg = if weight_sum > 0.0 { sum / weight_sum } else { 0.0 } - 192
let clamped = if avg < 0.0 { - 193○
0.0 - 194●
} else if avg > 255.0 { - 195○
255.0 - 196
} else { - 197●
avg - 198
} - 199●
out.push(clamped.to_int().to_byte()) - 200
} - 201
- 202
@types.Image::{ - 203
width: w, - 204
height: h, - 205
channels: images[0].channels, - 206●
data: Bytes::from_array(out), - 207
} - 208
} - 209
- 210
///| - 211
/// 最大值融合 - 212
pub fn fuse_maximum(images : Array[@types.Image]) -> @types.Image { - 213●
if images.length() == 0 { - 214●
return @types.Image::{ - 215
width: 0, - 216
height: 0, - 217
channels: 1, - 218●
data: Bytes::from_array([]), - 219
} - 220
} - 221
- 222
let w = images[0].width - 223
let h = images[0].height - 224
let total = w * h - 225
let out : Array[Byte] = [] - 226
- 227
for i in 0..<total { - 228●
let mut max_val = 0.0 - 229●
for j in 0..<images.length() { - 230●
let val = images[j].data[i].to_double() - 231
if val > max_val { - 232●
max_val = val - 233
} - 234
} - 235●
out.push(max_val.to_int().to_byte()) - 236
} - 237
- 238
@types.Image::{ - 239
width: w, - 240
height: h, - 241
channels: images[0].channels, - 242●
data: Bytes::from_array(out), - 243
} - 244
} - 245
- 246
///| - 247
/// 最小值融合 - 248
pub fn fuse_minimum(images : Array[@types.Image]) -> @types.Image { - 249●
if images.length() == 0 { - 250●
return @types.Image::{ - 251
width: 0, - 252
height: 0, - 253
channels: 1, - 254●
data: Bytes::from_array([]), - 255
} - 256
} - 257
- 258
let w = images[0].width - 259
let h = images[0].height - 260
let total = w * h - 261
let out : Array[Byte] = [] - 262
- 263
for i in 0..<total { - 264●
let mut min_val = 255.0 - 265●
for j in 0..<images.length() { - 266●
let val = images[j].data[i].to_double() - 267
if val < min_val { - 268●
min_val = val - 269
} - 270
} - 271●
out.push(min_val.to_int().to_byte()) - 272
} - 273
- 274
@types.Image::{ - 275
width: w, - 276
height: h, - 277
channels: images[0].channels, - 278●
data: Bytes::from_array(out), - 279
} - 280
} - 281
- 282
///| - 283
/// 简单均值融合 - 284
pub fn fuse_simple_mean(images : Array[@types.Image]) -> @types.Image { - 285●
let weights : Array[Double] = [] - 286●
for _ in 0..<images.length() { - 287●
weights.push(1.0 / images.length().to_double()) - 288
} - 289●
fuse_weighted_average(images, weights) - 290
} - 291
- 292
///| - 293
/// 高斯模糊(用于金字塔) - 294
pub fn gaussian_blur_fe( - 295
img : @types.Image, - 296
kernel_size : Int32, - 297
) -> @types.Image { - 298●
let w = img.width - 299
let h = img.height - 300
let _ = w * h - 301
let out : Array[Byte] = [] - 302
let radius = kernel_size / 2 - 303
- 304
for y in 0..<h { - 305●
for x in 0..<w { - 306●
let mut sum = 0.0 - 307
let mut weight_sum = 0.0 - 308
for dy in -radius..<(radius + 1) { - 309●
for dx in -radius..<(radius + 1) { - 310●
let nx = x + dx - 311
let ny = y + dy - 312
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 313●
let dist = (dx * dx + dy * dy).to_double() - 314
let weight = ( - 315●
0.0 - dist / (2.0 * radius.to_double() * radius.to_double()) - 316
) * - 317
0.1 - 318●
sum += img.data[ny * w + nx].to_double() * weight - 319
weight_sum += weight - 320
} - 321
} - 322
} - 323
let avg = if weight_sum > 0.0 { - 324○
sum / weight_sum - 325
} else { - 326●
img.data[y * w + x].to_double() - 327
} - 328
let clamped = if avg < 0.0 { - 329○
0.0 - 330●
} else if avg > 255.0 { - 331○
255.0 - 332
} else { - 333●
avg - 334
} - 335●
out.push(clamped.to_int().to_byte()) - 336
} - 337
} - 338
- 339
@types.Image::{ - 340
width: w, - 341
height: h, - 342
channels: img.channels, - 343●
data: Bytes::from_array(out), - 344
} - 345
} - 346
- 347
///| - 348
/// 图像下采样(2x) - 349
pub fn downsample_fe(img : @types.Image) -> @types.Image { - 350●
let w = img.width / 2 - 351
let h = img.height / 2 - 352
let out : Array[Byte] = [] - 353
- 354
for y in 0..<h { - 355●
for x in 0..<w { - 356●
let src_x = x * 2 - 357
let src_y = y * 2 - 358●
let p00 = img.data[src_y * img.width + src_x].to_double() - 359●
let p01 = img.data[src_y * img.width + src_x + 1].to_double() - 360●
let p10 = img.data[(src_y + 1) * img.width + src_x].to_double() - 361●
let p11 = img.data[(src_y + 1) * img.width + src_x + 1].to_double() - 362
let avg = (p00 + p01 + p10 + p11) / 4.0 - 363●
out.push(avg.to_int().to_byte()) - 364
} - 365
} - 366
- 367
@types.Image::{ - 368
width: w, - 369
height: h, - 370
channels: img.channels, - 371●
data: Bytes::from_array(out), - 372
} - 373
} - 374
- 375
///| - 376
/// 图像上采样(2x,双线性) - 377
pub fn upsample_fe(img : @types.Image) -> @types.Image { - 378●
let w = img.width * 2 - 379
let h = img.height * 2 - 380
let out : Array[Byte] = [] - 381
- 382
for y in 0..<h { - 383●
for x in 0..<w { - 384●
let src_x = x.to_double() / 2.0 - 385●
let src_y = y.to_double() / 2.0 - 386●
let x0 = src_x.to_int() - 387●
let y0 = src_y.to_int() - 388
let x1 = x0 + 1 - 389
let y1 = y0 + 1 - 390●
let fx = src_x - x0.to_double() - 391●
let fy = src_y - y0.to_double() - 392
- 393
let cx0 = if x0 < 0 { - 394○
0 - 395●
} else if x0 >= img.width { - 396○
img.width - 1 - 397
} else { - 398●
x0 - 399
} - 400
let cy0 = if y0 < 0 { - 401○
0 - 402●
} else if y0 >= img.height { - 403○
img.height - 1 - 404
} else { - 405●
y0 - 406
} - 407
let cx1 = if x1 < 0 { - 408○
0 - 409●
} else if x1 >= img.width { - 410●
img.width - 1 - 411
} else { - 412●
x1 - 413
} - 414
let cy1 = if y1 < 0 { - 415○
0 - 416●
} else if y1 >= img.height { - 417●
img.height - 1 - 418
} else { - 419●
y1 - 420
} - 421
- 422●
let p00 = img.data[cy0 * img.width + cx0].to_double() - 423●
let p01 = img.data[cy0 * img.width + cx1].to_double() - 424●
let p10 = img.data[cy1 * img.width + cx0].to_double() - 425●
let p11 = img.data[cy1 * img.width + cx1].to_double() - 426
- 427
let top = p00 * (1.0 - fx) + p01 * fx - 428
let bottom = p10 * (1.0 - fx) + p11 * fx - 429
let value = top * (1.0 - fy) + bottom * fy - 430
let clamped = if value < 0.0 { - 431○
0.0 - 432●
} else if value > 255.0 { - 433○
255.0 - 434
} else { - 435●
value - 436
} - 437●
out.push(clamped.to_int().to_byte()) - 438
} - 439
} - 440
- 441
@types.Image::{ - 442
width: w, - 443
height: h, - 444
channels: img.channels, - 445●
data: Bytes::from_array(out), - 446
} - 447
} - 448
- 449
///| - 450
/// 拉普拉斯金字塔融合(简化版) - 451
pub fn fuse_laplacian_pyramid( - 452
images : Array[@types.Image], - 453
levels : Int32, - 454
) -> @types.Image { - 455●
ignore(levels) - 456●
if images.length() < 2 { - 457●
return if images.length() == 1 { - 458●
images[0] - 459
} else { - 460●
@types.Image::{ - 461
width: 0, - 462
height: 0, - 463
channels: 1, - 464●
data: Bytes::from_array([]), - 465
} - 466
} - 467
} - 468
- 469
// 简化:直接加权平均 - 470
let weights : Array[Double] = [] - 471●
for _ in 0..<images.length() { - 472●
weights.push(1.0 / images.length().to_double()) - 473
} - 474●
fuse_weighted_average(images, weights) - 475
} - 476
- 477
///| - 478
/// 图像融合 - 479
pub fn fuse_images_advanced( - 480
images : Array[@types.Image], - 481
config : FusionEnhancedConfig, - 482
) -> FusionResult { - 483●
let result = if config.algorithm == FusionAlgorithm::WeightedAverage { - 484●
fuse_weighted_average(images, config.weights) - 485●
} else if config.algorithm == FusionAlgorithm::Maximum { - 486●
fuse_maximum(images) - 487●
} else if config.algorithm == FusionAlgorithm::Minimum { - 488●
fuse_minimum(images) - 489●
} else if config.algorithm == FusionAlgorithm::SimpleMean { - 490●
fuse_simple_mean(images) - 491●
} else if config.algorithm == FusionAlgorithm::LaplacianPyramid { - 492●
fuse_laplacian_pyramid(images, config.pyramid_levels) - 493
} else { - 494●
fuse_simple_mean(images) - 495
} - 496
- 497
FusionResult::{ - 498
image: result, - 499
algorithm: config.algorithm, - 500●
num_images: images.length(), - 501
processing_time_ms: 0.0, - 502
fusion_quality: 0.0, - 503
} - 504
} - 505
- 506
///| - 507
/// 相位相关配准(简化版) - 508
pub fn register_phase_correlation( - 509
reference : @types.Image, - 510
target : @types.Image, - 511
) -> RegistrationResult { - 512
// 简化:计算平移量 - 513●
let w = reference.width - 514
let h = reference.height - 515
let mut best_x = 0 - 516
let mut best_y = 0 - 517
let mut best_similarity = 0.0 - 518
- 519
for dy in -10..<11 { - 520●
for dx in -10..<11 { - 521●
let mut similarity = 0.0 - 522
let mut count = 0 - 523
for y in 0..<h { - 524●
for x in 0..<w { - 525●
let tx = x + dx - 526
let ty = y + dy - 527
if tx >= 0 && tx < w && ty >= 0 && ty < h { - 528●
let p1 = reference.data[y * w + x].to_double() - 529●
let p2 = target.data[ty * w + tx].to_double() - 530●
let diff = (p1 - p2).abs() - 531
similarity += 1.0 - diff / 255.0 - 532
count += 1 - 533
} - 534
} - 535
} - 536
if count > 0 { - 537●
similarity /= count.to_double() - 538
if similarity > best_similarity { - 539●
best_similarity = similarity - 540
best_x = dx - 541
best_y = dy - 542
} - 543
} - 544
} - 545
} - 546
- 547
RegistrationResult::{ - 548
image: target, - 549
algorithm: RegistrationAlgorithm::PhaseCorrelation, - 550●
translation_x: best_x.to_double(), - 551●
translation_y: best_y.to_double(), - 552
rotation_angle: 0.0, - 553
scale_factor: 1.0, - 554
similarity: best_similarity, - 555
processing_time_ms: 0.0, - 556
} - 557
} - 558
- 559
///| - 560
/// 图像配准 - 561
pub fn register_images_advanced( - 562
reference : @types.Image, - 563
target : @types.Image, - 564
config : RegistrationAdvancedConfig, - 565
) -> RegistrationResult { - 566●
if config.algorithm == RegistrationAlgorithm::PhaseCorrelation { - 567●
register_phase_correlation(reference, target) - 568
} else { - 569●
register_phase_correlation(reference, target) - 570
} - 571
} - 572
- 573
///| - 574
/// 应用变换(平移) - 575
pub fn apply_translation( - 576
img : @types.Image, - 577
dx : Int32, - 578
dy : Int32, - 579
) -> @types.Image { - 580●
let w = img.width - 581
let h = img.height - 582
let out : Array[Byte] = [] - 583
- 584
for y in 0..<h { - 585●
for x in 0..<w { - 586●
let src_x = x - dx - 587
let src_y = y - dy - 588
if src_x >= 0 && src_x < w && src_y >= 0 && src_y < h { - 589●
out.push(img.data[src_y * w + src_x]) - 590
} else { - 591●
out.push(b'\x00') - 592
} - 593
} - 594
} - 595
- 596
@types.Image::{ - 597
width: w, - 598
height: h, - 599
channels: img.channels, - 600●
data: Bytes::from_array(out), - 601
} - 602
} - 603
- 604
///| - 605
/// 计算融合质量(空间频率) - 606
pub fn calculate_fusion_quality(fused : @types.Image) -> Double { - 607●
let w = fused.width - 608
let h = fused.height - 609
let mut sf = 0.0 - 610
- 611
for y in 0..<h { - 612●
for x in 0..<(w - 1) { - 613●
let diff = fused.data[y * w + x + 1].to_double() - - 614●
fused.data[y * w + x].to_double() - 615
sf += diff * diff - 616
} - 617
} - 618
- 619
for y in 0..<(h - 1) { - 620●
for x in 0..<w { - 621●
let diff = fused.data[(y + 1) * w + x].to_double() - - 622●
fused.data[y * w + x].to_double() - 623
sf += diff * diff - 624
} - 625
} - 626
- 627●
sf / (w * h).to_double() - 628
} - 629
- 630
///| - 631
/// 生成融合报告 - 632
pub fn fusion_report_to_string( - 633
result : FusionResult, - 634
quality : Double, - 635
) -> String { - 636●
let mut report = "## 图像融合报告\n\n" - 637
report = report + "### 处理信息\n\n" - 638
report = report + "| 指标 | 值 |\n|------|------|\n" - 639
let algo_str = if result.algorithm == FusionAlgorithm::WeightedAverage { - 640●
"加权平均" - 641●
} else if result.algorithm == FusionAlgorithm::LaplacianPyramid { - 642●
"拉普拉斯金字塔" - 643●
} else if result.algorithm == FusionAlgorithm::GaussianPyramid { - 644●
"高斯金字塔" - 645●
} else if result.algorithm == FusionAlgorithm::Wavelet { - 646●
"小波变换" - 647●
} else if result.algorithm == FusionAlgorithm::PCA { - 648●
"PCA" - 649●
} else if result.algorithm == FusionAlgorithm::IHS { - 650●
"IHS" - 651●
} else if result.algorithm == FusionAlgorithm::Brovey { - 652●
"Brovey" - 653●
} else if result.algorithm == FusionAlgorithm::SimpleMean { - 654●
"简单均值" - 655●
} else if result.algorithm == FusionAlgorithm::Maximum { - 656●
"最大值" - 657●
} else if result.algorithm == FusionAlgorithm::Minimum { - 658●
"最小值" - 659
} else { - 660●
"未知" - 661
} - 662
report = report + "| 算法 | " + algo_str + " |\n" - 663
report = report + - 664
"| 融合图像数 | " + - 665●
result.num_images.to_string() + - 666
" |\n" - 667
report = report + - 668
"| 处理耗时 | " + - 669●
result.processing_time_ms.to_string() + - 670
"ms |\n" - 671
report = report + - 672
"| 融合质量(空间频率) | " + - 673●
quality.to_string() + - 674
" |\n" - 675
- 676
report - 677
}