image.process\feature\feature_advanced_16f.mbt

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