image.process\transform\pyramid.mbt

96.5% · 83 / 86 points · 203 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/// # 图像金字塔 pyramid
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像金字塔(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 高斯金字塔下采样。将图像尺寸减半(宽高各 /2),使用 2x2 均值滤波。
  10. 10pub fn pyr_down(img : @types.Image) -> @types.Image raise @types.LoadError {
  11. 11● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  12. 12● raise @types.LoadError::DecodeFailed("pyr_down 仅支持 channels=1/3/4")
  13. 13 }
  14. 14 let ch = img.channels
  15. 15 let w = img.width
  16. 16 let h = img.height
  17. 17 let new_w = w / 2
  18. 18 let new_h = h / 2
  19. 19 if new_w == 0 || new_h == 0 {
  20. 20● raise @types.LoadError::DecodeFailed("图像太小,无法下采样")
  21. 21 }
  22. 22● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  23. 23 for i in 0..<(w * h * ch) {
  24. 24● data_int[i] = img.data[i].to_int()
  25. 25 }
  26. 26● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  27. 27 for y in 0..<new_h {
  28. 28● for x in 0..<new_w {
  29. 29● for c in 0..<ch {
  30. 30● let mut sum = 0
  31. 31 let mut count = 0
  32. 32 for dy in 0..<2 {
  33. 33● for dx in 0..<2 {
  34. 34● let sx = x * 2 + dx
  35. 35 let sy = y * 2 + dy
  36. 36 if sx < w && sy < h {
  37. 37● sum += data_int[(sy * w + sx) * ch + c]
  38. 38 count += 1
  39. 39 }
  40. 40 }
  41. 41 }
  42. 42● out[(y * new_w + x) * ch + c] = (sum / count).to_byte()
  43. 43 }
  44. 44 }
  45. 45 }
  46. 46 @types.Image::{
  47. 47 width: new_w,
  48. 48 height: new_h,
  49. 49 channels: ch,
  50. 50● data: Bytes::from_array(out),
  51. 51 }
  52. 52}
  53. 53
  54. 54///|
  55. 55/// 拉普拉斯金字塔上采样。将图像尺寸加倍(宽高各 *2),使用双线性插值。
  56. 56pub fn pyr_up(img : @types.Image) -> @types.Image raise @types.LoadError {
  57. 57● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  58. 58● raise @types.LoadError::DecodeFailed("pyr_up 仅支持 channels=1/3/4")
  59. 59 }
  60. 60 let ch = img.channels
  61. 61 let w = img.width
  62. 62 let h = img.height
  63. 63 let new_w = w * 2
  64. 64 let new_h = h * 2
  65. 65● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  66. 66 for i in 0..<(w * h * ch) {
  67. 67● data_int[i] = img.data[i].to_int()
  68. 68 }
  69. 69 // 预计算 x 方向插值参数
  70. 70● let x0_arr : Array[Int32] = Array::make(new_w, 0)
  71. 71● let x1_arr : Array[Int32] = Array::make(new_w, 0)
  72. 72● let wx_arr : Array[Float] = Array::make(new_w, 0.0F)
  73. 73 for x in 0..<new_w {
  74. 74● let fx = (Float::from_int(x) - 0.5F) / 2.0F
  75. 75● let x0 = fx.to_int().clamp(min=0, max=w - 1)
  76. 76 x0_arr[x] = x0
  77. 77● x1_arr[x] = (x0 + 1).clamp(min=0, max=w - 1)
  78. 78● wx_arr[x] = fx - Float::from_int(x0)
  79. 79 }
  80. 80 // 预计算 y 方向插值参数
  81. 81● let y0_arr : Array[Int32] = Array::make(new_h, 0)
  82. 82● let y1_arr : Array[Int32] = Array::make(new_h, 0)
  83. 83● let wy_arr : Array[Float] = Array::make(new_h, 0.0F)
  84. 84 for y in 0..<new_h {
  85. 85● let fy = (Float::from_int(y) - 0.5F) / 2.0F
  86. 86● let y0 = fy.to_int().clamp(min=0, max=h - 1)
  87. 87 y0_arr[y] = y0
  88. 88● y1_arr[y] = (y0 + 1).clamp(min=0, max=h - 1)
  89. 89● wy_arr[y] = fy - Float::from_int(y0)
  90. 90 }
  91. 91● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  92. 92 for y in 0..<new_h {
  93. 93● let y0 = y0_arr[y]
  94. 94 let y1 = y1_arr[y]
  95. 95 let wy = wy_arr[y]
  96. 96 let wy_inv = 1.0F - wy
  97. 97 let row0 = y0 * w
  98. 98 let row1 = y1 * w
  99. 99 for x in 0..<new_w {
  100. 100● let x0 = x0_arr[x]
  101. 101 let x1 = x1_arr[x]
  102. 102 let wx = wx_arr[x]
  103. 103 let wx_inv = 1.0F - wx
  104. 104 for c in 0..<ch {
  105. 105● let v00 = Float::from_int(data_int[(row0 + x0) * ch + c])
  106. 106● let v01 = Float::from_int(data_int[(row0 + x1) * ch + c])
  107. 107● let v10 = Float::from_int(data_int[(row1 + x0) * ch + c])
  108. 108● let v11 = Float::from_int(data_int[(row1 + x1) * ch + c])
  109. 109 let top = v00 * wx_inv + v01 * wx
  110. 110 let bot = v10 * wx_inv + v11 * wx
  111. 111 let v = top * wy_inv + bot * wy
  112. 112 let clamped = if v < 0.0F {
  113. 113● 0
  114. 114● } else if v > 255.0F {
  115. 115○ 255
  116. 116 } else {
  117. 117● v.to_int()
  118. 118 }
  119. 119● out[(y * new_w + x) * ch + c] = clamped.to_byte()
  120. 120 }
  121. 121 }
  122. 122 }
  123. 123 @types.Image::{
  124. 124 width: new_w,
  125. 125 height: new_h,
  126. 126 channels: ch,
  127. 127● data: Bytes::from_array(out),
  128. 128 }
  129. 129}
  130. 130
  131. 131///|
  132. 132/// 构建高斯金字塔。返回各级图像(从原图到最小级),levels 为最大层数(0=自动到 1x1)。
  133. 133pub fn build_gaussian_pyramid(
  134. 134 img : @types.Image,
  135. 135 levels : Int32,
  136. 136) -> Array[@types.Image] raise @types.LoadError {
  137. 137● let result : Array[@types.Image] = [img]
  138. 138 let mut current = img
  139. 139● let max_levels = if levels == 0 { 100 } else { levels }
  140. 140 for _ in 1..<max_levels {
  141. 141● if current.width < 2 || current.height < 2 {
  142. 142● break
  143. 143 }
  144. 144● current = pyr_down(current)
  145. 145● result.push(current)
  146. 146 }
  147. 147 result
  148. 148}
  149. 149
  150. 150///|
  151. 151/// 构建拉普拉斯金字塔。返回各级拉普拉斯图像(高斯金字塔相邻层之差)。
  152. 152/// 最后一层为高斯金字塔的最小级(不加差)。
  153. 153pub fn build_laplacian_pyramid(
  154. 154 img : @types.Image,
  155. 155 levels : Int32,
  156. 156) -> Array[@types.Image] raise @types.LoadError {
  157. 157● let gaussian = build_gaussian_pyramid(img, levels)
  158. 158 let result : Array[@types.Image] = []
  159. 159● for i in 0..<(gaussian.length() - 1) {
  160. 160● let upsampled = pyr_up(gaussian[i + 1])
  161. 161● let lap = laplacian_diff(gaussian[i], upsampled)
  162. 162● result.push(lap)
  163. 163 }
  164. 164● result.push(gaussian[gaussian.length() - 1])
  165. 165 result
  166. 166}
  167. 167
  168. 168///|
  169. 169fn laplacian_diff(a : @types.Image, b : @types.Image) -> @types.Image {
  170. 170● let ch = a.channels
  171. 171 let w = a.width
  172. 172 let h = a.height
  173. 173● let a_int : Array[Int32] = Array::make(w * h * ch, 0)
  174. 174 for i in 0..<(w * h * ch) {
  175. 175● a_int[i] = a.data[i].to_int()
  176. 176 }
  177. 177 let bw = b.width
  178. 178 let bh = b.height
  179. 179● let b_int : Array[Int32] = Array::make(bw * bh * ch, 0)
  180. 180 for i in 0..<(bw * bh * ch) {
  181. 181● b_int[i] = b.data[i].to_int()
  182. 182 }
  183. 183● let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
  184. 184 for y in 0..<h {
  185. 185● let by = y * bh / h
  186. 186 for x in 0..<w {
  187. 187● let bx = x * bw / w
  188. 188 for c in 0..<ch {
  189. 189● let va = a_int[(y * w + x) * ch + c]
  190. 190 let vb = b_int[(by * bw + bx) * ch + c]
  191. 191 let diff = va - vb + 128
  192. 192◐ let clamped = if diff < 0 { 0 } else if diff > 255 { 255 } else { diff }
  193. 193● out[(y * w + x) * ch + c] = clamped.to_byte()
  194. 194 }
  195. 195 }
  196. 196 }
  197. 197 @types.Image::{
  198. 198 width: w,
  199. 199 height: h,
  200. 200 channels: ch,
  201. 201● data: Bytes::from_array(out),
  202. 202 }
  203. 203}