image.domain\remote_sensing\remote_sensing.mbt
94.6% · 191 / 202 points · 672 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 - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 遥感图像处理(v12.11.0) - 7
// NDVI、假彩色合成、多光谱指数计算 - 8
- 9
///| - 10
/// 多光谱图像波段 - 11
pub(all) struct MultispectralImage { - 12
width : Int32 - 13
height : Int32 - 14
bands : Array[@types.Image] // 每个波段一个灰度图像 - 15
band_names : Array[String] // 波段名称 - 16
} - 17
- 18
///| - 19
/// 创建多光谱图像 - 20
pub fn multispectral_new( - 21
width : Int32, - 22
height : Int32, - 23
bands : Array[@types.Image], - 24
band_names~ : Array[String] = [], - 25
) -> MultispectralImage { - 26●
let names = if band_names.length() > 0 { - 27●
band_names - 28
} else { - 29●
let default_names : Array[String] = [] - 30●
for i in 0..<bands.length() { - 31●
default_names.push("Band" + i.to_string()) - 32
} - 33
default_names - 34
} - 35
MultispectralImage::{ width, height, bands, band_names: names, } - 36
} - 37
- 38
///| - 39
/// 计算 NDVI(归一化植被指数) - 40
/// 描述 NDVI = (NIR - Red) / (NIR + Red) - 41
pub fn compute_ndvi( - 42
nir : @types.Image, - 43
red : @types.Image, - 44
) -> @types.Image raise @types.LoadError { - 45
// 输入验证 - 46●
if nir.width <= 0 || nir.height <= 0 { - 47●
raise @types.LoadError::DecodeFailed( - 48
"compute_ndvi: 图像尺寸必须大于0", - 49
) - 50
} - 51
if nir.channels < 1 || nir.channels > 4 { - 52●
raise @types.LoadError::DecodeFailed( - 53
"compute_ndvi: 通道数必须在1-4之间", - 54
) - 55
} - 56
let expected_size_nir = nir.width * nir.height * nir.channels - 57●
if nir.data.length() != expected_size_nir { - 58●
raise @types.LoadError::DecodeFailed("compute_ndvi: 数据大小不匹配") - 59
} - 60
if red.width <= 0 || red.height <= 0 { - 61●
raise @types.LoadError::DecodeFailed( - 62
"compute_ndvi: 图像尺寸必须大于0", - 63
) - 64
} - 65
if red.channels < 1 || red.channels > 4 { - 66●
raise @types.LoadError::DecodeFailed( - 67
"compute_ndvi: 通道数必须在1-4之间", - 68
) - 69
} - 70
let expected_size_red = red.width * red.height * red.channels - 71●
if red.data.length() != expected_size_red { - 72●
raise @types.LoadError::DecodeFailed("compute_ndvi: 数据大小不匹配") - 73
} - 74
if nir.width != red.width || nir.height != red.height { - 75●
return nir - 76
} - 77
let w = nir.width - 78
let h = nir.height - 79●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 80
for y in 0..<h { - 81●
for x in 0..<w { - 82●
let idx = y * w + x - 83●
let nir_val = nir.data[idx].to_double() - 84●
let red_val = red.data[idx].to_double() - 85
let sum = nir_val + red_val - 86●
let ndvi = if sum > 0.0 { (nir_val - red_val) / sum } else { 0.0 } - 87
// 映射到 0-255(NDVI 范围 -1 到 1) - 88●
let mapped = ((ndvi + 1.0) / 2.0 * 255.0).to_int() - 89●
out_data[idx] = mapped.min(255).max(0).to_byte() - 90
} - 91
} - 92
@types.Image::{ - 93
width: w, - 94
height: h, - 95
channels: 1, - 96●
data: Bytes::from_array(out_data), - 97
} - 98
} - 99
- 100
///| - 101
/// 计算 NDWI(归一化水指数) - 102
/// 描述 NDWI = (Green - NIR) / (Green + NIR) - 103
pub fn compute_ndwi( - 104
green : @types.Image, - 105
nir : @types.Image, - 106
) -> @types.Image raise @types.LoadError { - 107
// 输入验证 - 108●
if green.width <= 0 || green.height <= 0 { - 109●
raise @types.LoadError::DecodeFailed( - 110
"compute_ndwi: 图像尺寸必须大于0", - 111
) - 112
} - 113
if green.channels < 1 || green.channels > 4 { - 114●
raise @types.LoadError::DecodeFailed( - 115
"compute_ndwi: 通道数必须在1-4之间", - 116
) - 117
} - 118
let expected_size_green = green.width * green.height * green.channels - 119●
if green.data.length() != expected_size_green { - 120●
raise @types.LoadError::DecodeFailed("compute_ndwi: 数据大小不匹配") - 121
} - 122
if nir.width <= 0 || nir.height <= 0 { - 123●
raise @types.LoadError::DecodeFailed( - 124
"compute_ndwi: 图像尺寸必须大于0", - 125
) - 126
} - 127
if nir.channels < 1 || nir.channels > 4 { - 128●
raise @types.LoadError::DecodeFailed( - 129
"compute_ndwi: 通道数必须在1-4之间", - 130
) - 131
} - 132
let expected_size_nir = nir.width * nir.height * nir.channels - 133●
if nir.data.length() != expected_size_nir { - 134●
raise @types.LoadError::DecodeFailed("compute_ndwi: 数据大小不匹配") - 135
} - 136
if green.width != nir.width || green.height != nir.height { - 137●
return green - 138
} - 139
let w = green.width - 140
let h = green.height - 141●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 142
for y in 0..<h { - 143●
for x in 0..<w { - 144●
let idx = y * w + x - 145●
let green_val = green.data[idx].to_double() - 146●
let nir_val = nir.data[idx].to_double() - 147
let sum = green_val + nir_val - 148●
let ndwi = if sum > 0.0 { (green_val - nir_val) / sum } else { 0.0 } - 149●
let mapped = ((ndwi + 1.0) / 2.0 * 255.0).to_int() - 150●
out_data[idx] = mapped.min(255).max(0).to_byte() - 151
} - 152
} - 153
@types.Image::{ - 154
width: w, - 155
height: h, - 156
channels: 1, - 157●
data: Bytes::from_array(out_data), - 158
} - 159
} - 160
- 161
///| - 162
/// 计算 NDBI(归一化建筑指数) - 163
/// 描述 NDBI = (SWIR - NIR) / (SWIR + NIR) - 164
pub fn compute_ndbi( - 165
swir : @types.Image, - 166
nir : @types.Image, - 167
) -> @types.Image raise @types.LoadError { - 168
// 输入验证 - 169●
if swir.width <= 0 || swir.height <= 0 { - 170●
raise @types.LoadError::DecodeFailed( - 171
"compute_ndbi: 图像尺寸必须大于0", - 172
) - 173
} - 174
if swir.channels < 1 || swir.channels > 4 { - 175●
raise @types.LoadError::DecodeFailed( - 176
"compute_ndbi: 通道数必须在1-4之间", - 177
) - 178
} - 179
let expected_size_swir = swir.width * swir.height * swir.channels - 180●
if swir.data.length() != expected_size_swir { - 181●
raise @types.LoadError::DecodeFailed("compute_ndbi: 数据大小不匹配") - 182
} - 183
if nir.width <= 0 || nir.height <= 0 { - 184●
raise @types.LoadError::DecodeFailed( - 185
"compute_ndbi: 图像尺寸必须大于0", - 186
) - 187
} - 188
if nir.channels < 1 || nir.channels > 4 { - 189●
raise @types.LoadError::DecodeFailed( - 190
"compute_ndbi: 通道数必须在1-4之间", - 191
) - 192
} - 193
let expected_size_nir = nir.width * nir.height * nir.channels - 194●
if nir.data.length() != expected_size_nir { - 195●
raise @types.LoadError::DecodeFailed("compute_ndbi: 数据大小不匹配") - 196
} - 197
if swir.width != nir.width || swir.height != nir.height { - 198●
return swir - 199
} - 200
let w = swir.width - 201
let h = swir.height - 202●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 203
for y in 0..<h { - 204●
for x in 0..<w { - 205●
let idx = y * w + x - 206●
let swir_val = swir.data[idx].to_double() - 207●
let nir_val = nir.data[idx].to_double() - 208
let sum = swir_val + nir_val - 209●
let ndbi = if sum > 0.0 { (swir_val - nir_val) / sum } else { 0.0 } - 210●
let mapped = ((ndbi + 1.0) / 2.0 * 255.0).to_int() - 211●
out_data[idx] = mapped.min(255).max(0).to_byte() - 212
} - 213
} - 214
@types.Image::{ - 215
width: w, - 216
height: h, - 217
channels: 1, - 218●
data: Bytes::from_array(out_data), - 219
} - 220
} - 221
- 222
///| - 223
/// 计算 EVI(增强型植被指数) - 224
/// 描述 EVI = 2.5 * (NIR - Red) / (NIR + 6*Red - 7.5*Blue + 1) - 225
pub fn compute_evi( - 226
nir : @types.Image, - 227
red : @types.Image, - 228
blue : @types.Image, - 229
) -> @types.Image raise @types.LoadError { - 230
// 输入验证 - 231●
if nir.width <= 0 || nir.height <= 0 { - 232●
raise @types.LoadError::DecodeFailed( - 233
"compute_evi: 图像尺寸必须大于0", - 234
) - 235
} - 236
if nir.channels < 1 || nir.channels > 4 { - 237●
raise @types.LoadError::DecodeFailed( - 238
"compute_evi: 通道数必须在1-4之间", - 239
) - 240
} - 241
let expected_size_nir = nir.width * nir.height * nir.channels - 242●
if nir.data.length() != expected_size_nir { - 243●
raise @types.LoadError::DecodeFailed("compute_evi: 数据大小不匹配") - 244
} - 245
if red.width <= 0 || red.height <= 0 { - 246●
raise @types.LoadError::DecodeFailed( - 247
"compute_evi: 图像尺寸必须大于0", - 248
) - 249
} - 250
if red.channels < 1 || red.channels > 4 { - 251●
raise @types.LoadError::DecodeFailed( - 252
"compute_evi: 通道数必须在1-4之间", - 253
) - 254
} - 255
let expected_size_red = red.width * red.height * red.channels - 256●
if red.data.length() != expected_size_red { - 257●
raise @types.LoadError::DecodeFailed("compute_evi: 数据大小不匹配") - 258
} - 259
if blue.width <= 0 || blue.height <= 0 { - 260●
raise @types.LoadError::DecodeFailed( - 261
"compute_evi: 图像尺寸必须大于0", - 262
) - 263
} - 264
if blue.channels < 1 || blue.channels > 4 { - 265●
raise @types.LoadError::DecodeFailed( - 266
"compute_evi: 通道数必须在1-4之间", - 267
) - 268
} - 269
let expected_size_blue = blue.width * blue.height * blue.channels - 270●
if blue.data.length() != expected_size_blue { - 271●
raise @types.LoadError::DecodeFailed("compute_evi: 数据大小不匹配") - 272
} - 273
if nir.width != red.width || - 274
nir.height != red.height || - 275
nir.width != blue.width || - 276
nir.height != blue.height { - 277●
return nir - 278
} - 279
let w = nir.width - 280
let h = nir.height - 281●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 282
for y in 0..<h { - 283●
for x in 0..<w { - 284●
let idx = y * w + x - 285●
let nir_val = nir.data[idx].to_double() - 286●
let red_val = red.data[idx].to_double() - 287●
let blue_val = blue.data[idx].to_double() - 288
let denominator = nir_val + 6.0 * red_val - 7.5 * blue_val + 1.0 - 289●
let evi = if denominator.abs() > 0.0001 { - 290●
2.5 * (nir_val - red_val) / denominator - 291
} else { - 292●
0.0 - 293
} - 294●
let mapped = ((evi + 1.0) / 2.0 * 255.0).to_int() - 295●
out_data[idx] = mapped.min(255).max(0).to_byte() - 296
} - 297
} - 298
@types.Image::{ - 299
width: w, - 300
height: h, - 301
channels: 1, - 302●
data: Bytes::from_array(out_data), - 303
} - 304
} - 305
- 306
///| - 307
/// 假彩色合成(标准假彩色:NIR=红, Red=绿, Green=蓝) - 308
pub fn false_color_composite( - 309
nir : @types.Image, - 310
red : @types.Image, - 311
green : @types.Image, - 312
) -> @types.Image raise @types.LoadError { - 313
// 输入验证 - 314●
if nir.width <= 0 || nir.height <= 0 { - 315●
raise @types.LoadError::DecodeFailed( - 316
"false_color_composite: 图像尺寸必须大于0", - 317
) - 318
} - 319
if nir.channels < 1 || nir.channels > 4 { - 320●
raise @types.LoadError::DecodeFailed( - 321
"false_color_composite: 通道数必须在1-4之间", - 322
) - 323
} - 324
let expected_size_nir = nir.width * nir.height * nir.channels - 325●
if nir.data.length() != expected_size_nir { - 326●
raise @types.LoadError::DecodeFailed( - 327
"false_color_composite: 数据大小不匹配", - 328
) - 329
} - 330
if red.width <= 0 || red.height <= 0 { - 331●
raise @types.LoadError::DecodeFailed( - 332
"false_color_composite: 图像尺寸必须大于0", - 333
) - 334
} - 335
if red.channels < 1 || red.channels > 4 { - 336●
raise @types.LoadError::DecodeFailed( - 337
"false_color_composite: 通道数必须在1-4之间", - 338
) - 339
} - 340
let expected_size_red = red.width * red.height * red.channels - 341●
if red.data.length() != expected_size_red { - 342●
raise @types.LoadError::DecodeFailed( - 343
"false_color_composite: 数据大小不匹配", - 344
) - 345
} - 346
if green.width <= 0 || green.height <= 0 { - 347●
raise @types.LoadError::DecodeFailed( - 348
"false_color_composite: 图像尺寸必须大于0", - 349
) - 350
} - 351
if green.channels < 1 || green.channels > 4 { - 352●
raise @types.LoadError::DecodeFailed( - 353
"false_color_composite: 通道数必须在1-4之间", - 354
) - 355
} - 356
let expected_size_green = green.width * green.height * green.channels - 357●
if green.data.length() != expected_size_green { - 358●
raise @types.LoadError::DecodeFailed( - 359
"false_color_composite: 数据大小不匹配", - 360
) - 361
} - 362
if nir.width != red.width || - 363
nir.height != red.height || - 364
nir.width != green.width || - 365
nir.height != green.height { - 366●
return nir - 367
} - 368
let w = nir.width - 369
let h = nir.height - 370●
let out_data : Array[Byte] = Array::make(w * h * 3, b'\x00') - 371
for y in 0..<h { - 372●
for x in 0..<w { - 373●
let idx = y * w + x - 374
let out_idx = idx * 3 - 375
// 标准假彩色:NIR -> 红, Red -> 绿, Green -> 蓝 - 376
out_data[out_idx] = nir.data[idx] - 377
out_data[out_idx + 1] = red.data[idx] - 378
out_data[out_idx + 2] = green.data[idx] - 379
} - 380
} - 381
@types.Image::{ - 382
width: w, - 383
height: h, - 384
channels: 3, - 385●
data: Bytes::from_array(out_data), - 386
} - 387
} - 388
- 389
///| - 390
/// 真彩色合成(Red=红, Green=绿, Blue=蓝) - 391
pub fn true_color_composite( - 392
red : @types.Image, - 393
green : @types.Image, - 394
blue : @types.Image, - 395
) -> @types.Image raise @types.LoadError { - 396
// 输入验证 - 397●
if red.width <= 0 || red.height <= 0 { - 398●
raise @types.LoadError::DecodeFailed( - 399
"true_color_composite: 图像尺寸必须大于0", - 400
) - 401
} - 402
if red.channels < 1 || red.channels > 4 { - 403●
raise @types.LoadError::DecodeFailed( - 404
"true_color_composite: 通道数必须在1-4之间", - 405
) - 406
} - 407
let expected_size_red = red.width * red.height * red.channels - 408●
if red.data.length() != expected_size_red { - 409●
raise @types.LoadError::DecodeFailed( - 410
"true_color_composite: 数据大小不匹配", - 411
) - 412
} - 413
if green.width <= 0 || green.height <= 0 { - 414●
raise @types.LoadError::DecodeFailed( - 415
"true_color_composite: 图像尺寸必须大于0", - 416
) - 417
} - 418
if green.channels < 1 || green.channels > 4 { - 419●
raise @types.LoadError::DecodeFailed( - 420
"true_color_composite: 通道数必须在1-4之间", - 421
) - 422
} - 423
let expected_size_green = green.width * green.height * green.channels - 424●
if green.data.length() != expected_size_green { - 425●
raise @types.LoadError::DecodeFailed( - 426
"true_color_composite: 数据大小不匹配", - 427
) - 428
} - 429
if blue.width <= 0 || blue.height <= 0 { - 430●
raise @types.LoadError::DecodeFailed( - 431
"true_color_composite: 图像尺寸必须大于0", - 432
) - 433
} - 434
if blue.channels < 1 || blue.channels > 4 { - 435●
raise @types.LoadError::DecodeFailed( - 436
"true_color_composite: 通道数必须在1-4之间", - 437
) - 438
} - 439
let expected_size_blue = blue.width * blue.height * blue.channels - 440●
if blue.data.length() != expected_size_blue { - 441●
raise @types.LoadError::DecodeFailed( - 442
"true_color_composite: 数据大小不匹配", - 443
) - 444
} - 445
if red.width != green.width || - 446
red.height != green.height || - 447
red.width != blue.width || - 448
red.height != blue.height { - 449●
return red - 450
} - 451
let w = red.width - 452
let h = red.height - 453●
let out_data : Array[Byte] = Array::make(w * h * 3, b'\x00') - 454
for y in 0..<h { - 455●
for x in 0..<w { - 456●
let idx = y * w + x - 457
let out_idx = idx * 3 - 458
out_data[out_idx] = red.data[idx] - 459
out_data[out_idx + 1] = green.data[idx] - 460
out_data[out_idx + 2] = blue.data[idx] - 461
} - 462
} - 463
@types.Image::{ - 464
width: w, - 465
height: h, - 466
channels: 3, - 467●
data: Bytes::from_array(out_data), - 468
} - 469
} - 470
- 471
///| - 472
/// 植被覆盖度估算(基于 NDVI 阈值法) - 473
pub fn vegetation_coverage( - 474
ndvi : @types.Image, - 475
ndvi_soil~ : Double = 0.2, - 476
ndvi_veg~ : Double = 0.8, - 477
) -> @types.Image raise @types.LoadError { - 478
// 输入验证 - 479●
if ndvi.width <= 0 || ndvi.height <= 0 { - 480●
raise @types.LoadError::DecodeFailed( - 481
"vegetation_coverage: 图像尺寸必须大于0", - 482
) - 483
} - 484
if ndvi.channels < 1 || ndvi.channels > 4 { - 485●
raise @types.LoadError::DecodeFailed( - 486
"vegetation_coverage: 通道数必须在1-4之间", - 487
) - 488
} - 489
let expected_size = ndvi.width * ndvi.height * ndvi.channels - 490●
if ndvi.data.length() != expected_size { - 491●
raise @types.LoadError::DecodeFailed( - 492
"vegetation_coverage: 数据大小不匹配", - 493
) - 494
} - 495
let soil = ndvi_soil - 496
let veg = ndvi_veg - 497
let w = ndvi.width - 498
let h = ndvi.height - 499●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 500
for y in 0..<h { - 501●
for x in 0..<w { - 502●
let idx = y * w + x - 503
// 将 0-255 映射回 -1 到 1 - 504●
let ndvi_val = ndvi.data[idx].to_double() / 255.0 * 2.0 - 1.0 - 505
let coverage = if ndvi_val <= soil { - 506●
0.0 - 507●
} else if ndvi_val >= veg { - 508●
1.0 - 509
} else { - 510●
(ndvi_val - soil) / (veg - soil) - 511
} - 512●
out_data[idx] = (coverage * 255.0).to_int().min(255).max(0).to_byte() - 513
} - 514
} - 515
@types.Image::{ - 516
width: w, - 517
height: h, - 518
channels: 1, - 519●
data: Bytes::from_array(out_data), - 520
} - 521
} - 522
- 523
///| - 524
/// 土地覆盖分类(简单的基于指数的分类) - 525
/// 返回分类图:0=水体, 1=植被, 2=建筑, 3=裸土, 4=其他 - 526
pub fn land_cover_classification( - 527
ndvi : @types.Image, - 528
ndwi : @types.Image, - 529
ndbi : @types.Image, - 530
) -> @types.Image raise @types.LoadError { - 531
// 输入验证 - 532●
if ndvi.width <= 0 || ndvi.height <= 0 { - 533●
raise @types.LoadError::DecodeFailed( - 534
"land_cover_classification: 图像尺寸必须大于0", - 535
) - 536
} - 537
if ndvi.channels < 1 || ndvi.channels > 4 { - 538●
raise @types.LoadError::DecodeFailed( - 539
"land_cover_classification: 通道数必须在1-4之间", - 540
) - 541
} - 542
let expected_size_ndvi = ndvi.width * ndvi.height * ndvi.channels - 543●
if ndvi.data.length() != expected_size_ndvi { - 544○
raise @types.LoadError::DecodeFailed( - 545
"land_cover_classification: 数据大小不匹配", - 546
) - 547
} - 548
if ndwi.width <= 0 || ndwi.height <= 0 { - 549○
raise @types.LoadError::DecodeFailed( - 550
"land_cover_classification: 图像尺寸必须大于0", - 551
) - 552
} - 553
if ndwi.channels < 1 || ndwi.channels > 4 { - 554○
raise @types.LoadError::DecodeFailed( - 555
"land_cover_classification: 通道数必须在1-4之间", - 556
) - 557
} - 558
let expected_size_ndwi = ndwi.width * ndwi.height * ndwi.channels - 559●
if ndwi.data.length() != expected_size_ndwi { - 560○
raise @types.LoadError::DecodeFailed( - 561
"land_cover_classification: 数据大小不匹配", - 562
) - 563
} - 564
if ndbi.width <= 0 || ndbi.height <= 0 { - 565○
raise @types.LoadError::DecodeFailed( - 566
"land_cover_classification: 图像尺寸必须大于0", - 567
) - 568
} - 569
if ndbi.channels < 1 || ndbi.channels > 4 { - 570○
raise @types.LoadError::DecodeFailed( - 571
"land_cover_classification: 通道数必须在1-4之间", - 572
) - 573
} - 574
let expected_size_ndbi = ndbi.width * ndbi.height * ndbi.channels - 575●
if ndbi.data.length() != expected_size_ndbi { - 576○
raise @types.LoadError::DecodeFailed( - 577
"land_cover_classification: 数据大小不匹配", - 578
) - 579
} - 580
if ndvi.width != ndwi.width || - 581
ndvi.height != ndwi.height || - 582
ndvi.width != ndbi.width || - 583
ndvi.height != ndbi.height { - 584○
return ndvi - 585
} - 586
let w = ndvi.width - 587
let h = ndvi.height - 588●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 589
for y in 0..<h { - 590●
for x in 0..<w { - 591●
let idx = y * w + x - 592
// 将 0-255 映射回 -1 到 1 - 593●
let ndvi_val = ndvi.data[idx].to_double() / 255.0 * 2.0 - 1.0 - 594●
let ndwi_val = ndwi.data[idx].to_double() / 255.0 * 2.0 - 1.0 - 595●
let ndbi_val = ndbi.data[idx].to_double() / 255.0 * 2.0 - 1.0 - 596
// 简单分类规则 - 597
let class_id = if ndwi_val > 0.2 { - 598●
0 // 水体 - 599●
} else if ndvi_val > 0.3 { - 600○
1 // 植被 - 601●
} else if ndbi_val > 0.1 { - 602●
2 // 建筑 - 603●
} else if ndvi_val > 0.1 { - 604○
3 // 裸土 - 605
} else { - 606●
4 // 其他 - 607
} - 608●
out_data[idx] = class_id.to_byte() - 609
} - 610
} - 611
@types.Image::{ - 612
width: w, - 613
height: h, - 614
channels: 1, - 615●
data: Bytes::from_array(out_data), - 616
} - 617
} - 618
- 619
///| - 620
/// 分类图彩色可视化 - 621
pub fn classification_colormap( - 622
classification : @types.Image, - 623
) -> @types.Image raise @types.LoadError { - 624
// 输入验证 - 625●
if classification.width <= 0 || classification.height <= 0 { - 626●
raise @types.LoadError::DecodeFailed( - 627
"classification_colormap: 图像尺寸必须大于0", - 628
) - 629
} - 630
if classification.channels < 1 || classification.channels > 4 { - 631●
raise @types.LoadError::DecodeFailed( - 632
"classification_colormap: 通道数必须在1-4之间", - 633
) - 634
} - 635
let expected_size = classification.width * - 636
classification.height * - 637
classification.channels - 638●
if classification.data.length() != expected_size { - 639○
raise @types.LoadError::DecodeFailed( - 640
"classification_colormap: 数据大小不匹配", - 641
) - 642
} - 643
let w = classification.width - 644
let h = classification.height - 645●
let out_data : Array[Byte] = Array::make(w * h * 3, b'\x00') - 646
// 分类颜色:0=水体(蓝), 1=植被(绿), 2=建筑(红), 3=裸土(黄), 4=其他(灰) - 647
let colors = [ - 648
(0, 0, 255), // 水体 - 蓝 - 649
(0, 255, 0), // 植被 - 绿 - 650
(255, 0, 0), // 建筑 - 红 - 651
(255, 255, 0), // 裸土 - 黄 - 652
(128, 128, 128), // 其他 - 灰 - 653
] - 654
for y in 0..<h { - 655●
for x in 0..<w { - 656●
let idx = y * w + x - 657●
let class_id = classification.data[idx].to_int() - 658
let out_idx = idx * 3 - 659●
let color_idx = class_id.min(colors.length() - 1).max(0) - 660
let (r, g, b) = colors[color_idx] - 661●
out_data[out_idx] = r.to_byte() - 662●
out_data[out_idx + 1] = g.to_byte() - 663●
out_data[out_idx + 2] = b.to_byte() - 664
} - 665
} - 666
@types.Image::{ - 667
width: w, - 668
height: h, - 669
channels: 3, - 670●
data: Bytes::from_array(out_data), - 671
} - 672
}