image.process\feature\lbp_16f.mbt

93.2% · 221 / 237 points · 590 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 变体 Local Binary Pattern 16-bit/float variants
  3. 3///
  4. 4/// 16位/浮点 LBP lbp_16 / lbp_f
  5. 5/// 16位/浮点 LBP 16-bit/float LBP
  6. 6// 局部二值模式(LBP)16-bit/float 变体
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn read_u16_le_l(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 write_u16_le_l(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  18. 18● buf[offset] = (val & 0xFF).to_byte()
  19. 19● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  20. 20}
  21. 21
  22. 22///|
  23. 23fn read_float_le_l(data : Bytes, offset : Int32) -> Float {
  24. 24● let b0 = data[offset].to_int()
  25. 25● let b1 = data[offset + 1].to_int()
  26. 26● let b2 = data[offset + 2].to_int()
  27. 27● let b3 = data[offset + 3].to_int()
  28. 28 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  29. 29● Float::reinterpret_from_int(bits)
  30. 30}
  31. 31
  32. 32///|
  33. 33fn write_f32_le_l(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  34. 34● let bits = val.reinterpret_as_int()
  35. 35● buf[offset] = (bits & 0xFF).to_byte()
  36. 36● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  37. 37● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  38. 38● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  39. 39}
  40. 40
  41. 41///|
  42. 42fn get_pixel_safe_16(
  43. 43 src : Array[Int32],
  44. 44 x : Int32,
  45. 45 y : Int32,
  46. 46 c : Int32,
  47. 47 ch : Int32,
  48. 48 w : Int32,
  49. 49 h : Int32,
  50. 50) -> Int32 {
  51. 51● let sx = @types.clamp_int(x, 0, w - 1)
  52. 52● let sy = @types.clamp_int(y, 0, h - 1)
  53. 53 src[(sy * w + sx) * ch + c]
  54. 54}
  55. 55
  56. 56///|
  57. 57fn get_pixel_safe_f(
  58. 58 src : Array[Float],
  59. 59 x : Int32,
  60. 60 y : Int32,
  61. 61 c : Int32,
  62. 62 ch : Int32,
  63. 63 w : Int32,
  64. 64 h : Int32,
  65. 65) -> Float {
  66. 66● let sx = @types.clamp_int(x, 0, w - 1)
  67. 67● let sy = @types.clamp_int(y, 0, h - 1)
  68. 68 src[(sy * w + sx) * ch + c]
  69. 69}
  70. 70
  71. 71// 计算均匀 LBP 映射(跳变次数 <= 2)
  72. 72
  73. 73///|
  74. 74fn uniform_lbp_map(lbp_val : Int32) -> Int32 {
  75. 75 // 计算 0->1 和 1->0 跳变次数
  76. 76● let mut transitions = 0
  77. 77 let mut prev = (lbp_val >> 7) & 1
  78. 78 for i in 0..<8 {
  79. 79● let curr = (lbp_val >> i) & 1
  80. 80 if curr != prev {
  81. 81○ transitions += 1
  82. 82 }
  83. 83 prev = curr
  84. 84 }
  85. 85 if transitions <= 2 {
  86. 86 // 均匀模式:计算 1 的个数作为标签(0-8)
  87. 87● let mut count = 0
  88. 88 for i in 0..<8 {
  89. 89● if ((lbp_val >> i) & 1) == 1 {
  90. 90● count += 1
  91. 91 }
  92. 92 }
  93. 93 count
  94. 94 } else {
  95. 95 // 非均匀模式
  96. 96○ 9
  97. 97 }
  98. 98}
  99. 99
  100. 100// ========== 基本 LBP ==========
  101. 101
  102. 102///|
  103. 103/// 16-bit 基本局部二值模式(LBP)。输出 0-255 的编码值,存储为 16-bit。
  104. 104/// @see lbp 8-bit 版本,本函数为 Image16 变体
  105. 105pub fn lbp_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError {
  106. 106 // 输入验证
  107. 107● if img.width <= 0 || img.height <= 0 {
  108. 108● raise @types.LoadError::DecodeFailed("lbp_16: 图像尺寸必须大于0")
  109. 109 }
  110. 110 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  111. 111● raise @types.LoadError::DecodeFailed("lbp_16: 仅支持 channels=1/3/4")
  112. 112 }
  113. 113 let expected_size = img.width * img.height * img.channels * 2
  114. 114● if img.data.length() != expected_size {
  115. 115● raise @types.LoadError::DecodeFailed(
  116. 116 "lbp_16: 数据大小不匹配,期望 " +
  117. 117● expected_size.to_string() +
  118. 118 ",实际 " +
  119. 119● img.data.length().to_string(),
  120. 120 )
  121. 121 }
  122. 122 let ch = img.channels
  123. 123● let color_ch = if ch >= 3 { 3 } else { 1 }
  124. 124 let w = img.width
  125. 125 let h = img.height
  126. 126 let n = w * h
  127. 127 let bpp = ch * 2
  128. 128● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  129. 129● let src : Array[Int32] = Array::make(n * ch, 0)
  130. 130 for i in 0..<(n * ch) {
  131. 131● src[i] = read_u16_le_l(img.data, i * 2)
  132. 132 }
  133. 133 for c in 0..<color_ch {
  134. 134● for y in 1..<(h - 1) {
  135. 135● for x in 1..<(w - 1) {
  136. 136● let center = src[(y * w + x) * ch + c]
  137. 137 let mut lbp_val = 0
  138. 138 if src[((y - 1) * w + (x - 1)) * ch + c] >= center {
  139. 139● lbp_val = lbp_val | 1
  140. 140 }
  141. 141 if src[((y - 1) * w + x) * ch + c] >= center {
  142. 142● lbp_val = lbp_val | 2
  143. 143 }
  144. 144 if src[((y - 1) * w + (x + 1)) * ch + c] >= center {
  145. 145● lbp_val = lbp_val | 4
  146. 146 }
  147. 147 if src[(y * w + (x + 1)) * ch + c] >= center {
  148. 148● lbp_val = lbp_val | 8
  149. 149 }
  150. 150 if src[((y + 1) * w + (x + 1)) * ch + c] >= center {
  151. 151● lbp_val = lbp_val | 16
  152. 152 }
  153. 153 if src[((y + 1) * w + x) * ch + c] >= center {
  154. 154● lbp_val = lbp_val | 32
  155. 155 }
  156. 156 if src[((y + 1) * w + (x - 1)) * ch + c] >= center {
  157. 157● lbp_val = lbp_val | 64
  158. 158 }
  159. 159 if src[(y * w + (x - 1)) * ch + c] >= center {
  160. 160● lbp_val = lbp_val | 128
  161. 161 }
  162. 162● write_u16_le_l(out, (y * w + x) * bpp + c * 2, lbp_val)
  163. 163 }
  164. 164 }
  165. 165 for y in 0..<h {
  166. 166● let y_inner = y >= 1 && y < h - 1
  167. 167 for x in 0..<w {
  168. 168● if y_inner && x >= 1 && x < w - 1 {
  169. 169● continue
  170. 170 }
  171. 171 let center = src[(y * w + x) * ch + c]
  172. 172 let mut lbp_val = 0
  173. 173● if get_pixel_safe_16(src, x - 1, y - 1, c, ch, w, h) >= center {
  174. 174● lbp_val = lbp_val | 1
  175. 175 }
  176. 176● if get_pixel_safe_16(src, x, y - 1, c, ch, w, h) >= center {
  177. 177● lbp_val = lbp_val | 2
  178. 178 }
  179. 179● if get_pixel_safe_16(src, x + 1, y - 1, c, ch, w, h) >= center {
  180. 180● lbp_val = lbp_val | 4
  181. 181 }
  182. 182● if get_pixel_safe_16(src, x + 1, y, c, ch, w, h) >= center {
  183. 183● lbp_val = lbp_val | 8
  184. 184 }
  185. 185● if get_pixel_safe_16(src, x + 1, y + 1, c, ch, w, h) >= center {
  186. 186● lbp_val = lbp_val | 16
  187. 187 }
  188. 188● if get_pixel_safe_16(src, x, y + 1, c, ch, w, h) >= center {
  189. 189● lbp_val = lbp_val | 32
  190. 190 }
  191. 191● if get_pixel_safe_16(src, x - 1, y + 1, c, ch, w, h) >= center {
  192. 192● lbp_val = lbp_val | 64
  193. 193 }
  194. 194● if get_pixel_safe_16(src, x - 1, y, c, ch, w, h) >= center {
  195. 195● lbp_val = lbp_val | 128
  196. 196 }
  197. 197● write_u16_le_l(out, (y * w + x) * bpp + c * 2, lbp_val)
  198. 198 }
  199. 199 }
  200. 200 }
  201. 201 if ch == 4 {
  202. 202● for i in 0..<n {
  203. 203● let alpha_off = i * bpp + 6
  204. 204 out[alpha_off] = img.data[alpha_off]
  205. 205 out[alpha_off + 1] = img.data[alpha_off + 1]
  206. 206 }
  207. 207 }
  208. 208 @types.Image16::{
  209. 209 width: w,
  210. 210 height: h,
  211. 211 channels: ch,
  212. 212● data: Bytes::from_array(out),
  213. 213 }
  214. 214}
  215. 215
  216. 216///|
  217. 217/// float 基本局部二值模式(LBP)。输出归一化到 0.0-1.0(LBP值/255)。
  218. 218/// @see lbp 8-bit 版本,本函数为 ImageF 变体
  219. 219pub fn lbp_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  220. 220 // 输入验证
  221. 221● if img.width <= 0 || img.height <= 0 {
  222. 222● raise @types.LoadError::DecodeFailed("lbp_f: 图像尺寸必须大于0")
  223. 223 }
  224. 224 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  225. 225○ raise @types.LoadError::DecodeFailed("lbp_f: 仅支持 channels=1/3/4")
  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 "lbp_f: 数据大小不匹配,期望 " +
  231. 231● expected_size.to_string() +
  232. 232 ",实际 " +
  233. 233● img.data.length().to_string(),
  234. 234 )
  235. 235 }
  236. 236 let ch = img.channels
  237. 237◐ let color_ch = if ch >= 3 { 3 } else { 1 }
  238. 238 let w = img.width
  239. 239 let h = img.height
  240. 240 let n = w * h
  241. 241 let bpp = ch * 4
  242. 242● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  243. 243● let src : Array[Float] = Array::make(n * ch, 0.0F)
  244. 244 for i in 0..<(n * ch) {
  245. 245● src[i] = read_float_le_l(img.data, i * 4)
  246. 246 }
  247. 247 for c in 0..<color_ch {
  248. 248● for y in 1..<(h - 1) {
  249. 249● for x in 1..<(w - 1) {
  250. 250● let center = src[(y * w + x) * ch + c]
  251. 251 let mut lbp_val = 0
  252. 252 if src[((y - 1) * w + (x - 1)) * ch + c] >= center {
  253. 253● lbp_val = lbp_val | 1
  254. 254 }
  255. 255 if src[((y - 1) * w + x) * ch + c] >= center {
  256. 256● lbp_val = lbp_val | 2
  257. 257 }
  258. 258 if src[((y - 1) * w + (x + 1)) * ch + c] >= center {
  259. 259● lbp_val = lbp_val | 4
  260. 260 }
  261. 261 if src[(y * w + (x + 1)) * ch + c] >= center {
  262. 262● lbp_val = lbp_val | 8
  263. 263 }
  264. 264 if src[((y + 1) * w + (x + 1)) * ch + c] >= center {
  265. 265● lbp_val = lbp_val | 16
  266. 266 }
  267. 267 if src[((y + 1) * w + x) * ch + c] >= center {
  268. 268● lbp_val = lbp_val | 32
  269. 269 }
  270. 270 if src[((y + 1) * w + (x - 1)) * ch + c] >= center {
  271. 271● lbp_val = lbp_val | 64
  272. 272 }
  273. 273 if src[(y * w + (x - 1)) * ch + c] >= center {
  274. 274● lbp_val = lbp_val | 128
  275. 275 }
  276. 276● write_f32_le_l(
  277. 277 out,
  278. 278 (y * w + x) * bpp + c * 4,
  279. 279● Float::from_int(lbp_val) / 255.0F,
  280. 280 )
  281. 281 }
  282. 282 }
  283. 283 for y in 0..<h {
  284. 284● let y_inner = y >= 1 && y < h - 1
  285. 285 for x in 0..<w {
  286. 286● if y_inner && x >= 1 && x < w - 1 {
  287. 287● continue
  288. 288 }
  289. 289 let center = src[(y * w + x) * ch + c]
  290. 290 let mut lbp_val = 0
  291. 291● if get_pixel_safe_f(src, x - 1, y - 1, c, ch, w, h) >= center {
  292. 292● lbp_val = lbp_val | 1
  293. 293 }
  294. 294● if get_pixel_safe_f(src, x, y - 1, c, ch, w, h) >= center {
  295. 295● lbp_val = lbp_val | 2
  296. 296 }
  297. 297● if get_pixel_safe_f(src, x + 1, y - 1, c, ch, w, h) >= center {
  298. 298● lbp_val = lbp_val | 4
  299. 299 }
  300. 300● if get_pixel_safe_f(src, x + 1, y, c, ch, w, h) >= center {
  301. 301● lbp_val = lbp_val | 8
  302. 302 }
  303. 303● if get_pixel_safe_f(src, x + 1, y + 1, c, ch, w, h) >= center {
  304. 304● lbp_val = lbp_val | 16
  305. 305 }
  306. 306● if get_pixel_safe_f(src, x, y + 1, c, ch, w, h) >= center {
  307. 307● lbp_val = lbp_val | 32
  308. 308 }
  309. 309● if get_pixel_safe_f(src, x - 1, y + 1, c, ch, w, h) >= center {
  310. 310● lbp_val = lbp_val | 64
  311. 311 }
  312. 312● if get_pixel_safe_f(src, x - 1, y, c, ch, w, h) >= center {
  313. 313● lbp_val = lbp_val | 128
  314. 314 }
  315. 315● write_f32_le_l(
  316. 316 out,
  317. 317 (y * w + x) * bpp + c * 4,
  318. 318● Float::from_int(lbp_val) / 255.0F,
  319. 319 )
  320. 320 }
  321. 321 }
  322. 322 }
  323. 323 if ch == 4 {
  324. 324○ for i in 0..<n {
  325. 325○ let alpha_off = i * bpp + 12
  326. 326 for b in 0..<4 {
  327. 327○ out[alpha_off + b] = img.data[alpha_off + b]
  328. 328 }
  329. 329 }
  330. 330 }
  331. 331 @types.ImageF::{
  332. 332 width: w,
  333. 333 height: h,
  334. 334 channels: ch,
  335. 335● data: Bytes::from_array(out),
  336. 336 }
  337. 337}
  338. 338
  339. 339// ========== 均匀 LBP ==========
  340. 340
  341. 341///|
  342. 342/// 16-bit 均匀局部二值模式(Uniform LBP)。输出 0-9 的标签值,存储为 16-bit。
  343. 343/// @see lbp_uniform 8-bit 版本,本函数为 Image16 变体
  344. 344pub fn lbp_uniform_16(
  345. 345 img : @types.Image16,
  346. 346) -> @types.Image16 raise @types.LoadError {
  347. 347 // 输入验证
  348. 348● if img.width <= 0 || img.height <= 0 {
  349. 349● raise @types.LoadError::DecodeFailed(
  350. 350 "lbp_uniform_16: 图像尺寸必须大于0",
  351. 351 )
  352. 352 }
  353. 353 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  354. 354○ raise @types.LoadError::DecodeFailed(
  355. 355 "lbp_uniform_16: 仅支持 channels=1/3/4",
  356. 356 )
  357. 357 }
  358. 358 let expected_size = img.width * img.height * img.channels * 2
  359. 359● if img.data.length() != expected_size {
  360. 360● raise @types.LoadError::DecodeFailed(
  361. 361 "lbp_uniform_16: 数据大小不匹配,期望 " +
  362. 362● expected_size.to_string() +
  363. 363 ",实际 " +
  364. 364● img.data.length().to_string(),
  365. 365 )
  366. 366 }
  367. 367 let ch = img.channels
  368. 368◐ let color_ch = if ch >= 3 { 3 } else { 1 }
  369. 369 let w = img.width
  370. 370 let h = img.height
  371. 371 let n = w * h
  372. 372 let bpp = ch * 2
  373. 373● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  374. 374● let src : Array[Int32] = Array::make(n * ch, 0)
  375. 375 for i in 0..<(n * ch) {
  376. 376● src[i] = read_u16_le_l(img.data, i * 2)
  377. 377 }
  378. 378 for c in 0..<color_ch {
  379. 379● for y in 1..<(h - 1) {
  380. 380● for x in 1..<(w - 1) {
  381. 381● let center = src[(y * w + x) * ch + c]
  382. 382 let mut lbp_val = 0
  383. 383 if src[((y - 1) * w + (x - 1)) * ch + c] >= center {
  384. 384● lbp_val = lbp_val | 1
  385. 385 }
  386. 386 if src[((y - 1) * w + x) * ch + c] >= center {
  387. 387● lbp_val = lbp_val | 2
  388. 388 }
  389. 389 if src[((y - 1) * w + (x + 1)) * ch + c] >= center {
  390. 390● lbp_val = lbp_val | 4
  391. 391 }
  392. 392 if src[(y * w + (x + 1)) * ch + c] >= center {
  393. 393● lbp_val = lbp_val | 8
  394. 394 }
  395. 395 if src[((y + 1) * w + (x + 1)) * ch + c] >= center {
  396. 396● lbp_val = lbp_val | 16
  397. 397 }
  398. 398 if src[((y + 1) * w + x) * ch + c] >= center {
  399. 399● lbp_val = lbp_val | 32
  400. 400 }
  401. 401 if src[((y + 1) * w + (x - 1)) * ch + c] >= center {
  402. 402● lbp_val = lbp_val | 64
  403. 403 }
  404. 404 if src[(y * w + (x - 1)) * ch + c] >= center {
  405. 405● lbp_val = lbp_val | 128
  406. 406 }
  407. 407● write_u16_le_l(out, (y * w + x) * bpp + c * 2, uniform_lbp_map(lbp_val))
  408. 408 }
  409. 409 }
  410. 410 for y in 0..<h {
  411. 411● let y_inner = y >= 1 && y < h - 1
  412. 412 for x in 0..<w {
  413. 413● if y_inner && x >= 1 && x < w - 1 {
  414. 414● continue
  415. 415 }
  416. 416 let center = src[(y * w + x) * ch + c]
  417. 417 let mut lbp_val = 0
  418. 418● if get_pixel_safe_16(src, x - 1, y - 1, c, ch, w, h) >= center {
  419. 419● lbp_val = lbp_val | 1
  420. 420 }
  421. 421● if get_pixel_safe_16(src, x, y - 1, c, ch, w, h) >= center {
  422. 422● lbp_val = lbp_val | 2
  423. 423 }
  424. 424● if get_pixel_safe_16(src, x + 1, y - 1, c, ch, w, h) >= center {
  425. 425● lbp_val = lbp_val | 4
  426. 426 }
  427. 427● if get_pixel_safe_16(src, x + 1, y, c, ch, w, h) >= center {
  428. 428● lbp_val = lbp_val | 8
  429. 429 }
  430. 430● if get_pixel_safe_16(src, x + 1, y + 1, c, ch, w, h) >= center {
  431. 431● lbp_val = lbp_val | 16
  432. 432 }
  433. 433● if get_pixel_safe_16(src, x, y + 1, c, ch, w, h) >= center {
  434. 434● lbp_val = lbp_val | 32
  435. 435 }
  436. 436● if get_pixel_safe_16(src, x - 1, y + 1, c, ch, w, h) >= center {
  437. 437● lbp_val = lbp_val | 64
  438. 438 }
  439. 439● if get_pixel_safe_16(src, x - 1, y, c, ch, w, h) >= center {
  440. 440● lbp_val = lbp_val | 128
  441. 441 }
  442. 442● write_u16_le_l(out, (y * w + x) * bpp + c * 2, uniform_lbp_map(lbp_val))
  443. 443 }
  444. 444 }
  445. 445 }
  446. 446 if ch == 4 {
  447. 447○ for i in 0..<n {
  448. 448○ let alpha_off = i * bpp + 6
  449. 449 out[alpha_off] = img.data[alpha_off]
  450. 450 out[alpha_off + 1] = img.data[alpha_off + 1]
  451. 451 }
  452. 452 }
  453. 453 @types.Image16::{
  454. 454 width: w,
  455. 455 height: h,
  456. 456 channels: ch,
  457. 457● data: Bytes::from_array(out),
  458. 458 }
  459. 459}
  460. 460
  461. 461///|
  462. 462/// float 均匀局部二值模式(Uniform LBP)。输出归一化到 0.0-1.0(标签/9)。
  463. 463/// @see lbp_uniform 8-bit 版本,本函数为 ImageF 变体
  464. 464pub fn lbp_uniform_f(
  465. 465 img : @types.ImageF,
  466. 466) -> @types.ImageF raise @types.LoadError {
  467. 467 // 输入验证
  468. 468● if img.width <= 0 || img.height <= 0 {
  469. 469● raise @types.LoadError::DecodeFailed(
  470. 470 "lbp_uniform_f: 图像尺寸必须大于0",
  471. 471 )
  472. 472 }
  473. 473 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  474. 474○ raise @types.LoadError::DecodeFailed(
  475. 475 "lbp_uniform_f: 仅支持 channels=1/3/4",
  476. 476 )
  477. 477 }
  478. 478 let expected_size = img.width * img.height * img.channels * 4
  479. 479● if img.data.length() != expected_size {
  480. 480● raise @types.LoadError::DecodeFailed(
  481. 481 "lbp_uniform_f: 数据大小不匹配,期望 " +
  482. 482● expected_size.to_string() +
  483. 483 ",实际 " +
  484. 484● img.data.length().to_string(),
  485. 485 )
  486. 486 }
  487. 487 let ch = img.channels
  488. 488◐ let color_ch = if ch >= 3 { 3 } else { 1 }
  489. 489 let w = img.width
  490. 490 let h = img.height
  491. 491 let n = w * h
  492. 492 let bpp = ch * 4
  493. 493● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  494. 494● let src : Array[Float] = Array::make(n * ch, 0.0F)
  495. 495 for i in 0..<(n * ch) {
  496. 496● src[i] = read_float_le_l(img.data, i * 4)
  497. 497 }
  498. 498 for c in 0..<color_ch {
  499. 499● for y in 1..<(h - 1) {
  500. 500● for x in 1..<(w - 1) {
  501. 501● let center = src[(y * w + x) * ch + c]
  502. 502 let mut lbp_val = 0
  503. 503 if src[((y - 1) * w + (x - 1)) * ch + c] >= center {
  504. 504● lbp_val = lbp_val | 1
  505. 505 }
  506. 506 if src[((y - 1) * w + x) * ch + c] >= center {
  507. 507● lbp_val = lbp_val | 2
  508. 508 }
  509. 509 if src[((y - 1) * w + (x + 1)) * ch + c] >= center {
  510. 510● lbp_val = lbp_val | 4
  511. 511 }
  512. 512 if src[(y * w + (x + 1)) * ch + c] >= center {
  513. 513● lbp_val = lbp_val | 8
  514. 514 }
  515. 515 if src[((y + 1) * w + (x + 1)) * ch + c] >= center {
  516. 516● lbp_val = lbp_val | 16
  517. 517 }
  518. 518 if src[((y + 1) * w + x) * ch + c] >= center {
  519. 519● lbp_val = lbp_val | 32
  520. 520 }
  521. 521 if src[((y + 1) * w + (x - 1)) * ch + c] >= center {
  522. 522● lbp_val = lbp_val | 64
  523. 523 }
  524. 524 if src[(y * w + (x - 1)) * ch + c] >= center {
  525. 525● lbp_val = lbp_val | 128
  526. 526 }
  527. 527● let label = uniform_lbp_map(lbp_val)
  528. 528● write_f32_le_l(
  529. 529 out,
  530. 530 (y * w + x) * bpp + c * 4,
  531. 531● Float::from_int(label) / 9.0F,
  532. 532 )
  533. 533 }
  534. 534 }
  535. 535 for y in 0..<h {
  536. 536● let y_inner = y >= 1 && y < h - 1
  537. 537 for x in 0..<w {
  538. 538● if y_inner && x >= 1 && x < w - 1 {
  539. 539● continue
  540. 540 }
  541. 541 let center = src[(y * w + x) * ch + c]
  542. 542 let mut lbp_val = 0
  543. 543● if get_pixel_safe_f(src, x - 1, y - 1, c, ch, w, h) >= center {
  544. 544● lbp_val = lbp_val | 1
  545. 545 }
  546. 546● if get_pixel_safe_f(src, x, y - 1, c, ch, w, h) >= center {
  547. 547● lbp_val = lbp_val | 2
  548. 548 }
  549. 549● if get_pixel_safe_f(src, x + 1, y - 1, c, ch, w, h) >= center {
  550. 550● lbp_val = lbp_val | 4
  551. 551 }
  552. 552● if get_pixel_safe_f(src, x + 1, y, c, ch, w, h) >= center {
  553. 553● lbp_val = lbp_val | 8
  554. 554 }
  555. 555● if get_pixel_safe_f(src, x + 1, y + 1, c, ch, w, h) >= center {
  556. 556● lbp_val = lbp_val | 16
  557. 557 }
  558. 558● if get_pixel_safe_f(src, x, y + 1, c, ch, w, h) >= center {
  559. 559● lbp_val = lbp_val | 32
  560. 560 }
  561. 561● if get_pixel_safe_f(src, x - 1, y + 1, c, ch, w, h) >= center {
  562. 562● lbp_val = lbp_val | 64
  563. 563 }
  564. 564● if get_pixel_safe_f(src, x - 1, y, c, ch, w, h) >= center {
  565. 565● lbp_val = lbp_val | 128
  566. 566 }
  567. 567● let label = uniform_lbp_map(lbp_val)
  568. 568● write_f32_le_l(
  569. 569 out,
  570. 570 (y * w + x) * bpp + c * 4,
  571. 571● Float::from_int(label) / 9.0F,
  572. 572 )
  573. 573 }
  574. 574 }
  575. 575 }
  576. 576 if ch == 4 {
  577. 577○ for i in 0..<n {
  578. 578○ let alpha_off = i * bpp + 12
  579. 579 for b in 0..<4 {
  580. 580○ out[alpha_off + b] = img.data[alpha_off + b]
  581. 581 }
  582. 582 }
  583. 583 }
  584. 584 @types.ImageF::{
  585. 585 width: w,
  586. 586 height: h,
  587. 587 channels: ch,
  588. 588● data: Bytes::from_array(out),
  589. 589 }
  590. 590}