image.process\feature\optical_glcm_16f.mbt
83.9% · 130 / 155 points · 508 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 光流 + GLCM 16-bit/float 变体 Optical flow + GLCM 16-bit/float variants - 3
/// - 4
/// 16位/浮点光流/GLCM lucas_kanade_16 / glcm_16 - 5
/// 16位/浮点光流/GLCM 16-bit/float optical flow/GLCM - 6
// 光流 + GLCM 16-bit/float 变体 - 7
- 8
// ========== 辅助函数 ========== - 9
- 10
///| - 11
fn read_u16_le_of(data : Bytes, offset : Int32) -> Int32 { - 12●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 13
} - 14
- 15
///| - 16
fn read_float_le_of(data : Bytes, offset : Int32) -> Float { - 17●
let b0 = data[offset].to_int() - 18●
let b1 = data[offset + 1].to_int() - 19●
let b2 = data[offset + 2].to_int() - 20●
let b3 = data[offset + 3].to_int() - 21
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 22●
Float::reinterpret_from_int(bits) - 23
} - 24
- 25
///| - 26
fn img16_to_img8_of(img : @types.Image16) -> @types.Image { - 27●
let w = img.width - 28
let h = img.height - 29
let ch = img.channels - 30●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 31
for i in 0..<(w * h * ch) { - 32●
let v16 = read_u16_le_of(img.data, i * 2) - 33●
data8[i] = (v16 / 257).to_byte() - 34
} - 35
@types.Image::{ - 36
width: w, - 37
height: h, - 38
channels: ch, - 39●
data: Bytes::from_array(data8), - 40
} - 41
} - 42
- 43
///| - 44
fn imgf_to_img8_of(img : @types.ImageF) -> @types.Image { - 45●
let w = img.width - 46
let h = img.height - 47
let ch = img.channels - 48●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 49
for i in 0..<(w * h * ch) { - 50●
let vf = read_float_le_of(img.data, i * 4) - 51●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 52●
data8[i] = v8.to_byte() - 53
} - 54
@types.Image::{ - 55
width: w, - 56
height: h, - 57
channels: ch, - 58●
data: Bytes::from_array(data8), - 59
} - 60
} - 61
- 62
// ========== Lucas-Kanade 16-bit ========== - 63
- 64
///| - 65
/// 16-bit Lucas-Kanade 光流。 - 66
/// @see lucas_kanade 8-bit 版本,本函数为 Image16 变体 - 67
pub fn lucas_kanade_16( - 68
prev : @types.Image16, - 69
next : @types.Image16, - 70
points : Array[(Int32, Int32)], - 71
window_size~ : Int32 = 15, - 72
max_iter~ : Int32 = 20, - 73
) -> Array[FlowResult] raise @types.LoadError { - 74
// 输入验证 - 75●
if prev.width <= 0 || prev.height <= 0 { - 76●
raise @types.LoadError::DecodeFailed( - 77
"lucas_kanade_16: 前一帧图像尺寸必须大于0", - 78
) - 79
} - 80
if prev.channels < 1 || prev.channels > 4 { - 81●
raise @types.LoadError::DecodeFailed( - 82
"lucas_kanade_16: 前一帧通道数必须在1-4之间", - 83
) - 84
} - 85
let prev_expected_size = prev.width * prev.height * prev.channels * 2 - 86●
if prev.data.length() != prev_expected_size { - 87●
raise @types.LoadError::DecodeFailed( - 88
"lucas_kanade_16: 前一帧数据大小不匹配,期望 " + - 89●
prev_expected_size.to_string() + - 90
",实际 " + - 91●
prev.data.length().to_string(), - 92
) - 93
} - 94
if next.width <= 0 || next.height <= 0 { - 95●
raise @types.LoadError::DecodeFailed( - 96
"lucas_kanade_16: 后一帧图像尺寸必须大于0", - 97
) - 98
} - 99
if next.channels < 1 || next.channels > 4 { - 100●
raise @types.LoadError::DecodeFailed( - 101
"lucas_kanade_16: 后一帧通道数必须在1-4之间", - 102
) - 103
} - 104
let next_expected_size = next.width * next.height * next.channels * 2 - 105●
if next.data.length() != next_expected_size { - 106○
raise @types.LoadError::DecodeFailed( - 107
"lucas_kanade_16: 后一帧数据大小不匹配,期望 " + - 108○
next_expected_size.to_string() + - 109
",实际 " + - 110○
next.data.length().to_string(), - 111
) - 112
} - 113
if prev.width != next.width || prev.height != next.height { - 114●
raise @types.LoadError::DecodeFailed( - 115
"lucas_kanade_16: 两帧图像尺寸必须一致", - 116
) - 117
} - 118●
if points.length() == 0 { - 119●
raise @types.LoadError::DecodeFailed( - 120
"lucas_kanade_16: 点列表不能为空", - 121
) - 122
} - 123
if window_size <= 0 || window_size % 2 == 0 { - 124○
raise @types.LoadError::DecodeFailed( - 125
"lucas_kanade_16: window_size必须为正奇数", - 126
) - 127
} - 128
if max_iter <= 0 { - 129○
raise @types.LoadError::DecodeFailed( - 130
"lucas_kanade_16: max_iter必须大于0", - 131
) - 132
} - 133
- 134●
let prev8 = img16_to_img8_of(prev) - 135●
let next8 = img16_to_img8_of(next) - 136●
lucas_kanade(prev8, next8, points, window_size~, max_iter~) - 137
} - 138
- 139
// ========== Lucas-Kanade float ========== - 140
- 141
///| - 142
/// float Lucas-Kanade 光流。 - 143
/// @see lucas_kanade 8-bit 版本,本函数为 ImageF 变体 - 144
pub fn lucas_kanade_f( - 145
prev : @types.ImageF, - 146
next : @types.ImageF, - 147
points : Array[(Int32, Int32)], - 148
window_size~ : Int32 = 15, - 149
max_iter~ : Int32 = 20, - 150
) -> Array[FlowResult] raise @types.LoadError { - 151
// 输入验证 - 152●
if prev.width <= 0 || prev.height <= 0 { - 153●
raise @types.LoadError::DecodeFailed( - 154
"lucas_kanade_f: 前一帧图像尺寸必须大于0", - 155
) - 156
} - 157
if prev.channels < 1 || prev.channels > 4 { - 158●
raise @types.LoadError::DecodeFailed( - 159
"lucas_kanade_f: 前一帧通道数必须在1-4之间", - 160
) - 161
} - 162
let prev_expected_size = prev.width * prev.height * prev.channels * 4 - 163●
if prev.data.length() != prev_expected_size { - 164●
raise @types.LoadError::DecodeFailed( - 165
"lucas_kanade_f: 前一帧数据大小不匹配,期望 " + - 166●
prev_expected_size.to_string() + - 167
",实际 " + - 168●
prev.data.length().to_string(), - 169
) - 170
} - 171
if next.width <= 0 || next.height <= 0 { - 172○
raise @types.LoadError::DecodeFailed( - 173
"lucas_kanade_f: 后一帧图像尺寸必须大于0", - 174
) - 175
} - 176
if next.channels < 1 || next.channels > 4 { - 177○
raise @types.LoadError::DecodeFailed( - 178
"lucas_kanade_f: 后一帧通道数必须在1-4之间", - 179
) - 180
} - 181
let next_expected_size = next.width * next.height * next.channels * 4 - 182●
if next.data.length() != next_expected_size { - 183○
raise @types.LoadError::DecodeFailed( - 184
"lucas_kanade_f: 后一帧数据大小不匹配,期望 " + - 185○
next_expected_size.to_string() + - 186
",实际 " + - 187○
next.data.length().to_string(), - 188
) - 189
} - 190
if prev.width != next.width || prev.height != next.height { - 191○
raise @types.LoadError::DecodeFailed( - 192
"lucas_kanade_f: 两帧图像尺寸必须一致", - 193
) - 194
} - 195●
if points.length() == 0 { - 196●
raise @types.LoadError::DecodeFailed( - 197
"lucas_kanade_f: 点列表不能为空", - 198
) - 199
} - 200
if window_size <= 0 || window_size % 2 == 0 { - 201○
raise @types.LoadError::DecodeFailed( - 202
"lucas_kanade_f: window_size必须为正奇数", - 203
) - 204
} - 205
if max_iter <= 0 { - 206○
raise @types.LoadError::DecodeFailed( - 207
"lucas_kanade_f: max_iter必须大于0", - 208
) - 209
} - 210
- 211●
let prev8 = imgf_to_img8_of(prev) - 212●
let next8 = imgf_to_img8_of(next) - 213●
lucas_kanade(prev8, next8, points, window_size~, max_iter~) - 214
} - 215
- 216
// ========== Horn-Schunck 16-bit ========== - 217
- 218
///| - 219
/// 16-bit Horn-Schunck 密集光流。 - 220
/// @see horn_schunck 8-bit 版本,本函数为 Image16 变体 - 221
pub fn horn_schunck_16( - 222
prev : @types.Image16, - 223
next : @types.Image16, - 224
alpha~ : Float = 1.0F, - 225
iterations~ : Int32 = 100, - 226
) -> (Array[Float], Array[Float]) raise @types.LoadError { - 227
// 输入验证 - 228●
if prev.width <= 0 || prev.height <= 0 { - 229●
raise @types.LoadError::DecodeFailed( - 230
"horn_schunck_16: 前一帧图像尺寸必须大于0", - 231
) - 232
} - 233
if prev.channels < 1 || prev.channels > 4 { - 234●
raise @types.LoadError::DecodeFailed( - 235
"horn_schunck_16: 前一帧通道数必须在1-4之间", - 236
) - 237
} - 238
let prev_expected_size = prev.width * prev.height * prev.channels * 2 - 239●
if prev.data.length() != prev_expected_size { - 240●
raise @types.LoadError::DecodeFailed( - 241
"horn_schunck_16: 前一帧数据大小不匹配,期望 " + - 242●
prev_expected_size.to_string() + - 243
",实际 " + - 244●
prev.data.length().to_string(), - 245
) - 246
} - 247
if next.width <= 0 || next.height <= 0 { - 248○
raise @types.LoadError::DecodeFailed( - 249
"horn_schunck_16: 后一帧图像尺寸必须大于0", - 250
) - 251
} - 252
if next.channels < 1 || next.channels > 4 { - 253○
raise @types.LoadError::DecodeFailed( - 254
"horn_schunck_16: 后一帧通道数必须在1-4之间", - 255
) - 256
} - 257
let next_expected_size = next.width * next.height * next.channels * 2 - 258●
if next.data.length() != next_expected_size { - 259○
raise @types.LoadError::DecodeFailed( - 260
"horn_schunck_16: 后一帧数据大小不匹配,期望 " + - 261○
next_expected_size.to_string() + - 262
",实际 " + - 263○
next.data.length().to_string(), - 264
) - 265
} - 266
if prev.width != next.width || prev.height != next.height { - 267○
raise @types.LoadError::DecodeFailed( - 268
"horn_schunck_16: 两帧图像尺寸必须一致", - 269
) - 270
} - 271
if alpha <= 0.0F { - 272●
raise @types.LoadError::DecodeFailed("horn_schunck_16: alpha必须大于0") - 273
} - 274
if iterations <= 0 { - 275●
raise @types.LoadError::DecodeFailed( - 276
"horn_schunck_16: iterations必须大于0", - 277
) - 278
} - 279
- 280●
let prev8 = img16_to_img8_of(prev) - 281●
let next8 = img16_to_img8_of(next) - 282●
horn_schunck(prev8, next8, alpha~, iterations~) - 283
} - 284
- 285
// ========== Horn-Schunck float ========== - 286
- 287
///| - 288
/// float Horn-Schunck 密集光流。 - 289
/// @see horn_schunck 8-bit 版本,本函数为 ImageF 变体 - 290
pub fn horn_schunck_f( - 291
prev : @types.ImageF, - 292
next : @types.ImageF, - 293
alpha~ : Float = 1.0F, - 294
iterations~ : Int32 = 100, - 295
) -> (Array[Float], Array[Float]) raise @types.LoadError { - 296
// 输入验证 - 297●
if prev.width <= 0 || prev.height <= 0 { - 298●
raise @types.LoadError::DecodeFailed( - 299
"horn_schunck_f: 前一帧图像尺寸必须大于0", - 300
) - 301
} - 302
if prev.channels < 1 || prev.channels > 4 { - 303●
raise @types.LoadError::DecodeFailed( - 304
"horn_schunck_f: 前一帧通道数必须在1-4之间", - 305
) - 306
} - 307
let prev_expected_size = prev.width * prev.height * prev.channels * 4 - 308●
if prev.data.length() != prev_expected_size { - 309●
raise @types.LoadError::DecodeFailed( - 310
"horn_schunck_f: 前一帧数据大小不匹配,期望 " + - 311●
prev_expected_size.to_string() + - 312
",实际 " + - 313●
prev.data.length().to_string(), - 314
) - 315
} - 316
if next.width <= 0 || next.height <= 0 { - 317○
raise @types.LoadError::DecodeFailed( - 318
"horn_schunck_f: 后一帧图像尺寸必须大于0", - 319
) - 320
} - 321
if next.channels < 1 || next.channels > 4 { - 322○
raise @types.LoadError::DecodeFailed( - 323
"horn_schunck_f: 后一帧通道数必须在1-4之间", - 324
) - 325
} - 326
let next_expected_size = next.width * next.height * next.channels * 4 - 327●
if next.data.length() != next_expected_size { - 328●
raise @types.LoadError::DecodeFailed( - 329
"horn_schunck_f: 后一帧数据大小不匹配,期望 " + - 330●
next_expected_size.to_string() + - 331
",实际 " + - 332●
next.data.length().to_string(), - 333
) - 334
} - 335
if prev.width != next.width || prev.height != next.height { - 336○
raise @types.LoadError::DecodeFailed( - 337
"horn_schunck_f: 两帧图像尺寸必须一致", - 338
) - 339
} - 340
if alpha <= 0.0F { - 341●
raise @types.LoadError::DecodeFailed("horn_schunck_f: alpha必须大于0") - 342
} - 343
if iterations <= 0 { - 344●
raise @types.LoadError::DecodeFailed( - 345
"horn_schunck_f: iterations必须大于0", - 346
) - 347
} - 348
- 349●
let prev8 = imgf_to_img8_of(prev) - 350●
let next8 = imgf_to_img8_of(next) - 351●
horn_schunck(prev8, next8, alpha~, iterations~) - 352
} - 353
- 354
// ========== GLCM 16-bit ========== - 355
- 356
///| - 357
/// 16-bit 灰度共生矩阵。 - 358
/// @see compute_glcm 8-bit 版本,本函数为 Image16 变体 - 359
pub fn compute_glcm_16( - 360
img : @types.Image16, - 361
dx : Int32, - 362
dy : Int32, - 363
levels~ : Int32 = 256, - 364
) -> Array[Array[Int32]] raise @types.LoadError { - 365
// 输入验证 - 366●
if img.width <= 0 || img.height <= 0 { - 367●
raise @types.LoadError::DecodeFailed( - 368
"compute_glcm_16: 图像尺寸必须大于0", - 369
) - 370
} - 371
if img.channels < 1 || img.channels > 4 { - 372●
raise @types.LoadError::DecodeFailed( - 373
"compute_glcm_16: 通道数必须在1-4之间", - 374
) - 375
} - 376
let expected_size = img.width * img.height * img.channels * 2 - 377●
if img.data.length() != expected_size { - 378●
raise @types.LoadError::DecodeFailed( - 379
"compute_glcm_16: 数据大小不匹配,期望 " + - 380●
expected_size.to_string() + - 381
",实际 " + - 382●
img.data.length().to_string(), - 383
) - 384
} - 385
if levels <= 0 { - 386●
raise @types.LoadError::DecodeFailed("compute_glcm_16: levels必须大于0") - 387
} - 388
- 389●
let img8 = img16_to_img8_of(img) - 390●
compute_glcm(img8, dx, dy, levels~) - 391
} - 392
- 393
// ========== GLCM float ========== - 394
- 395
///| - 396
/// float 灰度共生矩阵。 - 397
/// @see compute_glcm 8-bit 版本,本函数为 ImageF 变体 - 398
pub fn compute_glcm_f( - 399
img : @types.ImageF, - 400
dx : Int32, - 401
dy : Int32, - 402
levels~ : Int32 = 256, - 403
) -> Array[Array[Int32]] raise @types.LoadError { - 404
// 输入验证 - 405●
if img.width <= 0 || img.height <= 0 { - 406●
raise @types.LoadError::DecodeFailed( - 407
"compute_glcm_f: 图像尺寸必须大于0", - 408
) - 409
} - 410
if img.channels < 1 || img.channels > 4 { - 411●
raise @types.LoadError::DecodeFailed( - 412
"compute_glcm_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
"compute_glcm_f: 数据大小不匹配,期望 " + - 419●
expected_size.to_string() + - 420
",实际 " + - 421●
img.data.length().to_string(), - 422
) - 423
} - 424
if levels <= 0 { - 425●
raise @types.LoadError::DecodeFailed("compute_glcm_f: levels必须大于0") - 426
} - 427
- 428●
let img8 = imgf_to_img8_of(img) - 429●
compute_glcm(img8, dx, dy, levels~) - 430
} - 431
- 432
// ========== GLCM Features Multi Direction 16-bit ========== - 433
- 434
///| - 435
/// 16-bit 多方向 GLCM 特征。 - 436
/// @see glcm_features_multi_direction 8-bit 版本,本函数为 Image16 变体 - 437
pub fn glcm_features_multi_direction_16( - 438
img : @types.Image16, - 439
levels~ : Int32 = 256, - 440
) -> Array[GlcmFeatures] raise @types.LoadError { - 441
// 输入验证 - 442●
if img.width <= 0 || img.height <= 0 { - 443●
raise @types.LoadError::DecodeFailed( - 444
"glcm_features_multi_direction_16: 图像尺寸必须大于0", - 445
) - 446
} - 447
if img.channels < 1 || img.channels > 4 { - 448●
raise @types.LoadError::DecodeFailed( - 449
"glcm_features_multi_direction_16: 通道数必须在1-4之间", - 450
) - 451
} - 452
let expected_size = img.width * img.height * img.channels * 2 - 453●
if img.data.length() != expected_size { - 454●
raise @types.LoadError::DecodeFailed( - 455
"glcm_features_multi_direction_16: 数据大小不匹配,期望 " + - 456●
expected_size.to_string() + - 457
",实际 " + - 458●
img.data.length().to_string(), - 459
) - 460
} - 461
if levels <= 0 { - 462●
raise @types.LoadError::DecodeFailed( - 463
"glcm_features_multi_direction_16: levels必须大于0", - 464
) - 465
} - 466
- 467●
let img8 = img16_to_img8_of(img) - 468●
glcm_features_multi_direction(img8, levels~) - 469
} - 470
- 471
// ========== GLCM Features Multi Direction float ========== - 472
- 473
///| - 474
/// float 多方向 GLCM 特征。 - 475
/// @see glcm_features_multi_direction 8-bit 版本,本函数为 ImageF 变体 - 476
pub fn glcm_features_multi_direction_f( - 477
img : @types.ImageF, - 478
levels~ : Int32 = 256, - 479
) -> Array[GlcmFeatures] raise @types.LoadError { - 480
// 输入验证 - 481●
if img.width <= 0 || img.height <= 0 { - 482●
raise @types.LoadError::DecodeFailed( - 483
"glcm_features_multi_direction_f: 图像尺寸必须大于0", - 484
) - 485
} - 486
if img.channels < 1 || img.channels > 4 { - 487●
raise @types.LoadError::DecodeFailed( - 488
"glcm_features_multi_direction_f: 通道数必须在1-4之间", - 489
) - 490
} - 491
let expected_size = img.width * img.height * img.channels * 4 - 492●
if img.data.length() != expected_size { - 493●
raise @types.LoadError::DecodeFailed( - 494
"glcm_features_multi_direction_f: 数据大小不匹配,期望 " + - 495●
expected_size.to_string() + - 496
",实际 " + - 497●
img.data.length().to_string(), - 498
) - 499
} - 500
if levels <= 0 { - 501●
raise @types.LoadError::DecodeFailed( - 502
"glcm_features_multi_direction_f: levels必须大于0", - 503
) - 504
} - 505
- 506●
let img8 = imgf_to_img8_of(img) - 507●
glcm_features_multi_direction(img8, levels~) - 508
}