image.process\feature\optical_glcm_16f.mbt

83.9% · 130 / 155 points · 508 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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