image.process\feature\gabor_template_16f.mbt

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