image.process\frequency\haar_wavelet.mbt

95.2% · 79 / 83 points · 264 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/// # Haar 小波 haar wavelet
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// Haar 小波变换(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// Haar 小波变换结果
  10. 10pub struct HaarWaveletResult {
  11. 11 width : Int32
  12. 12 height : Int32
  13. 13 levels : Int32 // 分解层数
  14. 14 coefficients : Array[Float] // 小波系数
  15. 15}
  16. 16
  17. 17///|
  18. 18/// 一维 Haar 小波变换(原地)。
  19. 19/// 对长度为 n(2 的幂)的数组进行 Haar 变换。
  20. 20pub fn haar_transform_1d(data : Array[Float]) -> Array[Float] {
  21. 21● let n = data.length()
  22. 22 if n < 2 {
  23. 23● return data.copy()
  24. 24 }
  25. 25● let out = data.copy()
  26. 26● let sqrt2 = 2.0F.sqrt()
  27. 27 let mut len = n
  28. 28 while len > 1 {
  29. 29● let half = len / 2
  30. 30● let temp : Array[Float] = Array::make(len, 0.0F)
  31. 31 for i in 0..<half {
  32. 32● temp[i] = (out[2 * i] + out[2 * i + 1]) / sqrt2
  33. 33 temp[half + i] = (out[2 * i] - out[2 * i + 1]) / sqrt2
  34. 34 }
  35. 35 for i in 0..<len {
  36. 36● out[i] = temp[i]
  37. 37 }
  38. 38 len = half
  39. 39 }
  40. 40 out
  41. 41}
  42. 42
  43. 43///|
  44. 44/// 一维 Haar 小波逆变换。
  45. 45pub fn ihaar_transform_1d(data : Array[Float]) -> Array[Float] {
  46. 46● let n = data.length()
  47. 47 if n < 2 {
  48. 48● return data.copy()
  49. 49 }
  50. 50● let out = data.copy()
  51. 51● let sqrt2 = 2.0F.sqrt()
  52. 52 // 从最小级别开始逆变换
  53. 53 let mut len = 2
  54. 54 while len <= n {
  55. 55● let half = len / 2
  56. 56● let temp : Array[Float] = Array::make(len, 0.0F)
  57. 57 for i in 0..<half {
  58. 58● temp[2 * i] = (out[i] + out[half + i]) / sqrt2
  59. 59 temp[2 * i + 1] = (out[i] - out[half + i]) / sqrt2
  60. 60 }
  61. 61 for i in 0..<len {
  62. 62● out[i] = temp[i]
  63. 63 }
  64. 64 len *= 2
  65. 65 }
  66. 66 out
  67. 67}
  68. 68
  69. 69///|
  70. 70/// 二维 Haar 小波变换。
  71. 71/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。
  72. 72/// levels 为分解层数。
  73. 73pub fn haar_transform_2d(
  74. 74 img : @types.Image,
  75. 75 levels~ : Int32 = 1,
  76. 76) -> HaarWaveletResult raise @types.LoadError {
  77. 77● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  78. 78● raise @types.LoadError::DecodeFailed(
  79. 79 "haar_transform_2d 仅支持 channels=1/3/4",
  80. 80 )
  81. 81 }
  82. 82 if levels < 1 || levels > 10 {
  83. 83● raise @types.LoadError::DecodeFailed("levels 必须在 1..10 之间")
  84. 84 }
  85. 85 let w = img.width
  86. 86 let h = img.height
  87. 87 let n = w * h
  88. 88 if n == 0 {
  89. 89○ return HaarWaveletResult::{
  90. 90 width: w,
  91. 91 height: h,
  92. 92 levels: 0,
  93. 93 coefficients: [],
  94. 94 }
  95. 95 }
  96. 96 // 转灰度为 Float
  97. 97● let coeffs : Array[Float] = Array::make(n, 0.0F)
  98. 98 let ch = img.channels
  99. 99● let data_int : Array[Int32] = Array::make(n * ch, 0)
  100. 100 for i in 0..<(n * ch) {
  101. 101● data_int[i] = img.data[i].to_int()
  102. 102 }
  103. 103 for i in 0..<n {
  104. 104● let v = if ch == 1 {
  105. 105● data_int[i]
  106. 106 } else {
  107. 107● let r = data_int[i * ch]
  108. 108 let g = data_int[i * ch + 1]
  109. 109 let b = data_int[i * ch + 2]
  110. 110 (r * 299 + g * 587 + b * 114) / 1000
  111. 111 }
  112. 112● coeffs[i] = Float::from_int(v)
  113. 113 }
  114. 114 // 多级分解
  115. 115 let mut cur_w = w
  116. 116 let mut cur_h = h
  117. 117 for _ in 0..<levels {
  118. 118● if cur_w < 2 || cur_h < 2 {
  119. 119● break
  120. 120 }
  121. 121 // 行变换
  122. 122 for y in 0..<cur_h {
  123. 123● let row : Array[Float] = Array::make(cur_w, 0.0F)
  124. 124 for x in 0..<cur_w {
  125. 125● row[x] = coeffs[y * w + x]
  126. 126 }
  127. 127● let transformed = haar_transform_1d(row)
  128. 128 for x in 0..<cur_w {
  129. 129● coeffs[y * w + x] = transformed[x]
  130. 130 }
  131. 131 }
  132. 132 // 列变换
  133. 133 for x in 0..<cur_w {
  134. 134● let col : Array[Float] = Array::make(cur_h, 0.0F)
  135. 135 for y in 0..<cur_h {
  136. 136● col[y] = coeffs[y * w + x]
  137. 137 }
  138. 138● let transformed = haar_transform_1d(col)
  139. 139 for y in 0..<cur_h {
  140. 140● coeffs[y * w + x] = transformed[y]
  141. 141 }
  142. 142 }
  143. 143 cur_w /= 2
  144. 144 cur_h /= 2
  145. 145 }
  146. 146 HaarWaveletResult::{ width: w, height: h, levels, coefficients: coeffs, }
  147. 147}
  148. 148
  149. 149///|
  150. 150/// 二维 Haar 小波逆变换。
  151. 151pub fn haar_inverse_transform_2d(result : HaarWaveletResult) -> @types.Image {
  152. 152● let w = result.width
  153. 153 let h = result.height
  154. 154 let n = w * h
  155. 155 if n == 0 {
  156. 156○ return @types.Image::{
  157. 157 width: 0,
  158. 158 height: 0,
  159. 159 channels: 1,
  160. 160○ data: Bytes::from_array([]),
  161. 161 }
  162. 162 }
  163. 163● let coeffs = result.coefficients.copy()
  164. 164 // 多级重构(从最小级别开始)
  165. 165 let mut cur_w = w
  166. 166 let mut cur_h = h
  167. 167 for _ in 0..<result.levels {
  168. 168● if cur_w < 2 || cur_h < 2 {
  169. 169● break
  170. 170 }
  171. 171 cur_w /= 2
  172. 172 cur_h /= 2
  173. 173 }
  174. 174 for _ in 0..<result.levels {
  175. 175● if cur_w * 2 > w || cur_h * 2 > h {
  176. 176● break
  177. 177 }
  178. 178 cur_w *= 2
  179. 179 cur_h *= 2
  180. 180 // 列逆变换
  181. 181 for x in 0..<cur_w {
  182. 182● let col : Array[Float] = Array::make(cur_h, 0.0F)
  183. 183 for y in 0..<cur_h {
  184. 184● col[y] = coeffs[y * w + x]
  185. 185 }
  186. 186● let inversed = ihaar_transform_1d(col)
  187. 187 for y in 0..<cur_h {
  188. 188● coeffs[y * w + x] = inversed[y]
  189. 189 }
  190. 190 }
  191. 191 // 行逆变换
  192. 192 for y in 0..<cur_h {
  193. 193● let row : Array[Float] = Array::make(cur_w, 0.0F)
  194. 194 for x in 0..<cur_w {
  195. 195● row[x] = coeffs[y * w + x]
  196. 196 }
  197. 197● let inversed = ihaar_transform_1d(row)
  198. 198 for x in 0..<cur_w {
  199. 199● coeffs[y * w + x] = inversed[x]
  200. 200 }
  201. 201 }
  202. 202 }
  203. 203 // 转为图像
  204. 204● let out : Array[Byte] = Array::make(n, b'\x00')
  205. 205 for i in 0..<n {
  206. 206● let val = coeffs[i].to_int()
  207. 207● out[i] = val.clamp(min=0, max=255).to_byte()
  208. 208 }
  209. 209 @types.Image::{
  210. 210 width: w,
  211. 211 height: h,
  212. 212 channels: 1,
  213. 213● data: Bytes::from_array(out),
  214. 214 }
  215. 215}
  216. 216
  217. 217///|
  218. 218/// Haar 小波去噪(阈值去噪)。
  219. 219/// 对小波系数应用软阈值或硬阈值,然后逆变换重构。
  220. 220/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。
  221. 221/// threshold 为阈值,soft=true 为软阈值,false 为硬阈值。
  222. 222pub fn haar_denoise(
  223. 223 img : @types.Image,
  224. 224 threshold~ : Float = 10.0F,
  225. 225 soft~ : Bool = true,
  226. 226 levels~ : Int32 = 3,
  227. 227) -> @types.Image raise @types.LoadError {
  228. 228● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  229. 229● raise @types.LoadError::DecodeFailed(
  230. 230 "haar_denoise 仅支持 channels=1/3/4",
  231. 231 )
  232. 232 }
  233. 233● let result = haar_transform_2d(img, levels~)
  234. 234 let w = result.width
  235. 235 let h = result.height
  236. 236 let coeffs = result.coefficients
  237. 237 // 阈值处理(保留低频系数不变)
  238. 238 let low_w = w >> levels
  239. 239 let low_h = h >> levels
  240. 240 for y in 0..<h {
  241. 241● for x in 0..<w {
  242. 242 // 保留低频区域
  243. 243● if x < low_w && y < low_h {
  244. 244● continue
  245. 245 }
  246. 246 let idx = y * w + x
  247. 247 let c = coeffs[idx]
  248. 248 if soft {
  249. 249 // 软阈值
  250. 250● if c > threshold {
  251. 251● coeffs[idx] = c - threshold
  252. 252● } else if c < -threshold {
  253. 253○ coeffs[idx] = c + threshold
  254. 254 } else {
  255. 255● coeffs[idx] = 0.0F
  256. 256 }
  257. 257 // 硬阈值
  258. 258● } else if c.abs() < threshold {
  259. 259● coeffs[idx] = 0.0F
  260. 260 }
  261. 261 }
  262. 262 }
  263. 263● haar_inverse_transform_2d(result)
  264. 264}