image.process\feature\lbp.mbt

90.8% · 99 / 109 points · 285 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/// # LBP 局部二值模式 lbp
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 局部二值模式(LBP,纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 基本局部二值模式(LBP)。对每个像素,与其 3x3 邻域的 8 个像素比较,
  10. 10/// 大于等于中心记 1,否则记 0,按顺时针顺序组成 8 位二进制数。
  11. 11/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4),alpha 通道不变。
  12. 12pub fn lbp(img : @types.Image) -> @types.Image raise @types.LoadError {
  13. 13 // 输入验证
  14. 14● if img.width <= 0 || img.height <= 0 {
  15. 15● raise @types.LoadError::DecodeFailed("lbp: 图像尺寸必须大于0")
  16. 16 }
  17. 17 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  18. 18● raise @types.LoadError::DecodeFailed("lbp: 仅支持 channels=1/3/4")
  19. 19 }
  20. 20 let expected_size = img.width * img.height * img.channels
  21. 21● if img.data.length() != expected_size {
  22. 22● raise @types.LoadError::DecodeFailed(
  23. 23 "lbp: 数据大小不匹配,期望 " +
  24. 24● expected_size.to_string() +
  25. 25 ",实际 " +
  26. 26● img.data.length().to_string(),
  27. 27 )
  28. 28 }
  29. 29 let ch = img.channels
  30. 30● let color_ch = if ch >= 3 { 3 } else { 1 }
  31. 31 let w = img.width
  32. 32 let h = img.height
  33. 33 let n = w * h
  34. 34● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  35. 35 // 预计算像素 Int 数组
  36. 36● let src : Array[Int32] = Array::make(n * ch, 0)
  37. 37 for i in 0..<(n * ch) {
  38. 38● src[i] = img.data[i].to_int()
  39. 39 }
  40. 40 for c in 0..<color_ch {
  41. 41 // 内部区域:x in [1, w-1), y in [1, h-1),直接访问 8 邻域
  42. 42● for y in 1..<(h - 1) {
  43. 43● for x in 1..<(w - 1) {
  44. 44● let center = src[(y * w + x) * ch + c]
  45. 45 let mut lbp_val = 0
  46. 46 // 左上、上、右上、右、右下、下、左下、左
  47. 47 if src[((y - 1) * w + (x - 1)) * ch + c] >= center {
  48. 48● lbp_val = lbp_val | 1
  49. 49 }
  50. 50 if src[((y - 1) * w + x) * ch + c] >= center {
  51. 51● lbp_val = lbp_val | 2
  52. 52 }
  53. 53 if src[((y - 1) * w + (x + 1)) * ch + c] >= center {
  54. 54● lbp_val = lbp_val | 4
  55. 55 }
  56. 56 if src[(y * w + (x + 1)) * ch + c] >= center {
  57. 57● lbp_val = lbp_val | 8
  58. 58 }
  59. 59 if src[((y + 1) * w + (x + 1)) * ch + c] >= center {
  60. 60● lbp_val = lbp_val | 16
  61. 61 }
  62. 62 if src[((y + 1) * w + x) * ch + c] >= center {
  63. 63● lbp_val = lbp_val | 32
  64. 64 }
  65. 65 if src[((y + 1) * w + (x - 1)) * ch + c] >= center {
  66. 66● lbp_val = lbp_val | 64
  67. 67 }
  68. 68 if src[(y * w + (x - 1)) * ch + c] >= center {
  69. 69● lbp_val = lbp_val | 128
  70. 70 }
  71. 71● out[(y * w + x) * ch + c] = lbp_val.to_byte()
  72. 72 }
  73. 73 }
  74. 74 // 边界区域:使用 get_pixel_safe
  75. 75 for y in 0..<h {
  76. 76● let y_inner = y >= 1 && y < h - 1
  77. 77 for x in 0..<w {
  78. 78● if y_inner && x >= 1 && x < w - 1 {
  79. 79● continue
  80. 80 }
  81. 81 let center = src[(y * w + x) * ch + c]
  82. 82 let mut lbp_val = 0
  83. 83● if get_pixel_safe(src, x - 1, y - 1, c, ch, w, h) >= center {
  84. 84● lbp_val = lbp_val | 1
  85. 85 }
  86. 86● if get_pixel_safe(src, x, y - 1, c, ch, w, h) >= center {
  87. 87● lbp_val = lbp_val | 2
  88. 88 }
  89. 89● if get_pixel_safe(src, x + 1, y - 1, c, ch, w, h) >= center {
  90. 90● lbp_val = lbp_val | 4
  91. 91 }
  92. 92● if get_pixel_safe(src, x + 1, y, c, ch, w, h) >= center {
  93. 93● lbp_val = lbp_val | 8
  94. 94 }
  95. 95● if get_pixel_safe(src, x + 1, y + 1, c, ch, w, h) >= center {
  96. 96● lbp_val = lbp_val | 16
  97. 97 }
  98. 98● if get_pixel_safe(src, x, y + 1, c, ch, w, h) >= center {
  99. 99● lbp_val = lbp_val | 32
  100. 100 }
  101. 101● if get_pixel_safe(src, x - 1, y + 1, c, ch, w, h) >= center {
  102. 102● lbp_val = lbp_val | 64
  103. 103 }
  104. 104● if get_pixel_safe(src, x - 1, y, c, ch, w, h) >= center {
  105. 105● lbp_val = lbp_val | 128
  106. 106 }
  107. 107● out[(y * w + x) * ch + c] = lbp_val.to_byte()
  108. 108 }
  109. 109 }
  110. 110 }
  111. 111 if ch == 4 {
  112. 112○ for i in 0..<n {
  113. 113○ out[i * 4 + 3] = img.data[i * 4 + 3]
  114. 114 }
  115. 115 }
  116. 116 @types.Image::{
  117. 117 width: w,
  118. 118 height: h,
  119. 119 channels: ch,
  120. 120● data: Bytes::from_array(out),
  121. 121 }
  122. 122}
  123. 123
  124. 124///|
  125. 125/// 均匀局部二值模式(Uniform LBP)。
  126. 126/// 均匀模式指 0→1 和 1→0 跳变次数 <= 2 的模式,共有 58 种均匀模式。
  127. 127/// 非均匀模式统一映射为 59。输出范围 0~59。
  128. 128pub fn lbp_uniform(img : @types.Image) -> @types.Image raise @types.LoadError {
  129. 129 // 输入验证
  130. 130● if img.width <= 0 || img.height <= 0 {
  131. 131● raise @types.LoadError::DecodeFailed(
  132. 132 "lbp_uniform: 图像尺寸必须大于0",
  133. 133 )
  134. 134 }
  135. 135 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  136. 136○ raise @types.LoadError::DecodeFailed(
  137. 137 "lbp_uniform: 仅支持 channels=1/3/4",
  138. 138 )
  139. 139 }
  140. 140 let expected_size = img.width * img.height * img.channels
  141. 141● if img.data.length() != expected_size {
  142. 142○ raise @types.LoadError::DecodeFailed(
  143. 143 "lbp_uniform: 数据大小不匹配,期望 " +
  144. 144○ expected_size.to_string() +
  145. 145 ",实际 " +
  146. 146○ img.data.length().to_string(),
  147. 147 )
  148. 148 }
  149. 149 let ch = img.channels
  150. 150◐ let color_ch = if ch >= 3 { 3 } else { 1 }
  151. 151 let w = img.width
  152. 152 let h = img.height
  153. 153 let n = w * h
  154. 154● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  155. 155 // 预计算均匀模式映射表
  156. 156● let uniform_map = build_uniform_map()
  157. 157 // 预计算像素 Int 数组
  158. 158● let src : Array[Int32] = Array::make(n * ch, 0)
  159. 159 for i in 0..<(n * ch) {
  160. 160● src[i] = img.data[i].to_int()
  161. 161 }
  162. 162 for c in 0..<color_ch {
  163. 163 // 内部区域
  164. 164● for y in 1..<(h - 1) {
  165. 165● for x in 1..<(w - 1) {
  166. 166● let center = src[(y * w + x) * ch + c]
  167. 167 let mut lbp_val = 0
  168. 168 if src[((y - 1) * w + (x - 1)) * ch + c] >= center {
  169. 169● lbp_val = lbp_val | 1
  170. 170 }
  171. 171 if src[((y - 1) * w + x) * ch + c] >= center {
  172. 172● lbp_val = lbp_val | 2
  173. 173 }
  174. 174 if src[((y - 1) * w + (x + 1)) * ch + c] >= center {
  175. 175● lbp_val = lbp_val | 4
  176. 176 }
  177. 177 if src[(y * w + (x + 1)) * ch + c] >= center {
  178. 178● lbp_val = lbp_val | 8
  179. 179 }
  180. 180 if src[((y + 1) * w + (x + 1)) * ch + c] >= center {
  181. 181● lbp_val = lbp_val | 16
  182. 182 }
  183. 183 if src[((y + 1) * w + x) * ch + c] >= center {
  184. 184● lbp_val = lbp_val | 32
  185. 185 }
  186. 186 if src[((y + 1) * w + (x - 1)) * ch + c] >= center {
  187. 187● lbp_val = lbp_val | 64
  188. 188 }
  189. 189 if src[(y * w + (x - 1)) * ch + c] >= center {
  190. 190● lbp_val = lbp_val | 128
  191. 191 }
  192. 192● out[(y * w + x) * ch + c] = uniform_map[lbp_val].to_byte()
  193. 193 }
  194. 194 }
  195. 195 // 边界区域
  196. 196 for y in 0..<h {
  197. 197● let y_inner = y >= 1 && y < h - 1
  198. 198 for x in 0..<w {
  199. 199● if y_inner && x >= 1 && x < w - 1 {
  200. 200● continue
  201. 201 }
  202. 202 let center = src[(y * w + x) * ch + c]
  203. 203 let mut lbp_val = 0
  204. 204● if get_pixel_safe(src, x - 1, y - 1, c, ch, w, h) >= center {
  205. 205● lbp_val = lbp_val | 1
  206. 206 }
  207. 207● if get_pixel_safe(src, x, y - 1, c, ch, w, h) >= center {
  208. 208● lbp_val = lbp_val | 2
  209. 209 }
  210. 210● if get_pixel_safe(src, x + 1, y - 1, c, ch, w, h) >= center {
  211. 211● lbp_val = lbp_val | 4
  212. 212 }
  213. 213● if get_pixel_safe(src, x + 1, y, c, ch, w, h) >= center {
  214. 214● lbp_val = lbp_val | 8
  215. 215 }
  216. 216● if get_pixel_safe(src, x + 1, y + 1, c, ch, w, h) >= center {
  217. 217● lbp_val = lbp_val | 16
  218. 218 }
  219. 219● if get_pixel_safe(src, x, y + 1, c, ch, w, h) >= center {
  220. 220● lbp_val = lbp_val | 32
  221. 221 }
  222. 222● if get_pixel_safe(src, x - 1, y + 1, c, ch, w, h) >= center {
  223. 223● lbp_val = lbp_val | 64
  224. 224 }
  225. 225● if get_pixel_safe(src, x - 1, y, c, ch, w, h) >= center {
  226. 226● lbp_val = lbp_val | 128
  227. 227 }
  228. 228● out[(y * w + x) * ch + c] = uniform_map[lbp_val].to_byte()
  229. 229 }
  230. 230 }
  231. 231 }
  232. 232 if ch == 4 {
  233. 233○ for i in 0..<n {
  234. 234○ out[i * 4 + 3] = img.data[i * 4 + 3]
  235. 235 }
  236. 236 }
  237. 237 @types.Image::{
  238. 238 width: w,
  239. 239 height: h,
  240. 240 channels: ch,
  241. 241● data: Bytes::from_array(out),
  242. 242 }
  243. 243}
  244. 244
  245. 245///|
  246. 246fn get_pixel_safe(
  247. 247 src : Array[Int32],
  248. 248 x : Int32,
  249. 249 y : Int32,
  250. 250 c : Int32,
  251. 251 ch : Int32,
  252. 252 w : Int32,
  253. 253 h : Int32,
  254. 254) -> Int32 {
  255. 255● if x < 0 || x >= w || y < 0 || y >= h {
  256. 256● return 0
  257. 257 }
  258. 258 src[(y * w + x) * ch + c]
  259. 259}
  260. 260
  261. 261///|
  262. 262fn build_uniform_map() -> Array[Int32] {
  263. 263● let map : Array[Int32] = Array::make(256, 59)
  264. 264 let mut label = 0
  265. 265 for v in 0..<256 {
  266. 266● if count_transitions(v) <= 2 {
  267. 267● map[v] = label
  268. 268 label += 1
  269. 269 }
  270. 270 }
  271. 271 map
  272. 272}
  273. 273
  274. 274///|
  275. 275fn count_transitions(v : Int32) -> Int32 {
  276. 276● let mut count = 0
  277. 277 for i in 0..<8 {
  278. 278● let bit_i = (v >> i) & 1
  279. 279 let bit_next = (v >> ((i + 1) % 8)) & 1
  280. 280 if bit_i != bit_next {
  281. 281● count += 1
  282. 282 }
  283. 283 }
  284. 284 count
  285. 285}