image.process\filter\nlm_denoise.mbt

85.5% · 124 / 145 points · 416 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/// # 非局部均值降噪 nlm denoise
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 非局部均值去噪 NLM(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 非局部均值去噪(Non-Local Means)。
  10. 10/// 通过搜索窗口内相似像素块的加权平均来去噪。
  11. 11/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。
  12. 12/// patch_size 为相似性比较的块半径(默认 3,即 7x7 块)。
  13. 13/// search_size 为搜索窗口半径(默认 7,即 15x15 窗口)。
  14. 14/// h 为滤波强度参数(默认 10),越大平滑越强。
  15. 15pub fn nlm_denoise(
  16. 16 img : @types.Image,
  17. 17 patch_size~ : Int32 = 3,
  18. 18 search_size~ : Int32 = 7,
  19. 19 h~ : Int32 = 10,
  20. 20) -> @types.Image raise @types.LoadError {
  21. 21 // 输入验证
  22. 22● if img.width <= 0 || img.height <= 0 {
  23. 23● raise @types.LoadError::DecodeFailed(
  24. 24 "nlm_denoise: 图像尺寸必须大于0",
  25. 25 )
  26. 26 }
  27. 27 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  28. 28● raise @types.LoadError::DecodeFailed(
  29. 29 "nlm_denoise: 仅支持 channels=1/3/4",
  30. 30 )
  31. 31 }
  32. 32 let expected_size = img.width * img.height * img.channels
  33. 33● if img.data.length() != expected_size {
  34. 34○ raise @types.LoadError::DecodeFailed(
  35. 35 "nlm_denoise: 数据大小不匹配,期望 " +
  36. 36○ expected_size.to_string() +
  37. 37 ",实际 " +
  38. 38○ img.data.length().to_string(),
  39. 39 )
  40. 40 }
  41. 41 if patch_size < 1 || patch_size > 10 {
  42. 42● raise @types.LoadError::DecodeFailed("patch_size 必须在 1..10 之间")
  43. 43 }
  44. 44 if search_size < 1 || search_size > 20 {
  45. 45● raise @types.LoadError::DecodeFailed("search_size 必须在 1..20 之间")
  46. 46 }
  47. 47 if h < 1 {
  48. 48● raise @types.LoadError::DecodeFailed("h 必须 >= 1")
  49. 49 }
  50. 50 let ch = img.channels
  51. 51 let w = img.width
  52. 52 let h_img = img.height
  53. 53 let n = w * h_img
  54. 54 if n == 0 {
  55. 55○ return img
  56. 56 }
  57. 57● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  58. 58 let h_sq = h * h
  59. 59 let patch_d = patch_size
  60. 60 let search_d = search_size
  61. 61 // 预计算常数
  62. 62 let patch_area = (2 * patch_d + 1) * (2 * patch_d + 1)
  63. 63 // 预计算像素 Int 数组,避免重复 .to_int() 转换
  64. 64● let pixels : Array[Int32] = Array::make(n * ch, 0)
  65. 65 for i in 0..<(n * ch) {
  66. 66● pixels[i] = img.data[i].to_int()
  67. 67 }
  68. 68● let denom = Float::from_int(h_sq * patch_area)
  69. 69● let early_term = (denom * 6.907755278982137F).to_int()
  70. 70 let inner_start = search_d + patch_d
  71. 71 // 内部区域:搜索窗口和块都完全在图像内
  72. 72 for y in inner_start..<(h_img - inner_start) {
  73. 73● for x in inner_start..<(w - inner_start) {
  74. 74● for c in 0..<ch {
  75. 75● if ch == 4 && c == 3 {
  76. 76● out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3]
  77. 77 continue
  78. 78 }
  79. 79 let mut weight_sum = 0.0F
  80. 80 let mut value_sum = 0.0F
  81. 81 for sy in (y - search_d)..<=(y + search_d) {
  82. 82● let row_sy = sy * w
  83. 83 for sx in (x - search_d)..<=(x + search_d) {
  84. 84● let dist = patch_distance_inner(
  85. 85 pixels, x, y, sx, sy, w, ch, c, patch_d, early_term,
  86. 86 )
  87. 87 if dist >= early_term {
  88. 88○ continue
  89. 89 }
  90. 90● let weight = @math.expf(Float::from_int(-dist) / denom)
  91. 91 weight_sum += weight
  92. 92 value_sum += weight *
  93. 93● Float::from_int(pixels[(row_sy + sx) * ch + c])
  94. 94 }
  95. 95 }
  96. 96 let val = if weight_sum > 0.0F {
  97. 97● (value_sum / weight_sum).to_int()
  98. 98 } else {
  99. 99○ pixels[(y * w + x) * ch + c]
  100. 100 }
  101. 101● out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte()
  102. 102 }
  103. 103 }
  104. 104 }
  105. 105 // 边界区域
  106. 106 for y in 0..<h_img {
  107. 107● let y_inner = y >= inner_start && y < h_img - inner_start
  108. 108 for x in 0..<w {
  109. 109● if y_inner && x >= inner_start && x < w - inner_start {
  110. 110● continue
  111. 111 }
  112. 112 for c in 0..<ch {
  113. 113● if ch == 4 && c == 3 {
  114. 114● out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3]
  115. 115 continue
  116. 116 }
  117. 117 let mut weight_sum = 0.0F
  118. 118 let mut value_sum = 0.0F
  119. 119● let sy_start = if y - search_d < 0 { 0 } else { y - search_d }
  120. 120 let sy_end = if y + search_d > h_img - 1 {
  121. 121● h_img - 1
  122. 122 } else {
  123. 123● y + search_d
  124. 124 }
  125. 125● let sx_start = if x - search_d < 0 { 0 } else { x - search_d }
  126. 126● let sx_end = if x + search_d > w - 1 { w - 1 } else { x + search_d }
  127. 127 for sy in sy_start..<=sy_end {
  128. 128● for sx in sx_start..<=sx_end {
  129. 129● let dist = patch_distance_fast(
  130. 130 pixels, x, y, sx, sy, w, h_img, ch, c, patch_d, early_term,
  131. 131 )
  132. 132 if dist >= early_term {
  133. 133● continue
  134. 134 }
  135. 135● let weight = @math.expf(Float::from_int(-dist) / denom)
  136. 136 weight_sum += weight
  137. 137 value_sum += weight *
  138. 138● Float::from_int(pixels[(sy * w + sx) * ch + c])
  139. 139 }
  140. 140 }
  141. 141 let val = if weight_sum > 0.0F {
  142. 142● (value_sum / weight_sum).to_int()
  143. 143 } else {
  144. 144○ pixels[(y * w + x) * ch + c]
  145. 145 }
  146. 146● out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte()
  147. 147 }
  148. 148 }
  149. 149 }
  150. 150 @types.Image::{
  151. 151 width: w,
  152. 152 height: h_img,
  153. 153 channels: ch,
  154. 154● data: Bytes::from_array(out),
  155. 155 }
  156. 156}
  157. 157
  158. 158///|
  159. 159/// 计算两个像素块之间的平方距离(内部区域,无边界检查)
  160. 160fn patch_distance_inner(
  161. 161 pixels : Array[Int32],
  162. 162 x1 : Int32,
  163. 163 y1 : Int32,
  164. 164 x2 : Int32,
  165. 165 y2 : Int32,
  166. 166 w : Int32,
  167. 167 ch : Int32,
  168. 168 c : Int32,
  169. 169 patch_d : Int32,
  170. 170 early_term : Int32,
  171. 171) -> Int32 {
  172. 172● let mut dist = 0
  173. 173 for dy in -patch_d..<=patch_d {
  174. 174● let row1 = (y1 + dy) * w
  175. 175 let row2 = (y2 + dy) * w
  176. 176 for dx in -patch_d..<=patch_d {
  177. 177● let v1 = pixels[(row1 + x1 + dx) * ch + c]
  178. 178 let v2 = pixels[(row2 + x2 + dx) * ch + c]
  179. 179 let diff = v1 - v2
  180. 180 dist += diff * diff
  181. 181 if dist >= early_term {
  182. 182○ return dist
  183. 183 }
  184. 184 }
  185. 185 }
  186. 186 dist
  187. 187}
  188. 188
  189. 189///|
  190. 190/// 计算两个像素块之间的平方距离(使用预计算 Int 数组,内联边界检查)
  191. 191fn patch_distance_fast(
  192. 192 pixels : Array[Int32],
  193. 193 x1 : Int32,
  194. 194 y1 : Int32,
  195. 195 x2 : Int32,
  196. 196 y2 : Int32,
  197. 197 w : Int32,
  198. 198 h : Int32,
  199. 199 ch : Int32,
  200. 200 c : Int32,
  201. 201 patch_d : Int32,
  202. 202 early_term : Int32,
  203. 203) -> Int32 {
  204. 204● let mut dist = 0
  205. 205 let w_max = w - 1
  206. 206 let h_max = h - 1
  207. 207 for dy in -patch_d..<=patch_d {
  208. 208● for dx in -patch_d..<=patch_d {
  209. 209 // 内联边界检查,避免 clamp 方法调用
  210. 210● let raw1x = x1 + dx
  211. 211 let raw1y = y1 + dy
  212. 212 let raw2x = x2 + dx
  213. 213 let raw2y = y2 + dy
  214. 214 let px1 = if raw1x < 0 {
  215. 215● 0
  216. 216● } else if raw1x > w_max {
  217. 217● w_max
  218. 218 } else {
  219. 219● raw1x
  220. 220 }
  221. 221 let py1 = if raw1y < 0 {
  222. 222● 0
  223. 223● } else if raw1y > h_max {
  224. 224● h_max
  225. 225 } else {
  226. 226● raw1y
  227. 227 }
  228. 228 let px2 = if raw2x < 0 {
  229. 229● 0
  230. 230● } else if raw2x > w_max {
  231. 231● w_max
  232. 232 } else {
  233. 233● raw2x
  234. 234 }
  235. 235 let py2 = if raw2y < 0 {
  236. 236● 0
  237. 237● } else if raw2y > h_max {
  238. 238● h_max
  239. 239 } else {
  240. 240● raw2y
  241. 241 }
  242. 242 let v1 = pixels[(py1 * w + px1) * ch + c]
  243. 243 let v2 = pixels[(py2 * w + px2) * ch + c]
  244. 244 let diff = v1 - v2
  245. 245 dist += diff * diff
  246. 246 if dist >= early_term {
  247. 247● return dist
  248. 248 }
  249. 249 }
  250. 250 }
  251. 251 dist
  252. 252}
  253. 253
  254. 254///|
  255. 255
  256. 256///|
  257. 257/// 快速 NLM 去噪(降采样搜索)。
  258. 258/// 使用积分图加速块距离计算,搜索步长 > 1 以加速。
  259. 259/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。
  260. 260pub fn nlm_denoise_fast(
  261. 261 img : @types.Image,
  262. 262 patch_size~ : Int32 = 3,
  263. 263 search_size~ : Int32 = 7,
  264. 264 h~ : Int32 = 10,
  265. 265 step~ : Int32 = 2,
  266. 266) -> @types.Image raise @types.LoadError {
  267. 267 // 输入验证
  268. 268● if img.width <= 0 || img.height <= 0 {
  269. 269● raise @types.LoadError::DecodeFailed(
  270. 270 "nlm_denoise_fast: 图像尺寸必须大于0",
  271. 271 )
  272. 272 }
  273. 273 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  274. 274● raise @types.LoadError::DecodeFailed(
  275. 275 "nlm_denoise_fast: 仅支持 channels=1/3/4",
  276. 276 )
  277. 277 }
  278. 278 let expected_size = img.width * img.height * img.channels
  279. 279● if img.data.length() != expected_size {
  280. 280○ raise @types.LoadError::DecodeFailed(
  281. 281 "nlm_denoise_fast: 数据大小不匹配,期望 " +
  282. 282○ expected_size.to_string() +
  283. 283 ",实际 " +
  284. 284○ img.data.length().to_string(),
  285. 285 )
  286. 286 }
  287. 287 if patch_size < 1 || patch_size > 10 {
  288. 288○ raise @types.LoadError::DecodeFailed("patch_size 必须在 1..10 之间")
  289. 289 }
  290. 290 if search_size < 1 || search_size > 20 {
  291. 291○ raise @types.LoadError::DecodeFailed("search_size 必须在 1..20 之间")
  292. 292 }
  293. 293 if h < 1 {
  294. 294○ raise @types.LoadError::DecodeFailed("h 必须 >= 1")
  295. 295 }
  296. 296 if step < 1 || step > 5 {
  297. 297● raise @types.LoadError::DecodeFailed("step 必须在 1..5 之间")
  298. 298 }
  299. 299 let ch = img.channels
  300. 300 let w = img.width
  301. 301 let h_img = img.height
  302. 302 let n = w * h_img
  303. 303 if n == 0 {
  304. 304○ return img
  305. 305 }
  306. 306● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  307. 307 let h_sq = h * h
  308. 308 let patch_d = patch_size
  309. 309 let search_d = search_size
  310. 310 let patch_area = (2 * patch_d + 1) * (2 * patch_d + 1)
  311. 311 // 预计算像素 Int 数组
  312. 312● let pixels : Array[Int32] = Array::make(n * ch, 0)
  313. 313 for i in 0..<(n * ch) {
  314. 314● pixels[i] = img.data[i].to_int()
  315. 315 }
  316. 316● let denom = Float::from_int(h_sq * patch_area)
  317. 317● let early_term = (denom * 6.907755278982137F).to_int()
  318. 318 let inner_start = search_d + patch_d
  319. 319 // 内部区域:搜索窗口和块都完全在图像内
  320. 320 for y in inner_start..<(h_img - inner_start) {
  321. 321● for x in inner_start..<(w - inner_start) {
  322. 322● for c in 0..<ch {
  323. 323● if ch == 4 && c == 3 {
  324. 324○ out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3]
  325. 325 continue
  326. 326 }
  327. 327 let mut weight_sum = 0.0F
  328. 328 let mut value_sum = 0.0F
  329. 329 let mut sy = y - search_d
  330. 330 let sy_end = y + search_d
  331. 331 while sy <= sy_end {
  332. 332● let row_sy = sy * w
  333. 333 let mut sx = x - search_d
  334. 334 let sx_end = x + search_d
  335. 335 while sx <= sx_end {
  336. 336● let dist = patch_distance_inner(
  337. 337 pixels, x, y, sx, sy, w, ch, c, patch_d, early_term,
  338. 338 )
  339. 339 if dist >= early_term {
  340. 340○ sx += step
  341. 341 continue
  342. 342 }
  343. 343● let weight = @math.expf(Float::from_int(-dist) / denom)
  344. 344 weight_sum += weight
  345. 345 value_sum += weight *
  346. 346● Float::from_int(pixels[(row_sy + sx) * ch + c])
  347. 347 sx += step
  348. 348 }
  349. 349 sy += step
  350. 350 }
  351. 351 let val = if weight_sum > 0.0F {
  352. 352● (value_sum / weight_sum).to_int()
  353. 353 } else {
  354. 354○ pixels[(y * w + x) * ch + c]
  355. 355 }
  356. 356● out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte()
  357. 357 }
  358. 358 }
  359. 359 }
  360. 360 // 边界区域
  361. 361 for y in 0..<h_img {
  362. 362● let y_inner = y >= inner_start && y < h_img - inner_start
  363. 363 for x in 0..<w {
  364. 364● if y_inner && x >= inner_start && x < w - inner_start {
  365. 365● continue
  366. 366 }
  367. 367 for c in 0..<ch {
  368. 368● if ch == 4 && c == 3 {
  369. 369○ out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3]
  370. 370 continue
  371. 371 }
  372. 372 let mut weight_sum = 0.0F
  373. 373 let mut value_sum = 0.0F
  374. 374● let sy_start = if y - search_d < 0 { 0 } else { y - search_d }
  375. 375 let sy_end = if y + search_d > h_img - 1 {
  376. 376● h_img - 1
  377. 377 } else {
  378. 378● y + search_d
  379. 379 }
  380. 380● let sx_start = if x - search_d < 0 { 0 } else { x - search_d }
  381. 381● let sx_end = if x + search_d > w - 1 { w - 1 } else { x + search_d }
  382. 382 let mut sy = sy_start
  383. 383 while sy <= sy_end {
  384. 384● let mut sx = sx_start
  385. 385 while sx <= sx_end {
  386. 386● let dist = patch_distance_fast(
  387. 387 pixels, x, y, sx, sy, w, h_img, ch, c, patch_d, early_term,
  388. 388 )
  389. 389 if dist >= early_term {
  390. 390● sx += step
  391. 391 continue
  392. 392 }
  393. 393● let weight = @math.expf(Float::from_int(-dist) / denom)
  394. 394 weight_sum += weight
  395. 395 value_sum += weight *
  396. 396● Float::from_int(pixels[(sy * w + sx) * ch + c])
  397. 397 sx += step
  398. 398 }
  399. 399 sy += step
  400. 400 }
  401. 401 let val = if weight_sum > 0.0F {
  402. 402● (value_sum / weight_sum).to_int()
  403. 403 } else {
  404. 404● pixels[(y * w + x) * ch + c]
  405. 405 }
  406. 406● out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte()
  407. 407 }
  408. 408 }
  409. 409 }
  410. 410 @types.Image::{
  411. 411 width: w,
  412. 412 height: h_img,
  413. 413 channels: ch,
  414. 414● data: Bytes::from_array(out),
  415. 415 }
  416. 416}