image.process\color\adaptive_threshold_16f.mbt
94.1% · 191 / 203 points · 605 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 adaptive threshold 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 阈值处理 16-bit/float 变体 - 7
// 支持 Image16 / ImageF 两种类型 - 8
// 输出均为二值图像(16-bit: 0/65535,float: 0.0/1.0) - 9
- 10
// 复用 color_16f.mbt 中的 read_u16_le_c / write_u16_le_c / write_f32_le - 11
// 复用 tonemap.mbt 中的 read_float_le - 12
- 13
///| - 14
/// 16-bit 自适应阈值(均值法)。block_size 必须为 >=3 的奇数。 - 15
/// 输出二值图像(0 或 65535),alpha 通道不变。 - 16
/// @see adaptive_threshold_mean 8-bit 版本,本函数为 Image16 变体 - 17
pub fn adaptive_threshold_mean_16( - 18
img : @types.Image16, - 19
block_size : Int32, - 20
c : Int32, - 21
) -> @types.Image16 raise @types.LoadError { - 22
// 输入验证 - 23●
if img.width <= 0 || img.height <= 0 { - 24●
raise @types.LoadError::DecodeFailed( - 25
"adaptive_threshold_mean_16: 图像尺寸必须大于0", - 26
) - 27
} - 28
if block_size < 3 || block_size % 2 == 0 { - 29●
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数") - 30
} - 31
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 32●
raise @types.LoadError::DecodeFailed( - 33
"adaptive_threshold_mean_16 仅支持 channels=1/3/4", - 34
) - 35
} - 36
let expected_size = img.width * img.height * img.channels * 2 - 37●
if img.data.length() != expected_size { - 38●
raise @types.LoadError::DecodeFailed( - 39
"adaptive_threshold_mean_16: 数据大小不匹配,期望 " + - 40●
expected_size.to_string() + - 41
",实际 " + - 42●
img.data.length().to_string(), - 43
) - 44
} - 45
let ch = img.channels - 46●
let color_ch = if ch >= 3 { 3 } else { 1 } - 47
let w = img.width - 48
let h = img.height - 49
let n = w * h - 50
let bpp = ch * 2 - 51●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 52
let half = block_size / 2 - 53●
let src : Array[Int32] = Array::make(n * ch, 0) - 54
for i in 0..<(n * ch) { - 55●
src[i] = read_u16_le_c(img.data, i * 2) - 56
} - 57
let inner_count = block_size * block_size - 58
for c_idx in 0..<color_ch { - 59●
for y in half..<(h - half) { - 60●
for x in half..<(w - half) { - 61●
let mut sum = 0 - 62
for dy in -half..<=half { - 63●
let row = (y + dy) * w - 64
for dx in -half..<=half { - 65●
sum += src[(row + x + dx) * ch + c_idx] - 66
} - 67
} - 68
let mean = sum / inner_count - 69
let threshold = mean - c - 70
let v = src[(y * w + x) * ch + c_idx] - 71
if v > threshold { - 72●
write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535) - 73
} - 74
} - 75
} - 76
for y in 0..<h { - 77●
let y_inner = y >= half && y < h - half - 78
for x in 0..<w { - 79●
if y_inner && x >= half && x < w - half { - 80●
continue - 81
} - 82
let mut sum = 0 - 83
let mut count = 0 - 84
for dy in -half..<=half { - 85●
for dx in -half..<=half { - 86●
let ny = y + dy - 87
let nx = x + dx - 88
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 89●
sum += src[(ny * w + nx) * ch + c_idx] - 90
count += 1 - 91
} - 92
} - 93
} - 94◐
let mean = if count > 0 { sum / count } else { 0 } - 95
let threshold = mean - c - 96
let v = src[(y * w + x) * ch + c_idx] - 97
if v > threshold { - 98●
write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535) - 99
} - 100
} - 101
} - 102
} - 103
if ch == 4 { - 104●
for i in 0..<n { - 105●
let alpha_off = i * bpp + 6 - 106
out[alpha_off] = img.data[alpha_off] - 107
out[alpha_off + 1] = img.data[alpha_off + 1] - 108
} - 109
} - 110
@types.Image16::{ - 111
width: w, - 112
height: h, - 113
channels: ch, - 114●
data: Bytes::from_array(out), - 115
} - 116
} - 117
- 118
///| - 119
/// float 自适应阈值(均值法)。c 为浮点偏移量。 - 120
/// 输出二值图像(0.0 或 1.0),alpha 通道不变。 - 121
/// @see adaptive_threshold_mean 8-bit 版本,本函数为 ImageF 变体 - 122
pub fn adaptive_threshold_mean_f( - 123
img : @types.ImageF, - 124
block_size : Int32, - 125
c : Float, - 126
) -> @types.ImageF raise @types.LoadError { - 127●
if block_size < 3 || block_size % 2 == 0 { - 128●
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数") - 129
} - 130
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"adaptive_threshold_mean_f 仅支持 channels=1/3/4", - 133
) - 134
} - 135
let ch = img.channels - 136●
let color_ch = if ch >= 3 { 3 } else { 1 } - 137
let w = img.width - 138
let h = img.height - 139
let n = w * h - 140
let bpp = ch * 4 - 141●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 142
let half = block_size / 2 - 143●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 144
for i in 0..<(n * ch) { - 145●
src[i] = read_float_le(img.data, i * 4) - 146
} - 147●
let inner_count = Float::from_int(block_size * block_size) - 148
for c_idx in 0..<color_ch { - 149●
for y in half..<(h - half) { - 150●
for x in half..<(w - half) { - 151●
let mut sum = 0.0F - 152
for dy in -half..<=half { - 153●
let row = (y + dy) * w - 154
for dx in -half..<=half { - 155●
sum += src[(row + x + dx) * ch + c_idx] - 156
} - 157
} - 158
let mean = sum / inner_count - 159
let threshold = mean - c - 160
let v = src[(y * w + x) * ch + c_idx] - 161
if v > threshold { - 162●
write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F) - 163
} - 164
} - 165
} - 166
for y in 0..<h { - 167●
let y_inner = y >= half && y < h - half - 168
for x in 0..<w { - 169●
if y_inner && x >= half && x < w - half { - 170●
continue - 171
} - 172
let mut sum = 0.0F - 173
let mut count = 0.0F - 174
for dy in -half..<=half { - 175●
for dx in -half..<=half { - 176●
let ny = y + dy - 177
let nx = x + dx - 178
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 179●
sum += src[(ny * w + nx) * ch + c_idx] - 180
count += 1.0F - 181
} - 182
} - 183
} - 184◐
let mean = if count > 0.0F { sum / count } else { 0.0F } - 185
let threshold = mean - c - 186
let v = src[(y * w + x) * ch + c_idx] - 187
if v > threshold { - 188●
write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F) - 189
} - 190
} - 191
} - 192
} - 193
if ch == 4 { - 194●
for i in 0..<n { - 195●
let alpha_off = i * bpp + 12 - 196
for b in 0..<4 { - 197●
out[alpha_off + b] = img.data[alpha_off + b] - 198
} - 199
} - 200
} - 201
@types.ImageF::{ - 202
width: w, - 203
height: h, - 204
channels: ch, - 205●
data: Bytes::from_array(out), - 206
} - 207
} - 208
- 209
///| - 210
/// 16-bit 自适应阈值(高斯加权法)。 - 211
/// @see adaptive_threshold_gaussian 8-bit 版本,本函数为 Image16 变体 - 212
pub fn adaptive_threshold_gaussian_16( - 213
img : @types.Image16, - 214
block_size : Int32, - 215
c : Int32, - 216
) -> @types.Image16 raise @types.LoadError { - 217
// 输入验证 - 218●
if img.width <= 0 || img.height <= 0 { - 219●
raise @types.LoadError::DecodeFailed( - 220
"adaptive_threshold_gaussian_16: 图像尺寸必须大于0", - 221
) - 222
} - 223
if block_size < 3 || block_size % 2 == 0 { - 224●
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数") - 225
} - 226
let expected_size = img.width * img.height * img.channels * 2 - 227●
if img.data.length() != expected_size { - 228○
raise @types.LoadError::DecodeFailed( - 229
"adaptive_threshold_gaussian_16: 数据大小不匹配,期望 " + - 230○
expected_size.to_string() + - 231
",实际 " + - 232○
img.data.length().to_string(), - 233
) - 234
} - 235
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 236●
raise @types.LoadError::DecodeFailed( - 237
"adaptive_threshold_gaussian_16 仅支持 channels=1/3/4", - 238
) - 239
} - 240
let ch = img.channels - 241●
let color_ch = if ch >= 3 { 3 } else { 1 } - 242
let w = img.width - 243
let h = img.height - 244
let n = w * h - 245
let bpp = ch * 2 - 246●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 247
let half = block_size / 2 - 248●
let sigma = Float::from_int(half) / 2.0F - 249
let two_sigma_sq = 2.0F * sigma * sigma - 250●
let weights : Array[Float] = Array::make(block_size * block_size, 0.0F) - 251
let mut total_weight = 0.0F - 252
for dy in 0..<block_size { - 253●
for dx in 0..<block_size { - 254●
let fy = Float::from_int(dy - half) - 255●
let fx = Float::from_int(dx - half) - 256●
let wt = @math.expf(-(fx * fx + fy * fy) / two_sigma_sq) - 257
weights[dy * block_size + dx] = wt - 258
total_weight += wt - 259
} - 260
} - 261●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 262
for i in 0..<(n * ch) { - 263●
src[i] = Float::from_int(read_u16_le_c(img.data, i * 2)) - 264
} - 265
for c_idx in 0..<color_ch { - 266●
for y in half..<(h - half) { - 267●
for x in half..<(w - half) { - 268●
let mut sum = 0.0F - 269
for dy in 0..<block_size { - 270●
let row = (y + dy - half) * w - 271
let wbase = dy * block_size - 272
for dx in 0..<block_size { - 273●
sum += src[(row + x + dx - half) * ch + c_idx] * weights[wbase + dx] - 274
} - 275
} - 276
let mean = sum / total_weight - 277●
let threshold = mean - Float::from_int(c) - 278
let v = src[(y * w + x) * ch + c_idx] - 279
if v > threshold { - 280●
write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535) - 281
} - 282
} - 283
} - 284
for y in 0..<h { - 285●
let y_inner = y >= half && y < h - half - 286
for x in 0..<w { - 287●
if y_inner && x >= half && x < w - half { - 288●
continue - 289
} - 290
let mut sum = 0.0F - 291
let mut weight_sum = 0.0F - 292
for dy in -half..<=half { - 293●
for dx in -half..<=half { - 294●
let ny = y + dy - 295
let nx = x + dx - 296
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 297●
let weight = weights[(dy + half) * block_size + (dx + half)] - 298
sum += src[(ny * w + nx) * ch + c_idx] * weight - 299
weight_sum += weight - 300
} - 301
} - 302
} - 303◐
let mean = if weight_sum > 0.0F { sum / weight_sum } else { 0.0F } - 304●
let threshold = mean - Float::from_int(c) - 305
let v = src[(y * w + x) * ch + c_idx] - 306
if v > threshold { - 307●
write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535) - 308
} - 309
} - 310
} - 311
} - 312
if ch == 4 { - 313●
for i in 0..<n { - 314●
let alpha_off = i * bpp + 6 - 315
out[alpha_off] = img.data[alpha_off] - 316
out[alpha_off + 1] = img.data[alpha_off + 1] - 317
} - 318
} - 319
@types.Image16::{ - 320
width: w, - 321
height: h, - 322
channels: ch, - 323●
data: Bytes::from_array(out), - 324
} - 325
} - 326
- 327
///| - 328
/// float 自适应阈值(高斯加权法)。 - 329
/// @see adaptive_threshold_gaussian 8-bit 版本,本函数为 ImageF 变体 - 330
pub fn adaptive_threshold_gaussian_f( - 331
img : @types.ImageF, - 332
block_size : Int32, - 333
c : Float, - 334
) -> @types.ImageF raise @types.LoadError { - 335●
if block_size < 3 || block_size % 2 == 0 { - 336●
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数") - 337
} - 338
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 339●
raise @types.LoadError::DecodeFailed( - 340
"adaptive_threshold_gaussian_f 仅支持 channels=1/3/4", - 341
) - 342
} - 343
let ch = img.channels - 344●
let color_ch = if ch >= 3 { 3 } else { 1 } - 345
let w = img.width - 346
let h = img.height - 347
let n = w * h - 348
let bpp = ch * 4 - 349●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 350
let half = block_size / 2 - 351●
let sigma = Float::from_int(half) / 2.0F - 352
let two_sigma_sq = 2.0F * sigma * sigma - 353●
let weights : Array[Float] = Array::make(block_size * block_size, 0.0F) - 354
let mut total_weight = 0.0F - 355
for dy in 0..<block_size { - 356●
for dx in 0..<block_size { - 357●
let fy = Float::from_int(dy - half) - 358●
let fx = Float::from_int(dx - half) - 359●
let wt = @math.expf(-(fx * fx + fy * fy) / two_sigma_sq) - 360
weights[dy * block_size + dx] = wt - 361
total_weight += wt - 362
} - 363
} - 364●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 365
for i in 0..<(n * ch) { - 366●
src[i] = read_float_le(img.data, i * 4) - 367
} - 368
for c_idx in 0..<color_ch { - 369●
for y in half..<(h - half) { - 370●
for x in half..<(w - half) { - 371●
let mut sum = 0.0F - 372
for dy in 0..<block_size { - 373●
let row = (y + dy - half) * w - 374
let wbase = dy * block_size - 375
for dx in 0..<block_size { - 376●
sum += src[(row + x + dx - half) * ch + c_idx] * weights[wbase + dx] - 377
} - 378
} - 379
let mean = sum / total_weight - 380
let threshold = mean - c - 381
let v = src[(y * w + x) * ch + c_idx] - 382
if v > threshold { - 383●
write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F) - 384
} - 385
} - 386
} - 387
for y in 0..<h { - 388●
let y_inner = y >= half && y < h - half - 389
for x in 0..<w { - 390●
if y_inner && x >= half && x < w - half { - 391●
continue - 392
} - 393
let mut sum = 0.0F - 394
let mut weight_sum = 0.0F - 395
for dy in -half..<=half { - 396●
for dx in -half..<=half { - 397●
let ny = y + dy - 398
let nx = x + dx - 399
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 400●
let weight = weights[(dy + half) * block_size + (dx + half)] - 401
sum += src[(ny * w + nx) * ch + c_idx] * weight - 402
weight_sum += weight - 403
} - 404
} - 405
} - 406◐
let mean = if weight_sum > 0.0F { sum / weight_sum } else { 0.0F } - 407
let threshold = mean - c - 408
let v = src[(y * w + x) * ch + c_idx] - 409
if v > threshold { - 410●
write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F) - 411
} - 412
} - 413
} - 414
} - 415
if ch == 4 { - 416●
for i in 0..<n { - 417●
let alpha_off = i * bpp + 12 - 418
for b in 0..<4 { - 419●
out[alpha_off + b] = img.data[alpha_off + b] - 420
} - 421
} - 422
} - 423
@types.ImageF::{ - 424
width: w, - 425
height: h, - 426
channels: ch, - 427●
data: Bytes::from_array(out), - 428
} - 429
} - 430
- 431
///| - 432
/// 16-bit Otsu 大津法自动阈值。使用 65536-bin 直方图。 - 433
/// 输出二值图像(0 或 65535)。 - 434
/// @see threshold_otsu 8-bit 版本,本函数为 Image16 变体 - 435
pub fn threshold_otsu_16( - 436
img : @types.Image16, - 437
) -> @types.Image16 raise @types.LoadError { - 438
// 输入验证 - 439●
if img.width <= 0 || img.height <= 0 { - 440●
raise @types.LoadError::DecodeFailed( - 441
"threshold_otsu_16: 图像尺寸必须大于0", - 442
) - 443
} - 444
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 445○
raise @types.LoadError::DecodeFailed( - 446
"threshold_otsu_16 仅支持 channels=1/3/4", - 447
) - 448
} - 449
let expected_size = img.width * img.height * img.channels * 2 - 450●
if img.data.length() != expected_size { - 451●
raise @types.LoadError::DecodeFailed( - 452
"threshold_otsu_16: 数据大小不匹配,期望 " + - 453●
expected_size.to_string() + - 454
",实际 " + - 455●
img.data.length().to_string(), - 456
) - 457
} - 458
let ch = img.channels - 459●
let color_ch = if ch >= 3 { 3 } else { 1 } - 460
let w = img.width - 461
let h = img.height - 462
let n = w * h - 463
let bpp = ch * 2 - 464●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 465
for c_idx in 0..<color_ch { - 466●
let hist : Array[Int32] = Array::make(65536, 0) - 467
for i in 0..<n { - 468●
hist[read_u16_le_c(img.data, i * bpp + c_idx * 2)] += 1 - 469
} - 470
let mut total = 0 - 471
let mut sum = 0 - 472
for t in 0..<65536 { - 473●
total += hist[t] - 474
sum += t * hist[t] - 475
} - 476
let mut best_t = 0 - 477
let mut best_var = -1.0F - 478
let mut w0 = 0 - 479
let mut sum0 = 0 - 480
for t in 0..<65536 { - 481●
w0 += hist[t] - 482
sum0 += t * hist[t] - 483
if w0 == 0 || w0 == total { - 484●
continue - 485
} - 486
let w1 = total - w0 - 487
let sum1 = sum - sum0 - 488●
let mean0 = Float::from_int(sum0) / Float::from_int(w0) - 489●
let mean1 = Float::from_int(sum1) / Float::from_int(w1) - 490●
let var_between = Float::from_int(w0) * - 491●
Float::from_int(w1) * - 492
(mean0 - mean1) * - 493
(mean0 - mean1) - 494
if var_between > best_var { - 495●
best_var = var_between - 496
best_t = t - 497
} - 498
} - 499
for i in 0..<n { - 500●
let v = read_u16_le_c(img.data, i * bpp + c_idx * 2) - 501
if v > best_t { - 502●
write_u16_le_c(out, i * bpp + c_idx * 2, 65535) - 503
} - 504
} - 505
} - 506
if ch == 4 { - 507●
for i in 0..<n { - 508●
let alpha_off = i * bpp + 6 - 509
out[alpha_off] = img.data[alpha_off] - 510
out[alpha_off + 1] = img.data[alpha_off + 1] - 511
} - 512
} - 513
@types.Image16::{ - 514
width: w, - 515
height: h, - 516
channels: ch, - 517●
data: Bytes::from_array(out), - 518
} - 519
} - 520
- 521
///| - 522
/// float Otsu 大津法自动阈值。将值归一化到 0-255 后使用 256-bin 直方图。 - 523
/// 输出二值图像(0.0 或 1.0)。 - 524
/// @see threshold_otsu 8-bit 版本,本函数为 ImageF 变体 - 525
pub fn threshold_otsu_f( - 526
img : @types.ImageF, - 527
) -> @types.ImageF raise @types.LoadError { - 528●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 529○
raise @types.LoadError::DecodeFailed( - 530
"threshold_otsu_f 仅支持 channels=1/3/4", - 531
) - 532
} - 533
let ch = img.channels - 534●
let color_ch = if ch >= 3 { 3 } else { 1 } - 535
let w = img.width - 536
let h = img.height - 537
let n = w * h - 538
let bpp = ch * 4 - 539●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 540
for c_idx in 0..<color_ch { - 541●
let hist : Array[Int32] = Array::make(256, 0) - 542●
let src : Array[Float] = Array::make(n, 0.0F) - 543
for i in 0..<n { - 544●
let v = read_float_le(img.data, i * bpp + c_idx * 4) - 545
src[i] = v - 546
let bin = if v < 0.0F { - 547○
0 - 548●
} else if v >= 1.0F { - 549○
255 - 550
} else { - 551●
(v * 255.0F + 0.5F).to_int() - 552
} - 553
hist[bin] += 1 - 554
} - 555
let mut total = 0 - 556
let mut sum = 0 - 557
for t in 0..<256 { - 558●
total += hist[t] - 559
sum += t * hist[t] - 560
} - 561
let mut best_t = 0 - 562
let mut best_var = -1.0F - 563
let mut w0 = 0 - 564
let mut sum0 = 0 - 565
for t in 0..<256 { - 566●
w0 += hist[t] - 567
sum0 += t * hist[t] - 568
if w0 == 0 || w0 == total { - 569●
continue - 570
} - 571
let w1 = total - w0 - 572
let sum1 = sum - sum0 - 573●
let mean0 = Float::from_int(sum0) / Float::from_int(w0) - 574●
let mean1 = Float::from_int(sum1) / Float::from_int(w1) - 575●
let var_between = Float::from_int(w0) * - 576●
Float::from_int(w1) * - 577
(mean0 - mean1) * - 578
(mean0 - mean1) - 579
if var_between > best_var { - 580●
best_var = var_between - 581
best_t = t - 582
} - 583
} - 584●
let threshold_f = Float::from_int(best_t) / 255.0F - 585
for i in 0..<n { - 586●
if src[i] > threshold_f { - 587●
write_f32_le(out, i * bpp + c_idx * 4, 1.0F) - 588
} - 589
} - 590
} - 591
if ch == 4 { - 592●
for i in 0..<n { - 593●
let alpha_off = i * bpp + 12 - 594
for b in 0..<4 { - 595●
out[alpha_off + b] = img.data[alpha_off + b] - 596
} - 597
} - 598
} - 599
@types.ImageF::{ - 600
width: w, - 601
height: h, - 602
channels: ch, - 603●
data: Bytes::from_array(out), - 604
} - 605
}