image.process\feature\feature_advanced_16f.mbt
100% · 123 / 123 points · 439 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 特征检测高级 16-bit/float 变体 Advanced feature detection 16-bit/float variants - 3
/// - 4
/// 16位 Harris/ORB/SIFT harris_16 / orb_16 / sift_16 - 5
/// 16位/浮点 Harris/ORB/SIFT 16-bit/float Harris/ORB/SIFT - 6
// 特征检测高级 16-bit/float 变体 - 7
// harris / sift / orb / shi_tomasi / gabor - 8
- 9
// ========== 辅助函数 ========== - 10
- 11
///| - 12
fn read_u16_le_feat(data : Bytes, offset : Int32) -> Int32 { - 13●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 14
} - 15
- 16
///| - 17
fn read_float_le_feat(data : Bytes, offset : Int32) -> Float { - 18●
let b0 = data[offset].to_int() - 19●
let b1 = data[offset + 1].to_int() - 20●
let b2 = data[offset + 2].to_int() - 21●
let b3 = data[offset + 3].to_int() - 22
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 23●
Float::reinterpret_from_int(bits) - 24
} - 25
- 26
///| - 27
fn img16_to_img8_feat(img : @types.Image16) -> @types.Image { - 28●
let w = img.width - 29
let h = img.height - 30
let ch = img.channels - 31●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 32
for i in 0..<(w * h * ch) { - 33●
let v16 = read_u16_le_feat(img.data, i * 2) - 34●
data8[i] = (v16 / 257).to_byte() - 35
} - 36
@types.Image::{ - 37
width: w, - 38
height: h, - 39
channels: ch, - 40●
data: Bytes::from_array(data8), - 41
} - 42
} - 43
- 44
///| - 45
fn imgf_to_img8_feat(img : @types.ImageF) -> @types.Image { - 46●
let w = img.width - 47
let h = img.height - 48
let ch = img.channels - 49●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 50
for i in 0..<(w * h * ch) { - 51●
let vf = read_float_le_feat(img.data, i * 4) - 52●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 53●
data8[i] = v8.to_byte() - 54
} - 55
@types.Image::{ - 56
width: w, - 57
height: h, - 58
channels: ch, - 59●
data: Bytes::from_array(data8), - 60
} - 61
} - 62
- 63
// ========== Harris 16-bit ========== - 64
- 65
///| - 66
/// 16-bit Harris 角点检测。 - 67
/// @see harris_corners 8-bit 版本,本函数为 Image16 变体 - 68
pub fn harris_corners_16( - 69
img : @types.Image16, - 70
k~ : Float = 0.04F, - 71
threshold~ : Float = 1000.0F, - 72
min_distance~ : Int32 = 5, - 73
) -> Array[CornerPoint] raise @types.LoadError { - 74
// 输入验证 - 75●
if img.width <= 0 || img.height <= 0 { - 76●
raise @types.LoadError::DecodeFailed( - 77
"harris_corners_16: 图像尺寸必须大于0", - 78
) - 79
} - 80
if img.channels < 1 || img.channels > 4 { - 81●
raise @types.LoadError::DecodeFailed( - 82
"harris_corners_16: 通道数必须在1-4之间", - 83
) - 84
} - 85
let expected_size = img.width * img.height * img.channels * 2 - 86●
if img.data.length() != expected_size { - 87●
raise @types.LoadError::DecodeFailed( - 88
"harris_corners_16: 数据大小不匹配,期望 " + - 89●
expected_size.to_string() + - 90
",实际 " + - 91●
img.data.length().to_string(), - 92
) - 93
} - 94
if k <= 0.0F { - 95●
raise @types.LoadError::DecodeFailed("harris_corners_16: k必须大于0") - 96
} - 97
if threshold <= 0.0F { - 98●
raise @types.LoadError::DecodeFailed( - 99
"harris_corners_16: threshold必须大于0", - 100
) - 101
} - 102
if min_distance <= 0 { - 103●
raise @types.LoadError::DecodeFailed( - 104
"harris_corners_16: min_distance必须大于0", - 105
) - 106
} - 107
- 108●
let img8 = img16_to_img8_feat(img) - 109●
harris_corners(img8, k~, threshold~, min_distance~) - 110
} - 111
- 112
// ========== Harris float ========== - 113
- 114
///| - 115
/// float Harris 角点检测。 - 116
/// @see harris_corners 8-bit 版本,本函数为 ImageF 变体 - 117
pub fn harris_corners_f( - 118
img : @types.ImageF, - 119
k~ : Float = 0.04F, - 120
threshold~ : Float = 1000.0F, - 121
min_distance~ : Int32 = 5, - 122
) -> Array[CornerPoint] raise @types.LoadError { - 123
// 输入验证 - 124●
if img.width <= 0 || img.height <= 0 { - 125●
raise @types.LoadError::DecodeFailed( - 126
"harris_corners_f: 图像尺寸必须大于0", - 127
) - 128
} - 129
if img.channels < 1 || img.channels > 4 { - 130●
raise @types.LoadError::DecodeFailed( - 131
"harris_corners_f: 通道数必须在1-4之间", - 132
) - 133
} - 134
let expected_size = img.width * img.height * img.channels * 4 - 135●
if img.data.length() != expected_size { - 136●
raise @types.LoadError::DecodeFailed( - 137
"harris_corners_f: 数据大小不匹配,期望 " + - 138●
expected_size.to_string() + - 139
",实际 " + - 140●
img.data.length().to_string(), - 141
) - 142
} - 143
if k <= 0.0F { - 144●
raise @types.LoadError::DecodeFailed("harris_corners_f: k必须大于0") - 145
} - 146
if threshold <= 0.0F { - 147●
raise @types.LoadError::DecodeFailed( - 148
"harris_corners_f: threshold必须大于0", - 149
) - 150
} - 151
if min_distance <= 0 { - 152●
raise @types.LoadError::DecodeFailed( - 153
"harris_corners_f: min_distance必须大于0", - 154
) - 155
} - 156
- 157●
let img8 = imgf_to_img8_feat(img) - 158●
harris_corners(img8, k~, threshold~, min_distance~) - 159
} - 160
- 161
// ========== SIFT 16-bit ========== - 162
- 163
///| - 164
/// 16-bit SIFT 特征检测。 - 165
/// @see sift_detect 8-bit 版本,本函数为 Image16 变体 - 166
pub fn sift_detect_16( - 167
img : @types.Image16, - 168
max_features~ : Int32 = 500, - 169
contrast_threshold~ : Float = 0.04F, - 170
) -> Array[SiftDescriptor] raise @types.LoadError { - 171
// 输入验证 - 172●
if img.width <= 0 || img.height <= 0 { - 173●
raise @types.LoadError::DecodeFailed( - 174
"sift_detect_16: 图像尺寸必须大于0", - 175
) - 176
} - 177
if img.channels < 1 || img.channels > 4 { - 178●
raise @types.LoadError::DecodeFailed( - 179
"sift_detect_16: 通道数必须在1-4之间", - 180
) - 181
} - 182
let expected_size = img.width * img.height * img.channels * 2 - 183●
if img.data.length() != expected_size { - 184●
raise @types.LoadError::DecodeFailed( - 185
"sift_detect_16: 数据大小不匹配,期望 " + - 186●
expected_size.to_string() + - 187
",实际 " + - 188●
img.data.length().to_string(), - 189
) - 190
} - 191
if max_features <= 0 { - 192●
raise @types.LoadError::DecodeFailed( - 193
"sift_detect_16: max_features必须大于0", - 194
) - 195
} - 196
if contrast_threshold <= 0.0F { - 197●
raise @types.LoadError::DecodeFailed( - 198
"sift_detect_16: contrast_threshold必须大于0", - 199
) - 200
} - 201
- 202●
let img8 = img16_to_img8_feat(img) - 203●
sift_detect(img8, max_features~, contrast_threshold~) - 204
} - 205
- 206
// ========== SIFT float ========== - 207
- 208
///| - 209
/// float SIFT 特征检测。 - 210
/// @see sift_detect 8-bit 版本,本函数为 ImageF 变体 - 211
pub fn sift_detect_f( - 212
img : @types.ImageF, - 213
max_features~ : Int32 = 500, - 214
contrast_threshold~ : Float = 0.04F, - 215
) -> Array[SiftDescriptor] raise @types.LoadError { - 216
// 输入验证 - 217●
if img.width <= 0 || img.height <= 0 { - 218●
raise @types.LoadError::DecodeFailed( - 219
"sift_detect_f: 图像尺寸必须大于0", - 220
) - 221
} - 222
if img.channels < 1 || img.channels > 4 { - 223●
raise @types.LoadError::DecodeFailed( - 224
"sift_detect_f: 通道数必须在1-4之间", - 225
) - 226
} - 227
let expected_size = img.width * img.height * img.channels * 4 - 228●
if img.data.length() != expected_size { - 229●
raise @types.LoadError::DecodeFailed( - 230
"sift_detect_f: 数据大小不匹配,期望 " + - 231●
expected_size.to_string() + - 232
",实际 " + - 233●
img.data.length().to_string(), - 234
) - 235
} - 236
if max_features <= 0 { - 237●
raise @types.LoadError::DecodeFailed( - 238
"sift_detect_f: max_features必须大于0", - 239
) - 240
} - 241
if contrast_threshold <= 0.0F { - 242●
raise @types.LoadError::DecodeFailed( - 243
"sift_detect_f: contrast_threshold必须大于0", - 244
) - 245
} - 246
- 247●
let img8 = imgf_to_img8_feat(img) - 248●
sift_detect(img8, max_features~, contrast_threshold~) - 249
} - 250
- 251
// ========== ORB 16-bit ========== - 252
- 253
///| - 254
/// 16-bit ORB 特征检测。 - 255
/// @see orb_detect 8-bit 版本,本函数为 Image16 变体 - 256
pub fn orb_detect_16( - 257
img : @types.Image16, - 258
max_features~ : Int32 = 500, - 259
threshold~ : Int32 = 20, - 260
) -> Array[OrbDescriptor] raise @types.LoadError { - 261
// 输入验证 - 262●
if img.width <= 0 || img.height <= 0 { - 263●
raise @types.LoadError::DecodeFailed( - 264
"orb_detect_16: 图像尺寸必须大于0", - 265
) - 266
} - 267
if img.channels < 1 || img.channels > 4 { - 268●
raise @types.LoadError::DecodeFailed( - 269
"orb_detect_16: 通道数必须在1-4之间", - 270
) - 271
} - 272
let expected_size = img.width * img.height * img.channels * 2 - 273●
if img.data.length() != expected_size { - 274●
raise @types.LoadError::DecodeFailed( - 275
"orb_detect_16: 数据大小不匹配,期望 " + - 276●
expected_size.to_string() + - 277
",实际 " + - 278●
img.data.length().to_string(), - 279
) - 280
} - 281
if max_features <= 0 { - 282●
raise @types.LoadError::DecodeFailed( - 283
"orb_detect_16: max_features必须大于0", - 284
) - 285
} - 286
if threshold <= 0 { - 287●
raise @types.LoadError::DecodeFailed( - 288
"orb_detect_16: threshold必须大于0", - 289
) - 290
} - 291
- 292●
let img8 = img16_to_img8_feat(img) - 293●
orb_detect(img8, max_features~, threshold~) - 294
} - 295
- 296
// ========== ORB float ========== - 297
- 298
///| - 299
/// float ORB 特征检测。 - 300
/// @see orb_detect 8-bit 版本,本函数为 ImageF 变体 - 301
pub fn orb_detect_f( - 302
img : @types.ImageF, - 303
max_features~ : Int32 = 500, - 304
threshold~ : Int32 = 20, - 305
) -> Array[OrbDescriptor] raise @types.LoadError { - 306
// 输入验证 - 307●
if img.width <= 0 || img.height <= 0 { - 308●
raise @types.LoadError::DecodeFailed( - 309
"orb_detect_f: 图像尺寸必须大于0", - 310
) - 311
} - 312
if img.channels < 1 || img.channels > 4 { - 313●
raise @types.LoadError::DecodeFailed( - 314
"orb_detect_f: 通道数必须在1-4之间", - 315
) - 316
} - 317
let expected_size = img.width * img.height * img.channels * 4 - 318●
if img.data.length() != expected_size { - 319●
raise @types.LoadError::DecodeFailed( - 320
"orb_detect_f: 数据大小不匹配,期望 " + - 321●
expected_size.to_string() + - 322
",实际 " + - 323●
img.data.length().to_string(), - 324
) - 325
} - 326
if max_features <= 0 { - 327●
raise @types.LoadError::DecodeFailed( - 328
"orb_detect_f: max_features必须大于0", - 329
) - 330
} - 331
if threshold <= 0 { - 332●
raise @types.LoadError::DecodeFailed("orb_detect_f: threshold必须大于0") - 333
} - 334
- 335●
let img8 = imgf_to_img8_feat(img) - 336●
orb_detect(img8, max_features~, threshold~) - 337
} - 338
- 339
// ========== Shi-Tomasi 16-bit ========== - 340
- 341
///| - 342
/// 16-bit Shi-Tomasi 角点检测。 - 343
/// @see good_features_to_track 8-bit 版本,本函数为 Image16 变体 - 344
pub fn good_features_to_track_16( - 345
img : @types.Image16, - 346
max_corners : Int32, - 347
quality_level : Float, - 348
min_distance~ : Int32 = 5, - 349
) -> Array[CornerPoint] raise @types.LoadError { - 350
// 输入验证 - 351●
if img.width <= 0 || img.height <= 0 { - 352●
raise @types.LoadError::DecodeFailed( - 353
"good_features_to_track_16: 图像尺寸必须大于0", - 354
) - 355
} - 356
if img.channels < 1 || img.channels > 4 { - 357●
raise @types.LoadError::DecodeFailed( - 358
"good_features_to_track_16: 通道数必须在1-4之间", - 359
) - 360
} - 361
let expected_size = img.width * img.height * img.channels * 2 - 362●
if img.data.length() != expected_size { - 363●
raise @types.LoadError::DecodeFailed( - 364
"good_features_to_track_16: 数据大小不匹配,期望 " + - 365●
expected_size.to_string() + - 366
",实际 " + - 367●
img.data.length().to_string(), - 368
) - 369
} - 370
if max_corners <= 0 { - 371●
raise @types.LoadError::DecodeFailed( - 372
"good_features_to_track_16: max_corners必须大于0", - 373
) - 374
} - 375
if quality_level <= 0.0F { - 376●
raise @types.LoadError::DecodeFailed( - 377
"good_features_to_track_16: quality_level必须大于0", - 378
) - 379
} - 380
if min_distance <= 0 { - 381●
raise @types.LoadError::DecodeFailed( - 382
"good_features_to_track_16: min_distance必须大于0", - 383
) - 384
} - 385
- 386●
let img8 = img16_to_img8_feat(img) - 387●
good_features_to_track(img8, max_corners, quality_level, min_distance~) - 388
} - 389
- 390
// ========== Shi-Tomasi float ========== - 391
- 392
///| - 393
/// float Shi-Tomasi 角点检测。 - 394
/// @see good_features_to_track 8-bit 版本,本函数为 ImageF 变体 - 395
pub fn good_features_to_track_f( - 396
img : @types.ImageF, - 397
max_corners : Int32, - 398
quality_level : Float, - 399
min_distance~ : Int32 = 5, - 400
) -> Array[CornerPoint] raise @types.LoadError { - 401
// 输入验证 - 402●
if img.width <= 0 || img.height <= 0 { - 403●
raise @types.LoadError::DecodeFailed( - 404
"good_features_to_track_f: 图像尺寸必须大于0", - 405
) - 406
} - 407
if img.channels < 1 || img.channels > 4 { - 408●
raise @types.LoadError::DecodeFailed( - 409
"good_features_to_track_f: 通道数必须在1-4之间", - 410
) - 411
} - 412
let expected_size = img.width * img.height * img.channels * 4 - 413●
if img.data.length() != expected_size { - 414●
raise @types.LoadError::DecodeFailed( - 415
"good_features_to_track_f: 数据大小不匹配,期望 " + - 416●
expected_size.to_string() + - 417
",实际 " + - 418●
img.data.length().to_string(), - 419
) - 420
} - 421
if max_corners <= 0 { - 422●
raise @types.LoadError::DecodeFailed( - 423
"good_features_to_track_f: max_corners必须大于0", - 424
) - 425
} - 426
if quality_level <= 0.0F { - 427●
raise @types.LoadError::DecodeFailed( - 428
"good_features_to_track_f: quality_level必须大于0", - 429
) - 430
} - 431
if min_distance <= 0 { - 432●
raise @types.LoadError::DecodeFailed( - 433
"good_features_to_track_f: min_distance必须大于0", - 434
) - 435
} - 436
- 437●
let img8 = imgf_to_img8_feat(img) - 438●
good_features_to_track(img8, max_corners, quality_level, min_distance~) - 439
}