image.domain\remote_sensing\remote_sensing.mbt

94.6% · 191 / 202 points · 672 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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