image.process\filter\inpaint.mbt

72.8% · 59 / 81 points · 252 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/// # 图像修复 inpaint
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像修复(纯 MoonBit)
  7. 7// 基于扩散法:对掩码区域求解 Laplace 方程
  8. 8// 已知像素作为 Dirichlet 边界条件,迭代至收敛
  9. 9// 参考: Bertalmio et al., "Image Inpainting" (2000)
  10. 10
  11. 11///|
  12. 12/// 图像修复。
  13. 13/// mask 为单通道掩码图像:0=已知像素,非0=待修复像素。
  14. 14/// iterations 控制最大迭代次数(默认100)。
  15. 15/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。
  16. 16pub fn inpaint(
  17. 17 img : @types.Image,
  18. 18 mask : @types.Image,
  19. 19 iterations~ : Int32 = 100,
  20. 20) -> @types.Image raise @types.LoadError {
  21. 21 // 输入验证
  22. 22● if img.width <= 0 || img.height <= 0 {
  23. 23● raise @types.LoadError::DecodeFailed("inpaint: 图像尺寸必须大于0")
  24. 24 }
  25. 25 if img.channels < 1 || img.channels > 4 {
  26. 26○ raise @types.LoadError::DecodeFailed("inpaint: 通道数必须在1-4之间")
  27. 27 }
  28. 28 let expected_size = img.width * img.height * img.channels
  29. 29● if img.data.length() != expected_size {
  30. 30○ raise @types.LoadError::DecodeFailed(
  31. 31 "inpaint: 图像数据大小不匹配,期望 " +
  32. 32○ expected_size.to_string() +
  33. 33 ",实际 " +
  34. 34○ img.data.length().to_string(),
  35. 35 )
  36. 36 }
  37. 37 if img.width != mask.width || img.height != mask.height {
  38. 38● raise @types.LoadError::DecodeFailed(
  39. 39 "inpaint: 掩码尺寸必须与图像一致",
  40. 40 )
  41. 41 }
  42. 42 if mask.channels != 1 {
  43. 43● raise @types.LoadError::DecodeFailed(
  44. 44 "inpaint: 掩码必须为单通道图像",
  45. 45 )
  46. 46 }
  47. 47 let mask_expected_size = mask.width * mask.height
  48. 48● if mask.data.length() != mask_expected_size {
  49. 49○ raise @types.LoadError::DecodeFailed(
  50. 50 "inpaint: 掩码数据大小不匹配,期望 " +
  51. 51○ mask_expected_size.to_string() +
  52. 52 ",实际 " +
  53. 53○ mask.data.length().to_string(),
  54. 54 )
  55. 55 }
  56. 56 if iterations <= 0 {
  57. 57○ raise @types.LoadError::DecodeFailed("inpaint: iterations必须大于0")
  58. 58 }
  59. 59 let w = img.width
  60. 60 let h = img.height
  61. 61 let ch = img.channels
  62. 62 let n = w * h
  63. 63 // 构建待修复标记
  64. 64● let unknown : Array[Bool] = Array::make(n, false)
  65. 65 let mut has_unknown = false
  66. 66 for i in 0..<n {
  67. 67● if mask.data[i] != b'\x00' {
  68. 68● unknown[i] = true
  69. 69 has_unknown = true
  70. 70 }
  71. 71 }
  72. 72 // 若无待修复像素,直接返回原图
  73. 73 if !has_unknown {
  74. 74● return img
  75. 75 }
  76. 76 // 复制原图数据
  77. 77● let out : Array[Int32] = Array::make(n * ch, 0)
  78. 78 for i in 0..<(n * ch) {
  79. 79● out[i] = img.data[i].to_int()
  80. 80 }
  81. 81 // Jacobi 迭代求解 Laplace 方程
  82. 82 for _ in 0..<iterations {
  83. 83● let prev = out.copy()
  84. 84 let mut changed = false
  85. 85 for y in 0..<h {
  86. 86● for x in 0..<w {
  87. 87● let idx = y * w + x
  88. 88 if !unknown[idx] {
  89. 89● continue
  90. 90 }
  91. 91 // 收集 8 邻域中已知/已更新像素
  92. 92 for c in 0..<ch {
  93. 93● let mut sum = 0
  94. 94 let mut count = 0
  95. 95 for dy in -1..<=1 {
  96. 96● let ny = y + dy
  97. 97 if ny < 0 || ny >= h {
  98. 98● continue
  99. 99 }
  100. 100 for dx in -1..<=1 {
  101. 101● if dx == 0 && dy == 0 {
  102. 102● continue
  103. 103 }
  104. 104 let nx = x + dx
  105. 105 if nx < 0 || nx >= w {
  106. 106○ continue
  107. 107 }
  108. 108 sum += prev[(ny * w + nx) * ch + c]
  109. 109 count += 1
  110. 110 }
  111. 111 }
  112. 112 if count > 0 {
  113. 113● let new_val = sum / count
  114. 114 if new_val != out[idx * ch + c] {
  115. 115● changed = true
  116. 116 }
  117. 117 out[idx * ch + c] = new_val
  118. 118 }
  119. 119 }
  120. 120 }
  121. 121 }
  122. 122 // 收敛检测
  123. 123 if !changed {
  124. 124● break
  125. 125 }
  126. 126 }
  127. 127 // 转换输出
  128. 128● let out_data : Array[Byte] = Array::make(n * ch, b'\x00')
  129. 129 for i in 0..<(n * ch) {
  130. 130● out_data[i] = out[i].clamp(min=0, max=255).to_byte()
  131. 131 }
  132. 132 @types.Image::{
  133. 133 width: w,
  134. 134 height: h,
  135. 135 channels: ch,
  136. 136● data: Bytes::from_array(out_data),
  137. 137 }
  138. 138}
  139. 139
  140. 140///|
  141. 141/// 快速图像修复(基于距离加权平均)。
  142. 142/// 对每个待修复像素,取周围已知像素的加权平均(距离越近权重越大)。
  143. 143/// 比 inpaint 快但质量略低,适合小面积修复。
  144. 144pub fn inpaint_fast(
  145. 145 img : @types.Image,
  146. 146 mask : @types.Image,
  147. 147 radius~ : Int32 = 5,
  148. 148) -> @types.Image raise @types.LoadError {
  149. 149 // 输入验证
  150. 150● if img.width <= 0 || img.height <= 0 {
  151. 151● raise @types.LoadError::DecodeFailed(
  152. 152 "inpaint_fast: 图像尺寸必须大于0",
  153. 153 )
  154. 154 }
  155. 155 if img.channels < 1 || img.channels > 4 {
  156. 156○ raise @types.LoadError::DecodeFailed(
  157. 157 "inpaint_fast: 通道数必须在1-4之间",
  158. 158 )
  159. 159 }
  160. 160 let expected_size = img.width * img.height * img.channels
  161. 161● if img.data.length() != expected_size {
  162. 162○ raise @types.LoadError::DecodeFailed(
  163. 163 "inpaint_fast: 图像数据大小不匹配,期望 " +
  164. 164○ expected_size.to_string() +
  165. 165 ",实际 " +
  166. 166○ img.data.length().to_string(),
  167. 167 )
  168. 168 }
  169. 169 if img.width != mask.width || img.height != mask.height {
  170. 170● raise @types.LoadError::DecodeFailed(
  171. 171 "inpaint_fast: 掩码尺寸必须与图像一致",
  172. 172 )
  173. 173 }
  174. 174 if mask.channels != 1 {
  175. 175○ raise @types.LoadError::DecodeFailed(
  176. 176 "inpaint_fast: 掩码必须为单通道图像",
  177. 177 )
  178. 178 }
  179. 179 let mask_expected_size = mask.width * mask.height
  180. 180● if mask.data.length() != mask_expected_size {
  181. 181○ raise @types.LoadError::DecodeFailed(
  182. 182 "inpaint_fast: 掩码数据大小不匹配,期望 " +
  183. 183○ mask_expected_size.to_string() +
  184. 184 ",实际 " +
  185. 185○ mask.data.length().to_string(),
  186. 186 )
  187. 187 }
  188. 188 if radius <= 0 {
  189. 189○ raise @types.LoadError::DecodeFailed("inpaint_fast: radius必须大于0")
  190. 190 }
  191. 191 let w = img.width
  192. 192 let h = img.height
  193. 193 let ch = img.channels
  194. 194 let n = w * h
  195. 195 let r = radius
  196. 196 // 复制原图数据
  197. 197● let out : Array[Int32] = Array::make(n * ch, 0)
  198. 198 for i in 0..<(n * ch) {
  199. 199● out[i] = img.data[i].to_int()
  200. 200 }
  201. 201 // 对每个待修复像素,用已知像素加权平均
  202. 202 for y in 0..<h {
  203. 203● for x in 0..<w {
  204. 204● let idx = y * w + x
  205. 205 if mask.data[idx] == b'\x00' {
  206. 206● continue
  207. 207 }
  208. 208 for c in 0..<ch {
  209. 209● let mut sum = 0.0F
  210. 210 let mut weight_sum = 0.0F
  211. 211 for dy in -r..<=r {
  212. 212● let ny = y + dy
  213. 213 if ny < 0 || ny >= h {
  214. 214● continue
  215. 215 }
  216. 216 for dx in -r..<=r {
  217. 217● if dx == 0 && dy == 0 {
  218. 218● continue
  219. 219 }
  220. 220 let nx = x + dx
  221. 221 if nx < 0 || nx >= w {
  222. 222● continue
  223. 223 }
  224. 224 let nidx = ny * w + nx
  225. 225 // 只用已知像素
  226. 226 if mask.data[nidx] != b'\x00' {
  227. 227● continue
  228. 228 }
  229. 229● let dist = Float::from_int(dx * dx + dy * dy).sqrt()
  230. 230 let weight = 1.0F / (dist + 1.0F)
  231. 231● sum += weight * Float::from_int(out[nidx * ch + c])
  232. 232 weight_sum += weight
  233. 233 }
  234. 234 }
  235. 235 if weight_sum > 0.0F {
  236. 236● out[idx * ch + c] = (sum / weight_sum).to_int()
  237. 237 }
  238. 238 }
  239. 239 }
  240. 240 }
  241. 241 // 转换输出
  242. 242● let out_data : Array[Byte] = Array::make(n * ch, b'\x00')
  243. 243 for i in 0..<(n * ch) {
  244. 244● out_data[i] = out[i].clamp(min=0, max=255).to_byte()
  245. 245 }
  246. 246 @types.Image::{
  247. 247 width: w,
  248. 248 height: h,
  249. 249 channels: ch,
  250. 250● data: Bytes::from_array(out_data),
  251. 251 }
  252. 252}