image.process\enhance\denoise.mbt

83% · 88 / 106 points · 353 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/// # 降噪 denoise
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像去噪(v12.1.0)
  7. 7// 非局部均值(NLM)+ 双边滤波增强 + 中值滤波增强
  8. 8
  9. 9///|
  10. 10/// 非局部均值去噪配置
  11. 11pub(all) struct NlmConfig {
  12. 12 search_window : Int32 // 搜索窗口大小
  13. 13 patch_size : Int32 // 补丁大小
  14. 14 h : Double // 滤波强度参数
  15. 15 sigma : Double // 噪声标准差
  16. 16}
  17. 17
  18. 18///|
  19. 19/// 创建默认 NLM 配置
  20. 20pub fn nlm_default_config() -> NlmConfig {
  21. 21● NlmConfig::{ search_window: 21, patch_size: 7, h: 10.0, sigma: 10.0, }
  22. 22}
  23. 23
  24. 24///|
  25. 25/// 非局部均值去噪(简化版)
  26. 26/// 基于图像块相似度的加权平均去噪
  27. 27pub fn denoise_nlm(
  28. 28 img : @types.Image,
  29. 29 config? : NlmConfig,
  30. 30) -> @types.Image raise @types.LoadError {
  31. 31 // 输入验证
  32. 32● if img.width <= 0 || img.height <= 0 {
  33. 33○ raise @types.LoadError::DecodeFailed(
  34. 34 "denoise_nlm: 图像尺寸必须大于0",
  35. 35 )
  36. 36 }
  37. 37 if img.channels < 1 || img.channels > 4 {
  38. 38● raise @types.LoadError::DecodeFailed(
  39. 39 "denoise_nlm: 通道数必须在1-4之间",
  40. 40 )
  41. 41 }
  42. 42 let expected_size = img.width * img.height * img.channels
  43. 43● if img.data.length() != expected_size {
  44. 44● raise @types.LoadError::DecodeFailed(
  45. 45 "denoise_nlm: 数据大小不匹配,期望 " +
  46. 46● expected_size.to_string() +
  47. 47 ",实际 " +
  48. 48● img.data.length().to_string(),
  49. 49 )
  50. 50 }
  51. 51
  52. 52● let cfg = match config {
  53. 53● Some(c) => c
  54. 54● None => nlm_default_config()
  55. 55 }
  56. 56
  57. 57 // 参数验证
  58. 58 if cfg.search_window <= 0 {
  59. 59○ raise @types.LoadError::DecodeFailed(
  60. 60 "denoise_nlm: search_window必须大于0",
  61. 61 )
  62. 62 }
  63. 63 if cfg.patch_size <= 0 {
  64. 64○ raise @types.LoadError::DecodeFailed("denoise_nlm: patch_size必须大于0")
  65. 65 }
  66. 66 if cfg.h <= 0.0 {
  67. 67● raise @types.LoadError::DecodeFailed("denoise_nlm: h必须大于0")
  68. 68 }
  69. 69
  70. 70 let w = img.width
  71. 71 let h = img.height
  72. 72 let channels = img.channels
  73. 73 let data = img.data
  74. 74● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  75. 75 let half_search = cfg.search_window / 2
  76. 76 let half_patch = cfg.patch_size / 2
  77. 77 let h2 = cfg.h * cfg.h
  78. 78 for y in 0..<h {
  79. 79● for x in 0..<w {
  80. 80● for c in 0..<channels {
  81. 81● let mut weighted_sum = 0.0
  82. 82 let mut weight_sum = 0.0
  83. 83 // 在搜索窗口内寻找相似块
  84. 84 for dy in -half_search..<(half_search + 1) {
  85. 85● for dx in -half_search..<(half_search + 1) {
  86. 86● let nx = x + dx
  87. 87 let ny = y + dy
  88. 88 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  89. 89 // 计算两个块的相似度(欧氏距离)
  90. 90● let mut dist = 0.0
  91. 91 let mut count = 0
  92. 92 for py in -half_patch..<(half_patch + 1) {
  93. 93● for px in -half_patch..<(half_patch + 1) {
  94. 94● let p1x = x + px
  95. 95 let p1y = y + py
  96. 96 let p2x = nx + px
  97. 97 let p2y = ny + py
  98. 98 if p1x >= 0 &&
  99. 99 p1x < w &&
  100. 100 p1y >= 0 &&
  101. 101 p1y < h &&
  102. 102 p2x >= 0 &&
  103. 103 p2x < w &&
  104. 104 p2y >= 0 &&
  105. 105 p2y < h {
  106. 106● let v1 = data[(p1y * w + p1x) * channels + c].to_double()
  107. 107● let v2 = data[(p2y * w + p2x) * channels + c].to_double()
  108. 108 let diff = v1 - v2
  109. 109 dist += diff * diff
  110. 110 count += 1
  111. 111 }
  112. 112 }
  113. 113 }
  114. 114 if count > 0 {
  115. 115● dist = dist / count.to_double()
  116. 116 // 高斯权重
  117. 117● let weight = @math.exp(-dist / h2)
  118. 118● weighted_sum += data[(ny * w + nx) * channels + c].to_double() *
  119. 119 weight
  120. 120 weight_sum += weight
  121. 121 }
  122. 122 }
  123. 123 }
  124. 124 }
  125. 125 if weight_sum > 0.0 {
  126. 126● out_data[(y * w + x) * channels + c] = (weighted_sum / weight_sum)
  127. 127● .min(255.0)
  128. 128● .max(0.0)
  129. 129● .to_int()
  130. 130● .to_byte()
  131. 131 } else {
  132. 132○ out_data[(y * w + x) * channels + c] = data[(y * w + x) * channels + c]
  133. 133 }
  134. 134 }
  135. 135 }
  136. 136 }
  137. 137 @types.Image::{
  138. 138 width: w,
  139. 139 height: h,
  140. 140 channels,
  141. 141● data: Bytes::from_array(out_data),
  142. 142 }
  143. 143}
  144. 144
  145. 145///|
  146. 146/// 双边滤波增强版
  147. 147/// 同时考虑空间距离和像素值差异
  148. 148pub fn denoise_bilateral(
  149. 149 img : @types.Image,
  150. 150 diameter~ : Int32 = 9,
  151. 151 sigma_color~ : Double = 75.0,
  152. 152 sigma_space~ : Double = 75.0,
  153. 153) -> @types.Image raise @types.LoadError {
  154. 154 // 输入验证
  155. 155● if img.width <= 0 || img.height <= 0 {
  156. 156○ raise @types.LoadError::DecodeFailed(
  157. 157 "denoise_bilateral: 图像尺寸必须大于0",
  158. 158 )
  159. 159 }
  160. 160 if img.channels < 1 || img.channels > 4 {
  161. 161● raise @types.LoadError::DecodeFailed(
  162. 162 "denoise_bilateral: 通道数必须在1-4之间",
  163. 163 )
  164. 164 }
  165. 165 let expected_size = img.width * img.height * img.channels
  166. 166● if img.data.length() != expected_size {
  167. 167● raise @types.LoadError::DecodeFailed(
  168. 168 "denoise_bilateral: 数据大小不匹配,期望 " +
  169. 169● expected_size.to_string() +
  170. 170 ",实际 " +
  171. 171● img.data.length().to_string(),
  172. 172 )
  173. 173 }
  174. 174 if diameter <= 0 {
  175. 175● raise @types.LoadError::DecodeFailed(
  176. 176 "denoise_bilateral: diameter必须大于0",
  177. 177 )
  178. 178 }
  179. 179 if sigma_color <= 0.0 {
  180. 180● raise @types.LoadError::DecodeFailed(
  181. 181 "denoise_bilateral: sigma_color必须大于0",
  182. 182 )
  183. 183 }
  184. 184 if sigma_space <= 0.0 {
  185. 185○ raise @types.LoadError::DecodeFailed(
  186. 186 "denoise_bilateral: sigma_space必须大于0",
  187. 187 )
  188. 188 }
  189. 189
  190. 190 let d = diameter
  191. 191 let sc = sigma_color
  192. 192 let ss = sigma_space
  193. 193 let w = img.width
  194. 194 let h = img.height
  195. 195 let channels = img.channels
  196. 196 let data = img.data
  197. 197● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  198. 198 let half = d / 2
  199. 199 for y in 0..<h {
  200. 200● for x in 0..<w {
  201. 201● for c in 0..<channels {
  202. 202● let center_val = data[(y * w + x) * channels + c].to_double()
  203. 203 let mut weighted_sum = 0.0
  204. 204 let mut weight_sum = 0.0
  205. 205 for dy in -half..<(half + 1) {
  206. 206● for dx in -half..<(half + 1) {
  207. 207● let nx = x + dx
  208. 208 let ny = y + dy
  209. 209 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  210. 210● let neighbor_val = data[(ny * w + nx) * channels + c].to_double()
  211. 211 // 空间距离权重
  212. 212● let spatial_dist = (dx * dx + dy * dy).to_double().sqrt()
  213. 213● let spatial_weight = @math.exp(
  214. 214 -(spatial_dist * spatial_dist) / (2.0 * ss * ss),
  215. 215 )
  216. 216 // 像素值差异权重
  217. 217● let color_diff = (center_val - neighbor_val).abs()
  218. 218● let color_weight = @math.exp(
  219. 219 -(color_diff * color_diff) / (2.0 * sc * sc),
  220. 220 )
  221. 221 // 组合权重
  222. 222 let weight = spatial_weight * color_weight
  223. 223 weighted_sum += neighbor_val * weight
  224. 224 weight_sum += weight
  225. 225 }
  226. 226 }
  227. 227 }
  228. 228 if weight_sum > 0.0 {
  229. 229● out_data[(y * w + x) * channels + c] = (weighted_sum / weight_sum)
  230. 230● .min(255.0)
  231. 231● .max(0.0)
  232. 232● .to_int()
  233. 233● .to_byte()
  234. 234 } else {
  235. 235○ out_data[(y * w + x) * channels + c] = data[(y * w + x) * channels + c]
  236. 236 }
  237. 237 }
  238. 238 }
  239. 239 }
  240. 240 @types.Image::{
  241. 241 width: w,
  242. 242 height: h,
  243. 243 channels,
  244. 244● data: Bytes::from_array(out_data),
  245. 245 }
  246. 246}
  247. 247
  248. 248///|
  249. 249/// 中值滤波增强版(支持任意核大小)
  250. 250pub fn denoise_median(
  251. 251 img : @types.Image,
  252. 252 kernel_size~ : Int32 = 3,
  253. 253) -> @types.Image raise @types.LoadError {
  254. 254 // 输入验证
  255. 255● if img.width <= 0 || img.height <= 0 {
  256. 256○ raise @types.LoadError::DecodeFailed(
  257. 257 "denoise_median: 图像尺寸必须大于0",
  258. 258 )
  259. 259 }
  260. 260 if img.channels < 1 || img.channels > 4 {
  261. 261● raise @types.LoadError::DecodeFailed(
  262. 262 "denoise_median: 通道数必须在1-4之间",
  263. 263 )
  264. 264 }
  265. 265 let expected_size = img.width * img.height * img.channels
  266. 266● if img.data.length() != expected_size {
  267. 267○ raise @types.LoadError::DecodeFailed(
  268. 268 "denoise_median: 数据大小不匹配,期望 " +
  269. 269○ expected_size.to_string() +
  270. 270 ",实际 " +
  271. 271○ img.data.length().to_string(),
  272. 272 )
  273. 273 }
  274. 274 if kernel_size <= 0 || kernel_size % 2 == 0 {
  275. 275● raise @types.LoadError::DecodeFailed(
  276. 276 "denoise_median: kernel_size必须是正奇数",
  277. 277 )
  278. 278 }
  279. 279
  280. 280 let ks = kernel_size
  281. 281 let w = img.width
  282. 282 let h = img.height
  283. 283 let channels = img.channels
  284. 284 let data = img.data
  285. 285● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  286. 286 let half = ks / 2
  287. 287 for y in 0..<h {
  288. 288● for x in 0..<w {
  289. 289● for c in 0..<channels {
  290. 290 // 收集邻域像素
  291. 291● let neighborhood : Array[Int32] = []
  292. 292 for dy in -half..<(half + 1) {
  293. 293● for dx in -half..<(half + 1) {
  294. 294● let nx = x + dx
  295. 295 let ny = y + dy
  296. 296 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  297. 297● neighborhood.push(data[(ny * w + nx) * channels + c].to_int())
  298. 298 }
  299. 299 }
  300. 300 }
  301. 301 // 排序取中值(选择排序)
  302. 302● let n = neighborhood.length()
  303. 303 for i in 0..<(n / 2 + 1) {
  304. 304● let mut min_idx = i
  305. 305 for j in (i + 1)..<n {
  306. 306● if neighborhood[j] < neighborhood[min_idx] {
  307. 307● min_idx = j
  308. 308 }
  309. 309 }
  310. 310 if min_idx != i {
  311. 311● let tmp = neighborhood[i]
  312. 312 neighborhood[i] = neighborhood[min_idx]
  313. 313 neighborhood[min_idx] = tmp
  314. 314 }
  315. 315 }
  316. 316 let median = if n > 0 {
  317. 317● neighborhood[n / 2]
  318. 318 } else {
  319. 319○ data[(y * w + x) * channels + c].to_int()
  320. 320 }
  321. 321● out_data[(y * w + x) * channels + c] = median.to_byte()
  322. 322 }
  323. 323 }
  324. 324 }
  325. 325 @types.Image::{
  326. 326 width: w,
  327. 327 height: h,
  328. 328 channels,
  329. 329● data: Bytes::from_array(out_data),
  330. 330 }
  331. 331}
  332. 332
  333. 333///|
  334. 334/// 通用去噪接口
  335. 335pub fn denoise(
  336. 336 img : @types.Image,
  337. 337 method_type : String,
  338. 338) -> @types.Image raise @types.LoadError {
  339. 339 // 输入验证
  340. 340● if img.width <= 0 || img.height <= 0 {
  341. 341○ raise @types.LoadError::DecodeFailed("denoise: 图像尺寸必须大于0")
  342. 342 }
  343. 343 if img.channels < 1 || img.channels > 4 {
  344. 344● raise @types.LoadError::DecodeFailed("denoise: 通道数必须在1-4之间")
  345. 345 }
  346. 346
  347. 347● match method_type {
  348. 348● "nlm" => denoise_nlm(img)
  349. 349○ "bilateral" => denoise_bilateral(img)
  350. 350○ "median" => denoise_median(img)
  351. 351○ _ => denoise_bilateral(img)
  352. 352 }
  353. 353}