image.domain\remote_sensing\remote_sensing_enhancement.mbt
86.5% · 148 / 171 points · 719 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 遥感增强 remote_sensing/remote_sensing_enhancement - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 遥感图像增强(v20.0.0) - 7
// 遥感图像处理、地物分类、变化检测、植被指数、光谱分析 - 8
- 9
///| - 10
/// 遥感图像类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum RemoteSensingImageType { - 15
Optical - 16
Multispectral - 17
Hyperspectral - 18
SAR - 19
Thermal - 20
LiDAR - 21
Unknown - 22
} - 23
- 24
///| - 25
pub extend RemoteSensingImageType with Eq::{not_equal, equal} - 26
- 27
///| - 28
pub extend RemoteSensingImageType with @debug.Debug::{to_repr} - 29
- 30
///| - 31
/// 遥感图像 - 32
pub(all) struct RemoteSensingImage { - 33
width : Int32 - 34
height : Int32 - 35
bands : Int32 - 36
data : Array[Double] - 37
image_type : RemoteSensingImageType - 38
wavelength : Array[Double] // 每个波段的波长(nm) - 39
resolution : Double // 空间分辨率(米) - 40
acquisition_date : String - 41
cloud_cover : Double // 云量百分比 - 42
} - 43
- 44
///| - 45
/// 地物类别 - 46
/// `#non_exhaustive` 表示未来可能添加新构造器 - 47
#derive(Eq, @debug.Debug) - 48
#non_exhaustive - 49
pub(all) enum LandCoverClass { - 50
Water - 51
Forest - 52
Grassland - 53
Cropland - 54
Urban - 55
Barren - 56
Wetland - 57
Snow - 58
Cloud - 59
Shadow - 60
Unknown - 61
} - 62
- 63
///| - 64
pub extend LandCoverClass with Eq::{not_equal, equal} - 65
- 66
///| - 67
pub extend LandCoverClass with @debug.Debug::{to_repr} - 68
- 69
///| - 70
/// 分类结果 - 71
#derive(Eq, Default, @debug.Debug) - 72
pub(all) struct ClassificationResult { - 73
width : Int32 - 74
height : Int32 - 75
labels : Array[Int32] - 76
class_names : Array[String] - 77
class_colors : Array[(Int32, Int32, Int32)] - 78
num_classes : Int32 - 79
overall_accuracy : Double - 80
kappa_coefficient : Double - 81
} - 82
- 83
///| - 84
pub extend ClassificationResult with @debug.Debug::{to_repr} - 85
- 86
///| - 87
pub extend ClassificationResult with Eq::{not_equal, equal} - 88
- 89
///| - 90
pub extend ClassificationResult with Default::{default} - 91
- 92
///| - 93
/// 变化检测结果 - 94
#derive(Eq, Default, @debug.Debug) - 95
pub(all) struct ChangeDetectionResult { - 96
width : Int32 - 97
height : Int32 - 98
change_mask : Array[Bool] - 99
change_percentage : Double - 100
num_changed_pixels : Int32 - 101
change_intensity : Array[Double] - 102
threshold : Double - 103
} - 104
- 105
///| - 106
pub extend ChangeDetectionResult with @debug.Debug::{to_repr} - 107
- 108
///| - 109
pub extend ChangeDetectionResult with Eq::{not_equal, equal} - 110
- 111
///| - 112
pub extend ChangeDetectionResult with Default::{default} - 113
- 114
///| - 115
/// 植被指数 - 116
#derive(Eq, Default, @debug.Debug) - 117
pub(all) struct VegetationIndices { - 118
ndvi : Array[Double] // 归一化植被指数 - 119
ndwi : Array[Double] // 归一化水体指数 - 120
ndbi : Array[Double] // 归一化建筑指数 - 121
evi : Array[Double] // 增强植被指数 - 122
savi : Array[Double] // 土壤调整植被指数 - 123
msavi : Array[Double] // 修正土壤调整植被指数 - 124
} - 125
- 126
///| - 127
pub extend VegetationIndices with @debug.Debug::{to_repr} - 128
- 129
///| - 130
pub extend VegetationIndices with Eq::{not_equal, equal} - 131
- 132
///| - 133
pub extend VegetationIndices with Default::{default} - 134
- 135
///| - 136
/// 光谱特征 - 137
#derive(Eq, Default, @debug.Debug) - 138
pub(all) struct SpectralSignature { - 139
wavelengths : Array[Double] - 140
reflectance : Array[Double] - 141
num_bands : Int32 - 142
mean_reflectance : Double - 143
std_reflectance : Double - 144
absorption_features : Array[(Double, Double)] // (波长, 深度) - 145
} - 146
- 147
///| - 148
pub extend SpectralSignature with @debug.Debug::{to_repr} - 149
- 150
///| - 151
pub extend SpectralSignature with Eq::{not_equal, equal} - 152
- 153
///| - 154
pub extend SpectralSignature with Default::{default} - 155
- 156
///| - 157
/// 纹理特征 - 158
#derive(Eq, Default, @debug.Debug) - 159
pub(all) struct TextureFeatures { - 160
contrast : Double - 161
homogeneity : Double - 162
energy : Double - 163
correlation : Double - 164
entropy : Double - 165
variance : Double - 166
dissimilarity : Double - 167
} - 168
- 169
///| - 170
pub extend TextureFeatures with @debug.Debug::{to_repr} - 171
- 172
///| - 173
pub extend TextureFeatures with Eq::{not_equal, equal} - 174
- 175
///| - 176
pub extend TextureFeatures with Default::{default} - 177
- 178
///| - 179
/// 遥感图像质量 - 180
#derive(Eq, Default, @debug.Debug) - 181
pub(all) struct RemoteSensingQuality { - 182
snr : Double - 183
cloud_cover : Double - 184
geometric_accuracy : Double - 185
radiometric_accuracy : Double - 186
overall_quality : Double - 187
} - 188
- 189
///| - 190
pub extend RemoteSensingQuality with @debug.Debug::{to_repr} - 191
- 192
///| - 193
pub extend RemoteSensingQuality with Eq::{not_equal, equal} - 194
- 195
///| - 196
pub extend RemoteSensingQuality with Default::{default} - 197
- 198
///| - 199
/// 创建默认遥感图像 - 200
pub fn create_default_remote_sensing_image( - 201
width : Int32, - 202
height : Int32, - 203
bands : Int32, - 204
) -> RemoteSensingImage { - 205●
let data : Array[Double] = [] - 206
let wavelength : Array[Double] = [] - 207
for _ in 0..<(width * height * bands) { - 208●
data.push(0.0) - 209
} - 210
for i in 0..<bands { - 211●
wavelength.push(400.0 + i.to_double() * 100.0) - 212
} - 213
RemoteSensingImage::{ - 214
width, - 215
height, - 216
bands, - 217
data, - 218
image_type: RemoteSensingImageType::Multispectral, - 219
wavelength, - 220
resolution: 10.0, - 221
acquisition_date: "2024-01-01", - 222
cloud_cover: 0.0, - 223
} - 224
} - 225
- 226
///| - 227
/// 计算 NDVI(归一化植被指数) - 228
pub fn calculate_ndvi( - 229
image : RemoteSensingImage, - 230
red_band : Int32, - 231
nir_band : Int32, - 232
) -> Array[Double] { - 233●
let w = image.width - 234
let h = image.height - 235
let total = w * h - 236
let ndvi : Array[Double] = [] - 237
- 238
for i in 0..<total { - 239●
let red = image.data[i * image.bands + red_band] - 240
let nir = image.data[i * image.bands + nir_band] - 241
let denominator = nir + red - 242
if denominator > 0.001 { - 243●
ndvi.push((nir - red) / denominator) - 244
} else { - 245●
ndvi.push(0.0) - 246
} - 247
} - 248
- 249
ndvi - 250
} - 251
- 252
///| - 253
/// 计算 NDWI(归一化水体指数) - 254
pub fn calculate_ndwi( - 255
image : RemoteSensingImage, - 256
green_band : Int32, - 257
nir_band : Int32, - 258
) -> Array[Double] { - 259●
let w = image.width - 260
let h = image.height - 261
let total = w * h - 262
let ndwi : Array[Double] = [] - 263
- 264
for i in 0..<total { - 265●
let green = image.data[i * image.bands + green_band] - 266
let nir = image.data[i * image.bands + nir_band] - 267
let denominator = nir + green - 268
if denominator > 0.001 { - 269●
ndwi.push((green - nir) / denominator) - 270
} else { - 271●
ndwi.push(0.0) - 272
} - 273
} - 274
- 275
ndwi - 276
} - 277
- 278
///| - 279
/// 计算 NDBI(归一化建筑指数) - 280
pub fn calculate_ndbi( - 281
image : RemoteSensingImage, - 282
swir_band : Int32, - 283
nir_band : Int32, - 284
) -> Array[Double] { - 285●
let w = image.width - 286
let h = image.height - 287
let total = w * h - 288
let ndbi : Array[Double] = [] - 289
- 290
for i in 0..<total { - 291●
let swir = image.data[i * image.bands + swir_band] - 292
let nir = image.data[i * image.bands + nir_band] - 293
let denominator = swir + nir - 294
if denominator > 0.001 { - 295●
ndbi.push((swir - nir) / denominator) - 296
} else { - 297●
ndbi.push(0.0) - 298
} - 299
} - 300
- 301
ndbi - 302
} - 303
- 304
///| - 305
/// 计算所有植被指数 - 306
pub fn calculate_all_vegetation_indices( - 307
image : RemoteSensingImage, - 308
) -> VegetationIndices { - 309●
let ndvi = if image.bands > 4 { calculate_ndvi(image, 2, 3) } else { [] } - 310●
let ndwi = if image.bands > 4 { calculate_ndwi(image, 1, 3) } else { [] } - 311●
let ndbi = if image.bands > 5 { calculate_ndbi(image, 4, 3) } else { [] } - 312
- 313
VegetationIndices::{ ndvi, ndwi, ndbi, evi: ndvi, savi: ndvi, msavi: ndvi, } - 314
} - 315
- 316
///| - 317
/// 简单地物分类(基于光谱指数的决策树) - 318
pub fn classify_land_cover_simple( - 319
image : RemoteSensingImage, - 320
) -> ClassificationResult { - 321●
let w = image.width - 322
let h = image.height - 323
let total = w * h - 324
let labels : Array[Int32] = [] - 325
- 326●
let ndvi = if image.bands > 4 { calculate_ndvi(image, 2, 3) } else { [] } - 327●
let ndwi = if image.bands > 4 { calculate_ndwi(image, 1, 3) } else { [] } - 328
- 329
for i in 0..<total { - 330●
let mut label = 10 // Unknown - 331●
if ndvi.length() > 0 && ndwi.length() > 0 { - 332●
let n = ndvi[i] - 333
let w_idx = ndwi[i] - 334
if w_idx > 0.3 { - 335○
label = 0 // Water - 336●
} else if n > 0.6 { - 337○
label = 1 // Forest - 338●
} else if n > 0.3 { - 339○
label = 2 // Grassland - 340●
} else if n > 0.1 { - 341○
label = 3 // Cropland - 342●
} else if n < -0.1 { - 343●
label = 4 // Urban - 344
} else { - 345●
label = 5 // Barren - 346
} - 347
} - 348●
labels.push(label) - 349
} - 350
- 351
let class_names : Array[String] = [ - 352
"Water", "Forest", "Grassland", "Cropland", "Urban", "Barren", "Wetland", "Snow", - 353
"Cloud", "Shadow", "Unknown", - 354
] - 355
let class_colors : Array[(Int32, Int32, Int32)] = [ - 356
(0, 0, 255), - 357
(0, 128, 0), - 358
(128, 255, 0), - 359
(255, 255, 0), - 360
(255, 0, 0), - 361
(128, 128, 128), - 362
(0, 255, 255), - 363
(255, 255, 255), - 364
(200, 200, 200), - 365
(50, 50, 50), - 366
(0, 0, 0), - 367
] - 368
- 369
ClassificationResult::{ - 370
width: w, - 371
height: h, - 372
labels, - 373
class_names, - 374
class_colors, - 375
num_classes: 11, - 376
overall_accuracy: 0.0, - 377
kappa_coefficient: 0.0, - 378
} - 379
} - 380
- 381
///| - 382
/// 变化检测(基于图像差值) - 383
pub fn detect_changes_simple( - 384
image1 : RemoteSensingImage, - 385
image2 : RemoteSensingImage, - 386
threshold : Double, - 387
) -> ChangeDetectionResult { - 388●
let w = image1.width - 389
let h = image1.height - 390
let total = w * h - 391
let change_mask : Array[Bool] = [] - 392
let change_intensity : Array[Double] = [] - 393
let mut num_changed = 0 - 394
- 395
for i in 0..<total { - 396●
let mut sum_diff = 0.0 - 397
for b in 0..<image1.bands { - 398●
let idx = i * image1.bands + b - 399●
if idx < image1.data.length() && idx < image2.data.length() { - 400●
let diff = image1.data[idx] - image2.data[idx] - 401
sum_diff += diff * diff - 402
} - 403
} - 404
let intensity = if image1.bands > 0 { - 405●
(sum_diff / image1.bands.to_double()).sqrt() - 406
} else { - 407○
0.0 - 408
} - 409●
change_intensity.push(intensity) - 410
let changed = intensity > threshold - 411●
change_mask.push(changed) - 412
if changed { - 413●
num_changed += 1 - 414
} - 415
} - 416
- 417
let change_percentage = if total > 0 { - 418●
num_changed.to_double() / total.to_double() * 100.0 - 419
} else { - 420○
0.0 - 421
} - 422
- 423
ChangeDetectionResult::{ - 424
width: w, - 425
height: h, - 426
change_mask, - 427
change_percentage, - 428
num_changed_pixels: num_changed, - 429
change_intensity, - 430
threshold, - 431
} - 432
} - 433
- 434
///| - 435
/// 计算纹理特征(基于灰度共生矩阵简化版) - 436
pub fn calculate_texture_features( - 437
image : @types.Image, - 438
x : Int32, - 439
y : Int32, - 440
window_size : Int32, - 441
) -> TextureFeatures raise @types.LoadError { - 442
// 输入验证 - 443●
if image.width <= 0 || image.height <= 0 { - 444●
raise @types.LoadError::DecodeFailed( - 445
"calculate_texture_features: 图像尺寸必须大于0", - 446
) - 447
} - 448
if image.channels < 1 || image.channels > 4 { - 449●
raise @types.LoadError::DecodeFailed( - 450
"calculate_texture_features: 通道数必须在1-4之间", - 451
) - 452
} - 453
let expected_size = image.width * image.height * image.channels - 454●
if image.data.length() != expected_size { - 455●
raise @types.LoadError::DecodeFailed( - 456
"calculate_texture_features: 数据大小不匹配", - 457
) - 458
} - 459
let w = image.width - 460
let h = image.height - 461
let half = window_size / 2 - 462
let mut sum = 0.0 - 463
let mut sum_sq = 0.0 - 464
let mut count = 0 - 465
let histogram : Array[Int32] = [] - 466
for _ in 0..<256 { - 467●
histogram.push(0) - 468
} - 469
- 470
for dy in -half..<(half + 1) { - 471●
for dx in -half..<(half + 1) { - 472●
let nx = x + dx - 473
let ny = y + dy - 474
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 475●
let pixel = image.data[ny * w + nx].to_double() - 476
sum += pixel - 477
sum_sq += pixel * pixel - 478
count += 1 - 479●
histogram[pixel.to_int()] += 1 - 480
} - 481
} - 482
} - 483
- 484●
let mean = if count > 0 { sum / count.to_double() } else { 0.0 } - 485
let variance = if count > 0 { - 486●
sum_sq / count.to_double() - mean * mean - 487
} else { - 488●
0.0 - 489
} - 490
- 491
// 计算熵 - 492
let mut entropy = 0.0 - 493
for i in 0..<256 { - 494●
if histogram[i] > 0 && count > 0 { - 495●
let p = histogram[i].to_double() / count.to_double() - 496
entropy -= p * (p * 10.0) - 497
} - 498
} - 499
- 500
// 简化的对比度、同质性、能量、相关性 - 501
let contrast = variance - 502●
let homogeneity = if variance > 0.0 { 1.0 / (1.0 + variance) } else { 1.0 } - 503
let energy = if count > 0 { - 504●
let mut e = 0.0 - 505
for i in 0..<256 { - 506●
let p = histogram[i].to_double() / count.to_double() - 507
e += p * p - 508
} - 509
e - 510
} else { - 511●
0.0 - 512
} - 513
let correlation = 0.5 - 514●
let dissimilarity = variance.sqrt() - 515
- 516
TextureFeatures::{ - 517
contrast, - 518
homogeneity, - 519
energy, - 520
correlation, - 521
entropy, - 522
variance, - 523
dissimilarity, - 524
} - 525
} - 526
- 527
///| - 528
/// 计算光谱特征 - 529
pub fn calculate_spectral_signature( - 530
image : RemoteSensingImage, - 531
x : Int32, - 532
y : Int32, - 533
) -> SpectralSignature { - 534●
let reflectance : Array[Double] = [] - 535
let wavelengths : Array[Double] = [] - 536
let mut sum = 0.0 - 537
let mut sum_sq = 0.0 - 538
- 539
for b in 0..<image.bands { - 540●
let idx = (y * image.width + x) * image.bands + b - 541●
if idx < image.data.length() { - 542●
let value = image.data[idx] - 543●
reflectance.push(value) - 544●
wavelengths.push(image.wavelength[b]) - 545
sum += value - 546
sum_sq += value * value - 547
} - 548
} - 549
- 550◐
let mean = if image.bands > 0 { sum / image.bands.to_double() } else { 0.0 } - 551
let variance = if image.bands > 0 { - 552●
sum_sq / image.bands.to_double() - mean * mean - 553
} else { - 554○
0.0 - 555
} - 556●
let std = if variance > 0.0 { variance.sqrt() } else { 0.0 } - 557
- 558
// 简化的吸收特征检测 - 559
let absorption_features : Array[(Double, Double)] = [] - 560
for b in 1..<(image.bands - 1) { - 561●
if b < reflectance.length() { - 562●
let prev = reflectance[b - 1] - 563
let curr = reflectance[b] - 564
let next = reflectance[b + 1] - 565
if curr < prev && curr < next { - 566○
let depth = (prev + next) / 2.0 - curr - 567
if depth > 0.01 { - 568○
absorption_features.push((wavelengths[b], depth)) - 569
} - 570
} - 571
} - 572
} - 573
- 574
SpectralSignature::{ - 575
wavelengths, - 576
reflectance, - 577
num_bands: image.bands, - 578
mean_reflectance: mean, - 579
std_reflectance: std, - 580
absorption_features, - 581
} - 582
} - 583
- 584
///| - 585
/// 计算遥感图像质量 - 586
pub fn calculate_remote_sensing_quality( - 587
image : RemoteSensingImage, - 588
) -> RemoteSensingQuality { - 589●
let w = image.width - 590
let h = image.height - 591
let total = w * h - 592
let mut sum = 0.0 - 593
let mut sum_sq = 0.0 - 594
- 595
for i in 0..<total { - 596●
if i < image.data.length() { - 597●
let pixel = image.data[i] - 598
sum += pixel - 599
sum_sq += pixel * pixel - 600
} - 601
} - 602
- 603◐
let mean = if total > 0 { sum / total.to_double() } else { 0.0 } - 604
let variance = if total > 0 { - 605●
sum_sq / total.to_double() - mean * mean - 606
} else { - 607○
0.0 - 608
} - 609●
let std = if variance > 0.0 { variance.sqrt() } else { 1.0 } - 610◐
let snr = if std > 0.0 { mean / std } else { 0.0 } - 611
- 612
let overall_quality = if snr > 10.0 && image.cloud_cover < 10.0 { - 613●
5.0 - 614●
} else if snr > 5.0 && image.cloud_cover < 30.0 { - 615●
3.0 - 616
} else { - 617●
2.0 - 618
} - 619
- 620
RemoteSensingQuality::{ - 621
snr, - 622
cloud_cover: image.cloud_cover, - 623
geometric_accuracy: 0.8, - 624
radiometric_accuracy: 0.85, - 625
overall_quality, - 626
} - 627
} - 628
- 629
///| - 630
/// 生成遥感图像分析报告 - 631
pub fn remote_sensing_report_to_string( - 632
image : RemoteSensingImage, - 633
classification : ClassificationResult, - 634
changes : ChangeDetectionResult, - 635
quality : RemoteSensingQuality, - 636
) -> String { - 637●
let mut report = "## 遥感图像分析报告\n\n" - 638
report = report + "### 图像信息\n\n" - 639
report = report + "| 指标 | 值 |\n|------|------|\n" - 640
let type_str = if image.image_type == RemoteSensingImageType::Optical { - 641●
"光学" - 642●
} else if image.image_type == RemoteSensingImageType::Multispectral { - 643●
"多光谱" - 644○
} else if image.image_type == RemoteSensingImageType::Hyperspectral { - 645○
"高光谱" - 646○
} else if image.image_type == RemoteSensingImageType::SAR { - 647○
"SAR" - 648○
} else if image.image_type == RemoteSensingImageType::Thermal { - 649○
"热红外" - 650○
} else if image.image_type == RemoteSensingImageType::LiDAR { - 651○
"激光雷达" - 652
} else { - 653○
"未知" - 654
} - 655
report = report + "| 类型 | " + type_str + " |\n" - 656
report = report + - 657
"| 尺寸 | " + - 658●
image.width.to_string() + - 659
"x" + - 660●
image.height.to_string() + - 661
" |\n" - 662●
report = report + "| 波段数 | " + image.bands.to_string() + " |\n" - 663
report = report + - 664
"| 空间分辨率 | " + - 665●
image.resolution.to_string() + - 666
"m |\n" - 667
report = report + "| 获取日期 | " + image.acquisition_date + " |\n" - 668●
report = report + "| 云量 | " + image.cloud_cover.to_string() + "% |\n\n" - 669
- 670
report = report + "### 图像质量\n\n" - 671
report = report + "| 指标 | 值 |\n|------|------|\n" - 672●
report = report + "| SNR | " + quality.snr.to_string() + " dB |\n" - 673●
report = report + "| 云量 | " + quality.cloud_cover.to_string() + "% |\n" - 674
report = report + - 675
"| 几何精度 | " + - 676●
(quality.geometric_accuracy * 100.0).to_string() + - 677
"% |\n" - 678
report = report + - 679
"| 辐射精度 | " + - 680●
(quality.radiometric_accuracy * 100.0).to_string() + - 681
"% |\n" - 682
report = report + - 683
"| 综合质量 | " + - 684●
quality.overall_quality.to_string() + - 685
"/5.0 |\n\n" - 686
- 687
report = report + "### 地物分类统计\n\n" - 688
report = report + "| 类别 | 颜色(RGB) |\n|------|-----------|\n" - 689
for i in 0..<classification.num_classes { - 690●
if i < classification.class_names.length() && - 691●
i < classification.class_colors.length() { - 692●
let color = classification.class_colors[i] - 693
report = report + - 694
"| " + - 695
classification.class_names[i] + - 696
" | " + - 697●
color.0.to_string() + - 698
"," + - 699●
color.1.to_string() + - 700
"," + - 701●
color.2.to_string() + - 702
" |\n" - 703
} - 704
} - 705
- 706
report = report + "\n### 变化检测\n\n" - 707
report = report + "| 指标 | 值 |\n|------|------|\n" - 708
report = report + - 709
"| 变化像素数 | " + - 710●
changes.num_changed_pixels.to_string() + - 711
" |\n" - 712
report = report + - 713
"| 变化百分比 | " + - 714●
changes.change_percentage.to_string() + - 715
"% |\n" - 716●
report = report + "| 变化阈值 | " + changes.threshold.to_string() + " |\n" - 717
- 718
report - 719
}