image.process\feature\gabor_template_16f.mbt
88% · 154 / 175 points · 581 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # Gabor 滤波 + 模板匹配 16-bit/float 变体 Gabor filter + template matching 16-bit/float variants - 3
/// - 4
/// 16位/浮点 Gabor/模板匹配 gabor_filter_16 / template_match_16 - 5
/// 16位/浮点 Gabor/模板匹配 16-bit/float Gabor/template matching - 6
// Gabor 滤波 + 模板匹配 16-bit/float 变体 - 7
- 8
// ========== 辅助函数 ========== - 9
- 10
///| - 11
fn read_u16_le_gabor(data : Bytes, offset : Int32) -> Int32 { - 12●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 13
} - 14
- 15
///| - 16
fn write_u16_le_gabor(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 17●
buf[offset] = (val & 0xFF).to_byte() - 18●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 19
} - 20
- 21
///| - 22
fn read_float_le_gabor(data : Bytes, offset : Int32) -> Float { - 23●
let b0 = data[offset].to_int() - 24●
let b1 = data[offset + 1].to_int() - 25●
let b2 = data[offset + 2].to_int() - 26●
let b3 = data[offset + 3].to_int() - 27
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 28●
Float::reinterpret_from_int(bits) - 29
} - 30
- 31
///| - 32
fn write_f32_le_gabor(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 33●
let bits = val.reinterpret_as_int() - 34●
buf[offset] = (bits & 0xFF).to_byte() - 35●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 36●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 37●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 38
} - 39
- 40
///| - 41
fn img16_to_img8_gabor(img : @types.Image16) -> @types.Image { - 42●
let w = img.width - 43
let h = img.height - 44
let ch = img.channels - 45●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 46
for i in 0..<(w * h * ch) { - 47●
let v16 = read_u16_le_gabor(img.data, i * 2) - 48●
data8[i] = (v16 / 257).to_byte() - 49
} - 50
@types.Image::{ - 51
width: w, - 52
height: h, - 53
channels: ch, - 54●
data: Bytes::from_array(data8), - 55
} - 56
} - 57
- 58
///| - 59
fn imgf_to_img8_gabor(img : @types.ImageF) -> @types.Image { - 60●
let w = img.width - 61
let h = img.height - 62
let ch = img.channels - 63●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 64
for i in 0..<(w * h * ch) { - 65●
let vf = read_float_le_gabor(img.data, i * 4) - 66●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 67●
data8[i] = v8.to_byte() - 68
} - 69
@types.Image::{ - 70
width: w, - 71
height: h, - 72
channels: ch, - 73●
data: Bytes::from_array(data8), - 74
} - 75
} - 76
- 77
///| - 78
fn img8_to_img16_gabor(img : @types.Image) -> @types.Image16 { - 79●
let w = img.width - 80
let h = img.height - 81
let ch = img.channels - 82●
let data16 : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 83
for i in 0..<(w * h * ch) { - 84●
let v8 = img.data[i].to_int() - 85●
write_u16_le_gabor(data16, i * 2, v8 * 257) - 86
} - 87
@types.Image16::{ - 88
width: w, - 89
height: h, - 90
channels: ch, - 91●
data: Bytes::from_array(data16), - 92
} - 93
} - 94
- 95
///| - 96
fn img8_to_imgf_gabor(img : @types.Image) -> @types.ImageF { - 97●
let w = img.width - 98
let h = img.height - 99
let ch = img.channels - 100●
let dataf : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 101
for i in 0..<(w * h * ch) { - 102●
let v8 = img.data[i].to_int() - 103●
write_f32_le_gabor(dataf, i * 4, Float::from_int(v8) / 255.0F) - 104
} - 105
@types.ImageF::{ - 106
width: w, - 107
height: h, - 108
channels: ch, - 109●
data: Bytes::from_array(dataf), - 110
} - 111
} - 112
- 113
// ========== Gabor 16-bit ========== - 114
- 115
///| - 116
/// 16-bit Gabor 滤波。 - 117
/// @see gabor_filter 8-bit 版本,本函数为 Image16 变体 - 118
pub fn gabor_filter_16( - 119
img : @types.Image16, - 120
ksize : Int32, - 121
sigma : Float, - 122
theta : Float, - 123
lambda~ : Float = 10.0F, - 124
gamma~ : Float = 0.5F, - 125
phi~ : Float = 0.0F, - 126
) -> @types.Image16 raise @types.LoadError { - 127
// 输入验证 - 128●
if img.width <= 0 || img.height <= 0 { - 129●
raise @types.LoadError::DecodeFailed( - 130
"gabor_filter_16: 图像尺寸必须大于0", - 131
) - 132
} - 133
if img.channels < 1 || img.channels > 4 { - 134●
raise @types.LoadError::DecodeFailed( - 135
"gabor_filter_16: 通道数必须在1-4之间", - 136
) - 137
} - 138
let expected_size = img.width * img.height * img.channels * 2 - 139●
if img.data.length() != expected_size { - 140●
raise @types.LoadError::DecodeFailed( - 141
"gabor_filter_16: 数据大小不匹配,期望 " + - 142●
expected_size.to_string() + - 143
",实际 " + - 144●
img.data.length().to_string(), - 145
) - 146
} - 147
if ksize <= 0 || ksize % 2 == 0 { - 148●
raise @types.LoadError::DecodeFailed( - 149
"gabor_filter_16: ksize必须为正奇数", - 150
) - 151
} - 152
if sigma <= 0.0F { - 153●
raise @types.LoadError::DecodeFailed("gabor_filter_16: sigma必须大于0") - 154
} - 155
- 156●
let img8 = img16_to_img8_gabor(img) - 157●
let result8 = gabor_filter(img8, ksize, sigma, theta, lambda~, gamma~, phi~) - 158●
img8_to_img16_gabor(result8) - 159
} - 160
- 161
// ========== Gabor float ========== - 162
- 163
///| - 164
/// float Gabor 滤波。 - 165
/// @see gabor_filter 8-bit 版本,本函数为 ImageF 变体 - 166
pub fn gabor_filter_f( - 167
img : @types.ImageF, - 168
ksize : Int32, - 169
sigma : Float, - 170
theta : Float, - 171
lambda~ : Float = 10.0F, - 172
gamma~ : Float = 0.5F, - 173
phi~ : Float = 0.0F, - 174
) -> @types.ImageF raise @types.LoadError { - 175
// 输入验证 - 176●
if img.width <= 0 || img.height <= 0 { - 177●
raise @types.LoadError::DecodeFailed( - 178
"gabor_filter_f: 图像尺寸必须大于0", - 179
) - 180
} - 181
if img.channels < 1 || img.channels > 4 { - 182●
raise @types.LoadError::DecodeFailed( - 183
"gabor_filter_f: 通道数必须在1-4之间", - 184
) - 185
} - 186
let expected_size = img.width * img.height * img.channels * 4 - 187●
if img.data.length() != expected_size { - 188●
raise @types.LoadError::DecodeFailed( - 189
"gabor_filter_f: 数据大小不匹配,期望 " + - 190●
expected_size.to_string() + - 191
",实际 " + - 192●
img.data.length().to_string(), - 193
) - 194
} - 195
if ksize <= 0 || ksize % 2 == 0 { - 196●
raise @types.LoadError::DecodeFailed( - 197
"gabor_filter_f: ksize必须为正奇数", - 198
) - 199
} - 200
if sigma <= 0.0F { - 201●
raise @types.LoadError::DecodeFailed("gabor_filter_f: sigma必须大于0") - 202
} - 203
- 204●
let img8 = imgf_to_img8_gabor(img) - 205●
let result8 = gabor_filter(img8, ksize, sigma, theta, lambda~, gamma~, phi~) - 206●
img8_to_imgf_gabor(result8) - 207
} - 208
- 209
// ========== Gabor Bank 16-bit ========== - 210
- 211
///| - 212
/// 16-bit Gabor 滤波器组。 - 213
/// @see gabor_filter_bank 8-bit 版本,本函数为 Image16 变体 - 214
pub fn gabor_filter_bank_16( - 215
img : @types.Image16, - 216
ksize : Int32, - 217
sigma : Float, - 218
num_orientations~ : Int32 = 4, - 219
lambda~ : Float = 10.0F, - 220
gamma~ : Float = 0.5F, - 221
) -> @types.Image16 raise @types.LoadError { - 222
// 输入验证 - 223●
if img.width <= 0 || img.height <= 0 { - 224●
raise @types.LoadError::DecodeFailed( - 225
"gabor_filter_bank_16: 图像尺寸必须大于0", - 226
) - 227
} - 228
if img.channels < 1 || img.channels > 4 { - 229●
raise @types.LoadError::DecodeFailed( - 230
"gabor_filter_bank_16: 通道数必须在1-4之间", - 231
) - 232
} - 233
let expected_size = img.width * img.height * img.channels * 2 - 234●
if img.data.length() != expected_size { - 235●
raise @types.LoadError::DecodeFailed( - 236
"gabor_filter_bank_16: 数据大小不匹配,期望 " + - 237●
expected_size.to_string() + - 238
",实际 " + - 239●
img.data.length().to_string(), - 240
) - 241
} - 242
if ksize <= 0 || ksize % 2 == 0 { - 243●
raise @types.LoadError::DecodeFailed( - 244
"gabor_filter_bank_16: ksize必须为正奇数", - 245
) - 246
} - 247
if sigma <= 0.0F { - 248○
raise @types.LoadError::DecodeFailed( - 249
"gabor_filter_bank_16: sigma必须大于0", - 250
) - 251
} - 252
if num_orientations <= 0 { - 253●
raise @types.LoadError::DecodeFailed( - 254
"gabor_filter_bank_16: num_orientations必须大于0", - 255
) - 256
} - 257
- 258●
let img8 = img16_to_img8_gabor(img) - 259●
let result8 = gabor_filter_bank( - 260
img8, - 261
ksize, - 262
sigma, - 263
num_orientations~, - 264
lambda~, - 265
gamma~, - 266
) - 267●
img8_to_img16_gabor(result8) - 268
} - 269
- 270
// ========== Gabor Bank float ========== - 271
- 272
///| - 273
/// float Gabor 滤波器组。 - 274
/// @see gabor_filter_bank 8-bit 版本,本函数为 ImageF 变体 - 275
pub fn gabor_filter_bank_f( - 276
img : @types.ImageF, - 277
ksize : Int32, - 278
sigma : Float, - 279
num_orientations~ : Int32 = 4, - 280
lambda~ : Float = 10.0F, - 281
gamma~ : Float = 0.5F, - 282
) -> @types.ImageF raise @types.LoadError { - 283
// 输入验证 - 284●
if img.width <= 0 || img.height <= 0 { - 285●
raise @types.LoadError::DecodeFailed( - 286
"gabor_filter_bank_f: 图像尺寸必须大于0", - 287
) - 288
} - 289
if img.channels < 1 || img.channels > 4 { - 290●
raise @types.LoadError::DecodeFailed( - 291
"gabor_filter_bank_f: 通道数必须在1-4之间", - 292
) - 293
} - 294
let expected_size = img.width * img.height * img.channels * 4 - 295●
if img.data.length() != expected_size { - 296●
raise @types.LoadError::DecodeFailed( - 297
"gabor_filter_bank_f: 数据大小不匹配,期望 " + - 298●
expected_size.to_string() + - 299
",实际 " + - 300●
img.data.length().to_string(), - 301
) - 302
} - 303
if ksize <= 0 || ksize % 2 == 0 { - 304●
raise @types.LoadError::DecodeFailed( - 305
"gabor_filter_bank_f: ksize必须为正奇数", - 306
) - 307
} - 308
if sigma <= 0.0F { - 309○
raise @types.LoadError::DecodeFailed( - 310
"gabor_filter_bank_f: sigma必须大于0", - 311
) - 312
} - 313
if num_orientations <= 0 { - 314●
raise @types.LoadError::DecodeFailed( - 315
"gabor_filter_bank_f: num_orientations必须大于0", - 316
) - 317
} - 318
- 319●
let img8 = imgf_to_img8_gabor(img) - 320●
let result8 = gabor_filter_bank( - 321
img8, - 322
ksize, - 323
sigma, - 324
num_orientations~, - 325
lambda~, - 326
gamma~, - 327
) - 328●
img8_to_imgf_gabor(result8) - 329
} - 330
- 331
// ========== Template Match 16-bit ========== - 332
- 333
///| - 334
/// 16-bit 模板匹配。 - 335
/// @see template_match 8-bit 版本,本函数为 Image16 变体 - 336
pub fn template_match_16( - 337
img : @types.Image16, - 338
template : @types.Image16, - 339
match_method~ : TemplateMatchMethod = TemplateMatchMethod::CCoeffNormed, - 340
) -> Array[TemplateMatchResult] raise @types.LoadError { - 341
// 输入验证 - 342●
if img.width <= 0 || img.height <= 0 { - 343●
raise @types.LoadError::DecodeFailed( - 344
"template_match_16: 图像尺寸必须大于0", - 345
) - 346
} - 347
if img.channels < 1 || img.channels > 4 { - 348●
raise @types.LoadError::DecodeFailed( - 349
"template_match_16: 通道数必须在1-4之间", - 350
) - 351
} - 352
let expected_size = img.width * img.height * img.channels * 2 - 353●
if img.data.length() != expected_size { - 354●
raise @types.LoadError::DecodeFailed( - 355
"template_match_16: 数据大小不匹配,期望 " + - 356●
expected_size.to_string() + - 357
",实际 " + - 358●
img.data.length().to_string(), - 359
) - 360
} - 361
if template.width <= 0 || template.height <= 0 { - 362●
raise @types.LoadError::DecodeFailed( - 363
"template_match_16: 模板尺寸必须大于0", - 364
) - 365
} - 366
if template.channels < 1 || template.channels > 4 { - 367●
raise @types.LoadError::DecodeFailed( - 368
"template_match_16: 模板通道数必须在1-4之间", - 369
) - 370
} - 371
let tpl_expected_size = template.width * - 372
template.height * - 373
template.channels * - 374
2 - 375●
if template.data.length() != tpl_expected_size { - 376●
raise @types.LoadError::DecodeFailed( - 377
"template_match_16: 模板数据大小不匹配,期望 " + - 378●
tpl_expected_size.to_string() + - 379
",实际 " + - 380●
template.data.length().to_string(), - 381
) - 382
} - 383
if template.width > img.width || template.height > img.height { - 384●
raise @types.LoadError::DecodeFailed( - 385
"template_match_16: 模板尺寸不能大于图像尺寸", - 386
) - 387
} - 388
- 389●
let img8 = img16_to_img8_gabor(img) - 390●
let tpl8 = img16_to_img8_gabor(template) - 391●
template_match(img8, tpl8, match_method~) - 392
} - 393
- 394
// ========== Template Match float ========== - 395
- 396
///| - 397
/// float 模板匹配。 - 398
/// @see template_match 8-bit 版本,本函数为 ImageF 变体 - 399
pub fn template_match_f( - 400
img : @types.ImageF, - 401
template : @types.ImageF, - 402
match_method~ : TemplateMatchMethod = TemplateMatchMethod::CCoeffNormed, - 403
) -> Array[TemplateMatchResult] raise @types.LoadError { - 404
// 输入验证 - 405●
if img.width <= 0 || img.height <= 0 { - 406●
raise @types.LoadError::DecodeFailed( - 407
"template_match_f: 图像尺寸必须大于0", - 408
) - 409
} - 410
if img.channels < 1 || img.channels > 4 { - 411●
raise @types.LoadError::DecodeFailed( - 412
"template_match_f: 通道数必须在1-4之间", - 413
) - 414
} - 415
let expected_size = img.width * img.height * img.channels * 4 - 416●
if img.data.length() != expected_size { - 417●
raise @types.LoadError::DecodeFailed( - 418
"template_match_f: 数据大小不匹配,期望 " + - 419●
expected_size.to_string() + - 420
",实际 " + - 421●
img.data.length().to_string(), - 422
) - 423
} - 424
if template.width <= 0 || template.height <= 0 { - 425●
raise @types.LoadError::DecodeFailed( - 426
"template_match_f: 模板尺寸必须大于0", - 427
) - 428
} - 429
if template.channels < 1 || template.channels > 4 { - 430○
raise @types.LoadError::DecodeFailed( - 431
"template_match_f: 模板通道数必须在1-4之间", - 432
) - 433
} - 434
let tpl_expected_size = template.width * - 435
template.height * - 436
template.channels * - 437
4 - 438●
if template.data.length() != tpl_expected_size { - 439○
raise @types.LoadError::DecodeFailed( - 440
"template_match_f: 模板数据大小不匹配,期望 " + - 441○
tpl_expected_size.to_string() + - 442
",实际 " + - 443○
template.data.length().to_string(), - 444
) - 445
} - 446
if template.width > img.width || template.height > img.height { - 447●
raise @types.LoadError::DecodeFailed( - 448
"template_match_f: 模板尺寸不能大于图像尺寸", - 449
) - 450
} - 451
- 452●
let img8 = imgf_to_img8_gabor(img) - 453●
let tpl8 = imgf_to_img8_gabor(template) - 454●
template_match(img8, tpl8, match_method~) - 455
} - 456
- 457
// ========== Template Match Best 16-bit ========== - 458
- 459
///| - 460
/// 16-bit 模板匹配(最佳匹配)。 - 461
/// @see template_match_best 8-bit 版本,本函数为 Image16 变体 - 462
pub fn template_match_best_16( - 463
img : @types.Image16, - 464
template : @types.Image16, - 465
match_method~ : TemplateMatchMethod = TemplateMatchMethod::CCoeffNormed, - 466
) -> TemplateMatchResult? raise @types.LoadError { - 467
// 输入验证 - 468●
if img.width <= 0 || img.height <= 0 { - 469●
raise @types.LoadError::DecodeFailed( - 470
"template_match_best_16: 图像尺寸必须大于0", - 471
) - 472
} - 473
if img.channels < 1 || img.channels > 4 { - 474●
raise @types.LoadError::DecodeFailed( - 475
"template_match_best_16: 通道数必须在1-4之间", - 476
) - 477
} - 478
let expected_size = img.width * img.height * img.channels * 2 - 479●
if img.data.length() != expected_size { - 480●
raise @types.LoadError::DecodeFailed( - 481
"template_match_best_16: 数据大小不匹配,期望 " + - 482●
expected_size.to_string() + - 483
",实际 " + - 484●
img.data.length().to_string(), - 485
) - 486
} - 487
if template.width <= 0 || template.height <= 0 { - 488○
raise @types.LoadError::DecodeFailed( - 489
"template_match_best_16: 模板尺寸必须大于0", - 490
) - 491
} - 492
if template.channels < 1 || template.channels > 4 { - 493○
raise @types.LoadError::DecodeFailed( - 494
"template_match_best_16: 模板通道数必须在1-4之间", - 495
) - 496
} - 497
let tpl_expected_size = template.width * - 498
template.height * - 499
template.channels * - 500
2 - 501●
if template.data.length() != tpl_expected_size { - 502○
raise @types.LoadError::DecodeFailed( - 503
"template_match_best_16: 模板数据大小不匹配,期望 " + - 504○
tpl_expected_size.to_string() + - 505
",实际 " + - 506○
template.data.length().to_string(), - 507
) - 508
} - 509
if template.width > img.width || template.height > img.height { - 510○
raise @types.LoadError::DecodeFailed( - 511
"template_match_best_16: 模板尺寸不能大于图像尺寸", - 512
) - 513
} - 514
- 515●
let img8 = img16_to_img8_gabor(img) - 516●
let tpl8 = img16_to_img8_gabor(template) - 517●
template_match_best(img8, tpl8, match_method~) - 518
} - 519
- 520
// ========== Template Match Best float ========== - 521
- 522
///| - 523
/// float 模板匹配(最佳匹配)。 - 524
/// @see template_match_best 8-bit 版本,本函数为 ImageF 变体 - 525
pub fn template_match_best_f( - 526
img : @types.ImageF, - 527
template : @types.ImageF, - 528
match_method~ : TemplateMatchMethod = TemplateMatchMethod::CCoeffNormed, - 529
) -> TemplateMatchResult? raise @types.LoadError { - 530
// 输入验证 - 531●
if img.width <= 0 || img.height <= 0 { - 532●
raise @types.LoadError::DecodeFailed( - 533
"template_match_best_f: 图像尺寸必须大于0", - 534
) - 535
} - 536
if img.channels < 1 || img.channels > 4 { - 537●
raise @types.LoadError::DecodeFailed( - 538
"template_match_best_f: 通道数必须在1-4之间", - 539
) - 540
} - 541
let expected_size = img.width * img.height * img.channels * 4 - 542●
if img.data.length() != expected_size { - 543●
raise @types.LoadError::DecodeFailed( - 544
"template_match_best_f: 数据大小不匹配,期望 " + - 545●
expected_size.to_string() + - 546
",实际 " + - 547●
img.data.length().to_string(), - 548
) - 549
} - 550
if template.width <= 0 || template.height <= 0 { - 551○
raise @types.LoadError::DecodeFailed( - 552
"template_match_best_f: 模板尺寸必须大于0", - 553
) - 554
} - 555
if template.channels < 1 || template.channels > 4 { - 556○
raise @types.LoadError::DecodeFailed( - 557
"template_match_best_f: 模板通道数必须在1-4之间", - 558
) - 559
} - 560
let tpl_expected_size = template.width * - 561
template.height * - 562
template.channels * - 563
4 - 564●
if template.data.length() != tpl_expected_size { - 565○
raise @types.LoadError::DecodeFailed( - 566
"template_match_best_f: 模板数据大小不匹配,期望 " + - 567○
tpl_expected_size.to_string() + - 568
",实际 " + - 569○
template.data.length().to_string(), - 570
) - 571
} - 572
if template.width > img.width || template.height > img.height { - 573○
raise @types.LoadError::DecodeFailed( - 574
"template_match_best_f: 模板尺寸不能大于图像尺寸", - 575
) - 576
} - 577
- 578●
let img8 = imgf_to_img8_gabor(img) - 579●
let tpl8 = imgf_to_img8_gabor(template) - 580●
template_match_best(img8, tpl8, match_method~) - 581
}