image.process\segment\morphology_16f.mbt
96.3% · 262 / 272 points · 765 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 形态学 16f morphology 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 形态学操作 16-bit/float 变体 - 7
// 支持 Image16 / ImageF 两种类型 - 8
// StructuringElement 复用 morphology.mbt 中的定义 - 9
- 10
// 复用 color_16f.mbt 中的 read_u16_le_m / write_u16_le_m / write_f32_le_m - 11
// 复用 tonemap.mbt 中的 read_float_le_m - 12
- 13
// ========== 基础操作:erode / dilate ========== - 14
- 15
///| - 16
/// 16-bit 腐蚀。每个像素取 3x3 邻域最小值。 - 17
/// @see erode 8-bit 版本,本函数为 Image16 变体 - 18
pub fn erode_16(img : @types.Image16) -> @types.Image16 { - 19●
let ch = img.channels - 20
let w = img.width - 21
let h = img.height - 22
let n = w * h - 23
let bpp = ch * 2 - 24●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 25●
let src : Array[Int32] = Array::make(n * ch, 0) - 26
for i in 0..<(n * ch) { - 27●
src[i] = read_u16_le_m(img.data, i * 2) - 28
} - 29
// 内部区域 - 30
for y in 1..<(h - 1) { - 31●
for x in 1..<(w - 1) { - 32●
for c in 0..<ch { - 33●
let base = ((y - 1) * w + (x - 1)) * ch + c - 34
let mut min_v = src[base] - 35
if src[base + ch] < min_v { - 36●
min_v = src[base + ch] - 37
} - 38
if src[base + 2 * ch] < min_v { - 39●
min_v = src[base + 2 * ch] - 40
} - 41
let row1 = base + w * ch - 42
if src[row1] < min_v { - 43●
min_v = src[row1] - 44
} - 45
if src[row1 + ch] < min_v { - 46●
min_v = src[row1 + ch] - 47
} - 48
if src[row1 + 2 * ch] < min_v { - 49●
min_v = src[row1 + 2 * ch] - 50
} - 51
let row2 = base + 2 * w * ch - 52
if src[row2] < min_v { - 53●
min_v = src[row2] - 54
} - 55
if src[row2 + ch] < min_v { - 56●
min_v = src[row2 + ch] - 57
} - 58
if src[row2 + 2 * ch] < min_v { - 59●
min_v = src[row2 + 2 * ch] - 60
} - 61●
write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v) - 62
} - 63
} - 64
} - 65
// 边界区域 - 66
for y in 0..<h { - 67●
let y_inner = y >= 1 && y < h - 1 - 68
for x in 0..<w { - 69●
if y_inner && x >= 1 && x < w - 1 { - 70●
continue - 71
} - 72
for c in 0..<ch { - 73●
let mut min_v = 65535 - 74
for dy in -1..<=1 { - 75●
for dx in -1..<=1 { - 76●
let sx = @types.clamp_int(x + dx, 0, w - 1) - 77●
let sy = @types.clamp_int(y + dy, 0, h - 1) - 78
let v = src[(sy * w + sx) * ch + c] - 79
if v < min_v { - 80●
min_v = v - 81
} - 82
} - 83
} - 84●
write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v) - 85
} - 86
} - 87
} - 88
@types.Image16::{ - 89
width: w, - 90
height: h, - 91
channels: ch, - 92●
data: Bytes::from_array(out), - 93
} - 94
} - 95
- 96
///| - 97
/// float 腐蚀。每个像素取 3x3 邻域最小值。 - 98
/// @see erode 8-bit 版本,本函数为 ImageF 变体 - 99
pub fn erode_f(img : @types.ImageF) -> @types.ImageF { - 100●
let ch = img.channels - 101
let w = img.width - 102
let h = img.height - 103
let n = w * h - 104
let bpp = ch * 4 - 105●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 106●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 107
for i in 0..<(n * ch) { - 108●
src[i] = read_float_le_m(img.data, i * 4) - 109
} - 110
for y in 1..<(h - 1) { - 111●
for x in 1..<(w - 1) { - 112●
for c in 0..<ch { - 113●
let base = ((y - 1) * w + (x - 1)) * ch + c - 114
let mut min_v = src[base] - 115
if src[base + ch] < min_v { - 116●
min_v = src[base + ch] - 117
} - 118
if src[base + 2 * ch] < min_v { - 119●
min_v = src[base + 2 * ch] - 120
} - 121
let row1 = base + w * ch - 122
if src[row1] < min_v { - 123●
min_v = src[row1] - 124
} - 125
if src[row1 + ch] < min_v { - 126●
min_v = src[row1 + ch] - 127
} - 128
if src[row1 + 2 * ch] < min_v { - 129●
min_v = src[row1 + 2 * ch] - 130
} - 131
let row2 = base + 2 * w * ch - 132
if src[row2] < min_v { - 133●
min_v = src[row2] - 134
} - 135
if src[row2 + ch] < min_v { - 136●
min_v = src[row2 + ch] - 137
} - 138
if src[row2 + 2 * ch] < min_v { - 139●
min_v = src[row2 + 2 * ch] - 140
} - 141●
write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v) - 142
} - 143
} - 144
} - 145
for y in 0..<h { - 146●
let y_inner = y >= 1 && y < h - 1 - 147
for x in 0..<w { - 148●
if y_inner && x >= 1 && x < w - 1 { - 149●
continue - 150
} - 151
for c in 0..<ch { - 152●
let mut min_v = 1.0F - 153
for dy in -1..<=1 { - 154●
for dx in -1..<=1 { - 155●
let sx = @types.clamp_int(x + dx, 0, w - 1) - 156●
let sy = @types.clamp_int(y + dy, 0, h - 1) - 157
let v = src[(sy * w + sx) * ch + c] - 158
if v < min_v { - 159●
min_v = v - 160
} - 161
} - 162
} - 163●
write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v) - 164
} - 165
} - 166
} - 167
@types.ImageF::{ - 168
width: w, - 169
height: h, - 170
channels: ch, - 171●
data: Bytes::from_array(out), - 172
} - 173
} - 174
- 175
///| - 176
/// 16-bit 膨胀。每个像素取 3x3 邻域最大值。 - 177
/// @see dilate 8-bit 版本,本函数为 Image16 变体 - 178
pub fn dilate_16(img : @types.Image16) -> @types.Image16 { - 179●
let ch = img.channels - 180
let w = img.width - 181
let h = img.height - 182
let n = w * h - 183
let bpp = ch * 2 - 184●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 185●
let src : Array[Int32] = Array::make(n * ch, 0) - 186
for i in 0..<(n * ch) { - 187●
src[i] = read_u16_le_m(img.data, i * 2) - 188
} - 189
for y in 1..<(h - 1) { - 190●
for x in 1..<(w - 1) { - 191●
for c in 0..<ch { - 192●
let base = ((y - 1) * w + (x - 1)) * ch + c - 193
let mut max_v = src[base] - 194
if src[base + ch] > max_v { - 195●
max_v = src[base + ch] - 196
} - 197
if src[base + 2 * ch] > max_v { - 198●
max_v = src[base + 2 * ch] - 199
} - 200
let row1 = base + w * ch - 201
if src[row1] > max_v { - 202●
max_v = src[row1] - 203
} - 204
if src[row1 + ch] > max_v { - 205●
max_v = src[row1 + ch] - 206
} - 207
if src[row1 + 2 * ch] > max_v { - 208●
max_v = src[row1 + 2 * ch] - 209
} - 210
let row2 = base + 2 * w * ch - 211
if src[row2] > max_v { - 212●
max_v = src[row2] - 213
} - 214
if src[row2 + ch] > max_v { - 215●
max_v = src[row2 + ch] - 216
} - 217
if src[row2 + 2 * ch] > max_v { - 218●
max_v = src[row2 + 2 * ch] - 219
} - 220●
write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v) - 221
} - 222
} - 223
} - 224
for y in 0..<h { - 225●
let y_inner = y >= 1 && y < h - 1 - 226
for x in 0..<w { - 227●
if y_inner && x >= 1 && x < w - 1 { - 228●
continue - 229
} - 230
for c in 0..<ch { - 231●
let mut max_v = 0 - 232
for dy in -1..<=1 { - 233●
for dx in -1..<=1 { - 234●
let sx = @types.clamp_int(x + dx, 0, w - 1) - 235●
let sy = @types.clamp_int(y + dy, 0, h - 1) - 236
let v = src[(sy * w + sx) * ch + c] - 237
if v > max_v { - 238●
max_v = v - 239
} - 240
} - 241
} - 242●
write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v) - 243
} - 244
} - 245
} - 246
@types.Image16::{ - 247
width: w, - 248
height: h, - 249
channels: ch, - 250●
data: Bytes::from_array(out), - 251
} - 252
} - 253
- 254
///| - 255
/// float 膨胀。每个像素取 3x3 邻域最大值。 - 256
/// @see dilate 8-bit 版本,本函数为 ImageF 变体 - 257
pub fn dilate_f(img : @types.ImageF) -> @types.ImageF { - 258●
let ch = img.channels - 259
let w = img.width - 260
let h = img.height - 261
let n = w * h - 262
let bpp = ch * 4 - 263●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 264●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 265
for i in 0..<(n * ch) { - 266●
src[i] = read_float_le_m(img.data, i * 4) - 267
} - 268
for y in 1..<(h - 1) { - 269●
for x in 1..<(w - 1) { - 270●
for c in 0..<ch { - 271●
let base = ((y - 1) * w + (x - 1)) * ch + c - 272
let mut max_v = src[base] - 273
if src[base + ch] > max_v { - 274●
max_v = src[base + ch] - 275
} - 276
if src[base + 2 * ch] > max_v { - 277●
max_v = src[base + 2 * ch] - 278
} - 279
let row1 = base + w * ch - 280
if src[row1] > max_v { - 281●
max_v = src[row1] - 282
} - 283
if src[row1 + ch] > max_v { - 284●
max_v = src[row1 + ch] - 285
} - 286
if src[row1 + 2 * ch] > max_v { - 287●
max_v = src[row1 + 2 * ch] - 288
} - 289
let row2 = base + 2 * w * ch - 290
if src[row2] > max_v { - 291●
max_v = src[row2] - 292
} - 293
if src[row2 + ch] > max_v { - 294●
max_v = src[row2 + ch] - 295
} - 296
if src[row2 + 2 * ch] > max_v { - 297●
max_v = src[row2 + 2 * ch] - 298
} - 299●
write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v) - 300
} - 301
} - 302
} - 303
for y in 0..<h { - 304●
let y_inner = y >= 1 && y < h - 1 - 305
for x in 0..<w { - 306●
if y_inner && x >= 1 && x < w - 1 { - 307●
continue - 308
} - 309
for c in 0..<ch { - 310●
let mut max_v = 0.0F - 311
for dy in -1..<=1 { - 312●
for dx in -1..<=1 { - 313●
let sx = @types.clamp_int(x + dx, 0, w - 1) - 314●
let sy = @types.clamp_int(y + dy, 0, h - 1) - 315
let v = src[(sy * w + sx) * ch + c] - 316
if v > max_v { - 317●
max_v = v - 318
} - 319
} - 320
} - 321●
write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v) - 322
} - 323
} - 324
} - 325
@types.ImageF::{ - 326
width: w, - 327
height: h, - 328
channels: ch, - 329●
data: Bytes::from_array(out), - 330
} - 331
} - 332
- 333
// ========== 组合操作 ========== - 334
- 335
///| - 336
/// 16-bit 开运算(先腐蚀后膨胀)。 - 337
/// @see morph_open 8-bit 版本,本函数为 Image16 变体 - 338
pub fn morph_open_16(img : @types.Image16) -> @types.Image16 { - 339●
dilate_16(erode_16(img)) - 340
} - 341
- 342
///| - 343
/// float 开运算(先腐蚀后膨胀)。 - 344
/// @see morph_open 8-bit 版本,本函数为 ImageF 变体 - 345
pub fn morph_open_f(img : @types.ImageF) -> @types.ImageF { - 346●
dilate_f(erode_f(img)) - 347
} - 348
- 349
///| - 350
/// 16-bit 闭运算(先膨胀后腐蚀)。 - 351
/// @see morph_close 8-bit 版本,本函数为 Image16 变体 - 352
pub fn morph_close_16(img : @types.Image16) -> @types.Image16 { - 353●
erode_16(dilate_16(img)) - 354
} - 355
- 356
///| - 357
/// float 闭运算(先膨胀后腐蚀)。 - 358
/// @see morph_close 8-bit 版本,本函数为 ImageF 变体 - 359
pub fn morph_close_f(img : @types.ImageF) -> @types.ImageF { - 360●
erode_f(dilate_f(img)) - 361
} - 362
- 363
///| - 364
/// 16-bit 形态学梯度(dilate - erode)。 - 365
/// @see morph_gradient 8-bit 版本,本函数为 Image16 变体 - 366
pub fn morph_gradient_16(img : @types.Image16) -> @types.Image16 { - 367●
let dilated = dilate_16(img) - 368●
let eroded = erode_16(img) - 369
let ch = img.channels - 370
let w = img.width - 371
let h = img.height - 372
let n = w * h - 373
let bpp = ch * 2 - 374●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 375
for i in 0..<(n * ch) { - 376●
let v = read_u16_le_m(dilated.data, i * 2) - - 377●
read_u16_le_m(eroded.data, i * 2) - 378◐
write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v }) - 379
} - 380
@types.Image16::{ - 381
width: w, - 382
height: h, - 383
channels: ch, - 384●
data: Bytes::from_array(out), - 385
} - 386
} - 387
- 388
///| - 389
/// float 形态学梯度(dilate - erode)。 - 390
/// @see morph_gradient 8-bit 版本,本函数为 ImageF 变体 - 391
pub fn morph_gradient_f(img : @types.ImageF) -> @types.ImageF { - 392●
let dilated = dilate_f(img) - 393●
let eroded = erode_f(img) - 394
let ch = img.channels - 395
let w = img.width - 396
let h = img.height - 397
let n = w * h - 398
let bpp = ch * 4 - 399●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 400
for i in 0..<(n * ch) { - 401●
let v = read_float_le_m(dilated.data, i * 4) - - 402●
read_float_le_m(eroded.data, i * 4) - 403◐
write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v }) - 404
} - 405
@types.ImageF::{ - 406
width: w, - 407
height: h, - 408
channels: ch, - 409●
data: Bytes::from_array(out), - 410
} - 411
} - 412
- 413
///| - 414
/// 16-bit 顶帽变换(original - open)。 - 415
/// @see morph_tophat 8-bit 版本,本函数为 Image16 变体 - 416
pub fn morph_tophat_16(img : @types.Image16) -> @types.Image16 { - 417●
let opened = morph_open_16(img) - 418
let ch = img.channels - 419
let w = img.width - 420
let h = img.height - 421
let n = w * h - 422
let bpp = ch * 2 - 423●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 424
for i in 0..<(n * ch) { - 425●
let v = read_u16_le_m(img.data, i * 2) - read_u16_le_m(opened.data, i * 2) - 426◐
write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v }) - 427
} - 428
@types.Image16::{ - 429
width: w, - 430
height: h, - 431
channels: ch, - 432●
data: Bytes::from_array(out), - 433
} - 434
} - 435
- 436
///| - 437
/// float 顶帽变换(original - open)。 - 438
/// @see morph_tophat 8-bit 版本,本函数为 ImageF 变体 - 439
pub fn morph_tophat_f(img : @types.ImageF) -> @types.ImageF { - 440●
let opened = morph_open_f(img) - 441
let ch = img.channels - 442
let w = img.width - 443
let h = img.height - 444
let n = w * h - 445
let bpp = ch * 4 - 446●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 447
for i in 0..<(n * ch) { - 448●
let v = read_float_le_m(img.data, i * 4) - - 449●
read_float_le_m(opened.data, i * 4) - 450◐
write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v }) - 451
} - 452
@types.ImageF::{ - 453
width: w, - 454
height: h, - 455
channels: ch, - 456●
data: Bytes::from_array(out), - 457
} - 458
} - 459
- 460
///| - 461
/// 16-bit 黑帽变换(close - original)。 - 462
/// @see morph_blackhat 8-bit 版本,本函数为 Image16 变体 - 463
pub fn morph_blackhat_16(img : @types.Image16) -> @types.Image16 { - 464●
let closed = morph_close_16(img) - 465
let ch = img.channels - 466
let w = img.width - 467
let h = img.height - 468
let n = w * h - 469
let bpp = ch * 2 - 470●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 471
for i in 0..<(n * ch) { - 472●
let v = read_u16_le_m(closed.data, i * 2) - read_u16_le_m(img.data, i * 2) - 473◐
write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v }) - 474
} - 475
@types.Image16::{ - 476
width: w, - 477
height: h, - 478
channels: ch, - 479●
data: Bytes::from_array(out), - 480
} - 481
} - 482
- 483
///| - 484
/// float 黑帽变换(close - original)。 - 485
/// @see morph_blackhat 8-bit 版本,本函数为 ImageF 变体 - 486
pub fn morph_blackhat_f(img : @types.ImageF) -> @types.ImageF { - 487●
let closed = morph_close_f(img) - 488
let ch = img.channels - 489
let w = img.width - 490
let h = img.height - 491
let n = w * h - 492
let bpp = ch * 4 - 493●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 494
for i in 0..<(n * ch) { - 495●
let v = read_float_le_m(closed.data, i * 4) - - 496●
read_float_le_m(img.data, i * 4) - 497◐
write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v }) - 498
} - 499
@types.ImageF::{ - 500
width: w, - 501
height: h, - 502
channels: ch, - 503●
data: Bytes::from_array(out), - 504
} - 505
} - 506
- 507
// ========== 自定义结构元素 ========== - 508
- 509
///| - 510
/// 16-bit 自定义结构元素腐蚀。 - 511
/// @see erode_custom 8-bit 版本,本函数为 Image16 变体 - 512
pub fn erode_custom_16( - 513
img : @types.Image16, - 514
se : StructuringElement, - 515
) -> @types.Image16 { - 516●
let ch = img.channels - 517
let w = img.width - 518
let h = img.height - 519
let n = w * h - 520
let bpp = ch * 2 - 521●
let src : Array[Int32] = Array::make(n * ch, 0) - 522
for i in 0..<(n * ch) { - 523●
src[i] = read_u16_le_m(img.data, i * 2) - 524
} - 525●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 526
let cx = se.width / 2 - 527
let cy = se.height / 2 - 528
for y in 0..<h { - 529●
for x in 0..<w { - 530●
for c in 0..<ch { - 531●
let mut min_v = 65535 - 532
for sy in 0..<se.height { - 533●
for sx in 0..<se.width { - 534●
if se.data[sy * se.width + sx] { - 535●
let nx = @types.clamp_int(x + sx - cx, 0, w - 1) - 536●
let ny = @types.clamp_int(y + sy - cy, 0, h - 1) - 537
let v = src[(ny * w + nx) * ch + c] - 538
if v < min_v { - 539●
min_v = v - 540
} - 541
} - 542
} - 543
} - 544●
write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v) - 545
} - 546
} - 547
} - 548
@types.Image16::{ - 549
width: w, - 550
height: h, - 551
channels: ch, - 552●
data: Bytes::from_array(out), - 553
} - 554
} - 555
- 556
///| - 557
/// float 自定义结构元素腐蚀。 - 558
/// @see erode_custom 8-bit 版本,本函数为 ImageF 变体 - 559
pub fn erode_custom_f( - 560
img : @types.ImageF, - 561
se : StructuringElement, - 562
) -> @types.ImageF { - 563●
let ch = img.channels - 564
let w = img.width - 565
let h = img.height - 566
let n = w * h - 567
let bpp = ch * 4 - 568●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 569
for i in 0..<(n * ch) { - 570●
src[i] = read_float_le_m(img.data, i * 4) - 571
} - 572●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 573
let cx = se.width / 2 - 574
let cy = se.height / 2 - 575
for y in 0..<h { - 576●
for x in 0..<w { - 577●
for c in 0..<ch { - 578●
let mut min_v = 1.0F - 579
for sy in 0..<se.height { - 580●
for sx in 0..<se.width { - 581●
if se.data[sy * se.width + sx] { - 582●
let nx = @types.clamp_int(x + sx - cx, 0, w - 1) - 583●
let ny = @types.clamp_int(y + sy - cy, 0, h - 1) - 584
let v = src[(ny * w + nx) * ch + c] - 585
if v < min_v { - 586●
min_v = v - 587
} - 588
} - 589
} - 590
} - 591●
write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v) - 592
} - 593
} - 594
} - 595
@types.ImageF::{ - 596
width: w, - 597
height: h, - 598
channels: ch, - 599●
data: Bytes::from_array(out), - 600
} - 601
} - 602
- 603
///| - 604
/// 16-bit 自定义结构元素膨胀。 - 605
/// @see dilate_custom 8-bit 版本,本函数为 Image16 变体 - 606
pub fn dilate_custom_16( - 607
img : @types.Image16, - 608
se : StructuringElement, - 609
) -> @types.Image16 { - 610●
let ch = img.channels - 611
let w = img.width - 612
let h = img.height - 613
let n = w * h - 614
let bpp = ch * 2 - 615●
let src : Array[Int32] = Array::make(n * ch, 0) - 616
for i in 0..<(n * ch) { - 617●
src[i] = read_u16_le_m(img.data, i * 2) - 618
} - 619●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 620
let cx = se.width / 2 - 621
let cy = se.height / 2 - 622
for y in 0..<h { - 623●
for x in 0..<w { - 624●
for c in 0..<ch { - 625●
let mut max_v = 0 - 626
for sy in 0..<se.height { - 627●
for sx in 0..<se.width { - 628●
if se.data[sy * se.width + sx] { - 629●
let nx = @types.clamp_int(x + sx - cx, 0, w - 1) - 630●
let ny = @types.clamp_int(y + sy - cy, 0, h - 1) - 631
let v = src[(ny * w + nx) * ch + c] - 632
if v > max_v { - 633●
max_v = v - 634
} - 635
} - 636
} - 637
} - 638●
write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v) - 639
} - 640
} - 641
} - 642
@types.Image16::{ - 643
width: w, - 644
height: h, - 645
channels: ch, - 646●
data: Bytes::from_array(out), - 647
} - 648
} - 649
- 650
///| - 651
/// float 自定义结构元素膨胀。 - 652
/// @see dilate_custom 8-bit 版本,本函数为 ImageF 变体 - 653
pub fn dilate_custom_f( - 654
img : @types.ImageF, - 655
se : StructuringElement, - 656
) -> @types.ImageF { - 657●
let ch = img.channels - 658
let w = img.width - 659
let h = img.height - 660
let n = w * h - 661
let bpp = ch * 4 - 662●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 663
for i in 0..<(n * ch) { - 664●
src[i] = read_float_le_m(img.data, i * 4) - 665
} - 666●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 667
let cx = se.width / 2 - 668
let cy = se.height / 2 - 669
for y in 0..<h { - 670●
for x in 0..<w { - 671●
for c in 0..<ch { - 672●
let mut max_v = 0.0F - 673
for sy in 0..<se.height { - 674●
for sx in 0..<se.width { - 675●
if se.data[sy * se.width + sx] { - 676●
let nx = @types.clamp_int(x + sx - cx, 0, w - 1) - 677●
let ny = @types.clamp_int(y + sy - cy, 0, h - 1) - 678
let v = src[(ny * w + nx) * ch + c] - 679
if v > max_v { - 680●
max_v = v - 681
} - 682
} - 683
} - 684
} - 685●
write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v) - 686
} - 687
} - 688
} - 689
@types.ImageF::{ - 690
width: w, - 691
height: h, - 692
channels: ch, - 693●
data: Bytes::from_array(out), - 694
} - 695
} - 696
- 697
///| - 698
/// 16-bit 自定义结构元素开运算。 - 699
/// @see morph_open_custom 8-bit 版本,本函数为 Image16 变体 - 700
pub fn morph_open_custom_16( - 701
img : @types.Image16, - 702
se : StructuringElement, - 703
) -> @types.Image16 { - 704●
dilate_custom_16(erode_custom_16(img, se), se) - 705
} - 706
- 707
///| - 708
/// float 自定义结构元素开运算。 - 709
/// @see morph_open_custom 8-bit 版本,本函数为 ImageF 变体 - 710
pub fn morph_open_custom_f( - 711
img : @types.ImageF, - 712
se : StructuringElement, - 713
) -> @types.ImageF { - 714○
dilate_custom_f(erode_custom_f(img, se), se) - 715
} - 716
- 717
///| - 718
/// 16-bit 自定义结构元素闭运算。 - 719
/// @see morph_close_custom 8-bit 版本,本函数为 Image16 变体 - 720
pub fn morph_close_custom_16( - 721
img : @types.Image16, - 722
se : StructuringElement, - 723
) -> @types.Image16 { - 724○
erode_custom_16(dilate_custom_16(img, se), se) - 725
} - 726
- 727
///| - 728
/// float 自定义结构元素闭运算。 - 729
/// @see morph_close_custom 8-bit 版本,本函数为 ImageF 变体 - 730
pub fn morph_close_custom_f( - 731
img : @types.ImageF, - 732
se : StructuringElement, - 733
) -> @types.ImageF { - 734●
erode_custom_f(dilate_custom_f(img, se), se) - 735
} - 736
- 737
///| - 738
fn read_u16_le_m(data : Bytes, offset : Int32) -> Int32 { - 739●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 740
} - 741
- 742
///| - 743
fn write_u16_le_m(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 744●
buf[offset] = (val & 0xFF).to_byte() - 745●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 746
} - 747
- 748
///| - 749
fn read_float_le_m(data : Bytes, offset : Int32) -> Float { - 750●
let b0 = data[offset].to_int() - 751●
let b1 = data[offset + 1].to_int() - 752●
let b2 = data[offset + 2].to_int() - 753●
let b3 = data[offset + 3].to_int() - 754
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 755●
Float::reinterpret_from_int(bits) - 756
} - 757
- 758
///| - 759
fn write_f32_le_m(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 760●
let bits = val.reinterpret_as_int() - 761●
buf[offset] = (bits & 0xFF).to_byte() - 762●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 763●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 764●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 765
}