image.process\enhance\inpaint.mbt

96.6% · 113 / 117 points · 386 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// 图像修复(v12.0.0)
  7. 7// 基于快速行进法(Fast Marching Method)的图像修复
  8. 8
  9. 9///|
  10. 10/// 图像修复配置
  11. 11pub(all) struct InpaintConfig {
  12. 12 radius : Int32 // 修复半径
  13. 13 method_type : String // "telea" / "ns"(Navier-Stokes)
  14. 14 max_iterations : Int32 // 最大迭代次数
  15. 15}
  16. 16
  17. 17///|
  18. 18/// 创建默认图像修复配置
  19. 19pub fn inpaint_default_config() -> InpaintConfig {
  20. 20● InpaintConfig::{ radius: 5, method_type: "telea", max_iterations: 1000, }
  21. 21}
  22. 22
  23. 23///|
  24. 24/// 图像修复(Telea 算法,简化版)
  25. 25/// 基于快速行进法,从边界向内部逐步修复
  26. 26pub fn inpaint_advanced(
  27. 27 img : @types.Image,
  28. 28 mask : @types.Image,
  29. 29 radius~ : Int32 = 5,
  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 "inpaint_advanced: 图像尺寸必须大于0",
  35. 35 )
  36. 36 }
  37. 37 if img.channels < 1 || img.channels > 4 {
  38. 38● raise @types.LoadError::DecodeFailed(
  39. 39 "inpaint_advanced: 通道数必须在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 "inpaint_advanced: 图像数据大小不匹配,期望 " +
  46. 46● expected_size.to_string() +
  47. 47 ",实际 " +
  48. 48● img.data.length().to_string(),
  49. 49 )
  50. 50 }
  51. 51 if mask.width != img.width || mask.height != img.height {
  52. 52● raise @types.LoadError::DecodeFailed(
  53. 53 "inpaint_advanced: 掩码图像尺寸必须与原图一致",
  54. 54 )
  55. 55 }
  56. 56 if mask.channels < 1 {
  57. 57○ raise @types.LoadError::DecodeFailed(
  58. 58 "inpaint_advanced: 掩码通道数必须大于0",
  59. 59 )
  60. 60 }
  61. 61 if radius <= 0 {
  62. 62● raise @types.LoadError::DecodeFailed(
  63. 63 "inpaint_advanced: radius必须大于0",
  64. 64 )
  65. 65 }
  66. 66
  67. 67 let r = radius
  68. 68 let w = img.width
  69. 69 let h = img.height
  70. 70 let channels = img.channels
  71. 71 let img_data = img.data
  72. 72 let mask_data = mask.data
  73. 73 // 复制原图
  74. 74● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  75. 75 for i in 0..<(w * h * channels) {
  76. 76● out_data[i] = img_data[i]
  77. 77 }
  78. 78 // 标记需要修复的区域(mask 中非零像素)
  79. 79 let to_inpaint : Array[Bool] = []
  80. 80 for i in 0..<(w * h) {
  81. 81● let mask_val = if mask.channels >= 1 {
  82. 82● mask_data[i * mask.channels].to_int()
  83. 83 } else {
  84. 84○ 0
  85. 85 }
  86. 86● to_inpaint.push(mask_val > 128)
  87. 87 }
  88. 88 // 快速行进法:迭代修复边界像素
  89. 89 let mut changed = true
  90. 90 let mut iteration = 0
  91. 91 while changed && iteration < 1000 {
  92. 92● changed = false
  93. 93 iteration += 1
  94. 94 // 找到所有待修复区域的边界像素
  95. 95 let boundary : Array[(Int32, Int32)] = []
  96. 96 for y in 0..<h {
  97. 97● for x in 0..<w {
  98. 98● if to_inpaint[y * w + x] {
  99. 99 // 检查是否有已修复的邻域
  100. 100● let mut has_known_neighbor = false
  101. 101 let neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)]
  102. 102 for _tup0 in neighbors {
  103. 103● let (dx, dy) = _tup0
  104. 104 let nx = x + dx
  105. 105 let ny = y + dy
  106. 106 if nx >= 0 &&
  107. 107 nx < w &&
  108. 108 ny >= 0 &&
  109. 109 ny < h &&
  110. 110 !to_inpaint[ny * w + nx] {
  111. 111● has_known_neighbor = true
  112. 112 }
  113. 113 }
  114. 114 if has_known_neighbor {
  115. 115● boundary.push((x, y))
  116. 116 }
  117. 117 }
  118. 118 }
  119. 119 }
  120. 120 // 对边界像素进行修复
  121. 121 for _tup1 in boundary {
  122. 122● let (x, y) = _tup1
  123. 123 // 计算邻域已知像素的加权平均
  124. 124 let mut sum_r = 0.0
  125. 125 let mut sum_g = 0.0
  126. 126 let mut sum_b = 0.0
  127. 127 let mut weight_sum = 0.0
  128. 128 for dy in -r..<(r + 1) {
  129. 129● for dx in -r..<(r + 1) {
  130. 130● let nx = x + dx
  131. 131 let ny = y + dy
  132. 132 if nx >= 0 && nx < w && ny >= 0 && ny < h && !to_inpaint[ny * w + nx] {
  133. 133 // 距离加权(越近权重越大)
  134. 134● let dist = (dx * dx + dy * dy).to_double().sqrt()
  135. 135 let weight = 1.0 / (dist + 1.0)
  136. 136 let idx = (ny * w + nx) * channels
  137. 137● sum_r += out_data[idx].to_double() * weight
  138. 138 if channels >= 2 {
  139. 139● sum_g += out_data[idx + 1].to_double() * weight
  140. 140 }
  141. 141 if channels >= 3 {
  142. 142● sum_b += out_data[idx + 2].to_double() * weight
  143. 143 }
  144. 144 weight_sum += weight
  145. 145 }
  146. 146 }
  147. 147 }
  148. 148 if weight_sum > 0.0 {
  149. 149● let idx = (y * w + x) * channels
  150. 150 out_data[idx] = (sum_r / weight_sum)
  151. 151● .min(255.0)
  152. 152● .max(0.0)
  153. 153● .to_int()
  154. 154● .to_byte()
  155. 155 if channels >= 2 {
  156. 156● out_data[idx + 1] = (sum_g / weight_sum)
  157. 157● .min(255.0)
  158. 158● .max(0.0)
  159. 159● .to_int()
  160. 160● .to_byte()
  161. 161 }
  162. 162 if channels >= 3 {
  163. 163● out_data[idx + 2] = (sum_b / weight_sum)
  164. 164● .min(255.0)
  165. 165● .max(0.0)
  166. 166● .to_int()
  167. 167● .to_byte()
  168. 168 }
  169. 169 if channels >= 4 {
  170. 170● out_data[idx + 3] = img_data[idx + 3]
  171. 171 }
  172. 172 // 标记为已修复
  173. 173 to_inpaint[y * w + x] = false
  174. 174 changed = true
  175. 175 }
  176. 176 }
  177. 177 }
  178. 178 @types.Image::{
  179. 179 width: w,
  180. 180 height: h,
  181. 181 channels,
  182. 182● data: Bytes::from_array(out_data),
  183. 183 }
  184. 184}
  185. 185
  186. 186///|
  187. 187/// 创建矩形修复掩码
  188. 188pub fn create_rect_mask(
  189. 189 width : Int32,
  190. 190 height : Int32,
  191. 191 x : Int32,
  192. 192 y : Int32,
  193. 193 rect_w : Int32,
  194. 194 rect_h : Int32,
  195. 195) -> @types.Image raise @types.LoadError {
  196. 196 // 输入验证
  197. 197● if width <= 0 || height <= 0 {
  198. 198● raise @types.LoadError::DecodeFailed(
  199. 199 "create_rect_mask: 图像尺寸必须大于0",
  200. 200 )
  201. 201 }
  202. 202 if x < 0 || y < 0 {
  203. 203● raise @types.LoadError::DecodeFailed(
  204. 204 "create_rect_mask: 矩形坐标不能为负",
  205. 205 )
  206. 206 }
  207. 207 if rect_w <= 0 || rect_h <= 0 {
  208. 208● raise @types.LoadError::DecodeFailed(
  209. 209 "create_rect_mask: 矩形尺寸必须大于0",
  210. 210 )
  211. 211 }
  212. 212 if x + rect_w > width || y + rect_h > height {
  213. 213● raise @types.LoadError::DecodeFailed(
  214. 214 "create_rect_mask: 矩形超出图像边界",
  215. 215 )
  216. 216 }
  217. 217
  218. 218● let mask_data : Array[Byte] = Array::make(width * height, b'\x00')
  219. 219 for py in 0..<height {
  220. 220● for px in 0..<width {
  221. 221● if px >= x && px < x + rect_w && py >= y && py < y + rect_h {
  222. 222● mask_data[py * width + px] = b'\xFF'
  223. 223 }
  224. 224 }
  225. 225 }
  226. 226 @types.Image::{
  227. 227 width,
  228. 228 height,
  229. 229 channels: 1,
  230. 230● data: Bytes::from_array(mask_data),
  231. 231 }
  232. 232}
  233. 233
  234. 234///|
  235. 235/// 去除图像中的小物体(基于连通域分析)
  236. 236pub fn remove_small_objects(
  237. 237 img : @types.Image,
  238. 238 max_size~ : Int32 = 100,
  239. 239) -> @types.Image raise @types.LoadError {
  240. 240 // 输入验证
  241. 241● if img.width <= 0 || img.height <= 0 {
  242. 242● raise @types.LoadError::DecodeFailed(
  243. 243 "remove_small_objects: 图像尺寸必须大于0",
  244. 244 )
  245. 245 }
  246. 246 if img.channels < 1 || img.channels > 4 {
  247. 247● raise @types.LoadError::DecodeFailed(
  248. 248 "remove_small_objects: 通道数必须在1-4之间",
  249. 249 )
  250. 250 }
  251. 251 let expected_size = img.width * img.height * img.channels
  252. 252● if img.data.length() != expected_size {
  253. 253● raise @types.LoadError::DecodeFailed(
  254. 254 "remove_small_objects: 数据大小不匹配,期望 " +
  255. 255● expected_size.to_string() +
  256. 256 ",实际 " +
  257. 257● img.data.length().to_string(),
  258. 258 )
  259. 259 }
  260. 260 if max_size <= 0 {
  261. 261○ raise @types.LoadError::DecodeFailed(
  262. 262 "remove_small_objects: max_size必须大于0",
  263. 263 )
  264. 264 }
  265. 265
  266. 266 let ms = max_size
  267. 267 let w = img.width
  268. 268 let h = img.height
  269. 269 let channels = img.channels
  270. 270 let data = img.data
  271. 271 // 转换为灰度
  272. 272 let gray : Array[Int32] = []
  273. 273 for i in 0..<(w * h) {
  274. 274● let idx = i * channels
  275. 275● let r = data[idx].to_int()
  276. 276● let g = if channels >= 2 { data[idx + 1].to_int() } else { r }
  277. 277● let b = if channels >= 3 { data[idx + 2].to_int() } else { r }
  278. 278● gray.push(
  279. 279● (0.299 * r.to_double() + 0.587 * g.to_double() + 0.114 * b.to_double()).to_int(),
  280. 280 )
  281. 281 }
  282. 282 // 连通域分析(洪水填充)
  283. 283● let labels : Array[Int32] = Array::make(w * h, 0)
  284. 284 let mut next_label = 1
  285. 285 let sizes : Array[Int32] = []
  286. 286 for y in 0..<h {
  287. 287● for x in 0..<w {
  288. 288● if gray[y * w + x] > 128 && labels[y * w + x] == 0 {
  289. 289 // 洪水填充
  290. 290● let stack : Array[(Int32, Int32)] = []
  291. 291● stack.push((x, y))
  292. 292 labels[y * w + x] = next_label
  293. 293 let mut size = 0
  294. 294● while stack.length() > 0 {
  295. 295● match stack.pop() {
  296. 296● Some((cx, cy)) => {
  297. 297 size += 1
  298. 298 let neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)]
  299. 299 for _tup2 in neighbors {
  300. 300● let (dx, dy) = _tup2
  301. 301 let nx = cx + dx
  302. 302 let ny = cy + dy
  303. 303 if nx >= 0 &&
  304. 304 nx < w &&
  305. 305 ny >= 0 &&
  306. 306 ny < h &&
  307. 307 gray[ny * w + nx] > 128 &&
  308. 308 labels[ny * w + nx] == 0 {
  309. 309● labels[ny * w + nx] = next_label
  310. 310● stack.push((nx, ny))
  311. 311 }
  312. 312 }
  313. 313 }
  314. 314○ None => ignore(())
  315. 315 }
  316. 316 }
  317. 317● sizes.push(size)
  318. 318 next_label += 1
  319. 319 }
  320. 320 }
  321. 321 }
  322. 322 // 去除小物体(将小连通域的像素设为背景色)
  323. 323● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  324. 324 for i in 0..<(w * h) {
  325. 325● let idx = i * channels
  326. 326 let label = labels[i]
  327. 327 if label > 0 && sizes[label - 1] <= ms {
  328. 328 // 小物体:设为黑色(或周围背景色)
  329. 329● for c in 0..<channels {
  330. 330● out_data[idx + c] = b'\x00'
  331. 331 }
  332. 332 } else {
  333. 333 // 保留原像素
  334. 334● for c in 0..<channels {
  335. 335● out_data[idx + c] = data[idx + c]
  336. 336 }
  337. 337 }
  338. 338 }
  339. 339 @types.Image::{
  340. 340 width: w,
  341. 341 height: h,
  342. 342 channels,
  343. 343● data: Bytes::from_array(out_data),
  344. 344 }
  345. 345}
  346. 346
  347. 347///|
  348. 348/// 图像去水印(基于修复的简化版)
  349. 349/// 将指定区域的像素用周围像素填充
  350. 350pub fn remove_watermark(
  351. 351 img : @types.Image,
  352. 352 x : Int32,
  353. 353 y : Int32,
  354. 354 width : Int32,
  355. 355 height : Int32,
  356. 356) -> @types.Image raise @types.LoadError {
  357. 357 // 输入验证
  358. 358● if img.width <= 0 || img.height <= 0 {
  359. 359● raise @types.LoadError::DecodeFailed(
  360. 360 "remove_watermark: 图像尺寸必须大于0",
  361. 361 )
  362. 362 }
  363. 363 if img.channels < 1 || img.channels > 4 {
  364. 364● raise @types.LoadError::DecodeFailed(
  365. 365 "remove_watermark: 通道数必须在1-4之间",
  366. 366 )
  367. 367 }
  368. 368 if x < 0 || y < 0 {
  369. 369● raise @types.LoadError::DecodeFailed(
  370. 370 "remove_watermark: 区域坐标不能为负",
  371. 371 )
  372. 372 }
  373. 373 if width <= 0 || height <= 0 {
  374. 374● raise @types.LoadError::DecodeFailed(
  375. 375 "remove_watermark: 区域尺寸必须大于0",
  376. 376 )
  377. 377 }
  378. 378 if x + width > img.width || y + height > img.height {
  379. 379● raise @types.LoadError::DecodeFailed(
  380. 380 "remove_watermark: 区域超出图像边界",
  381. 381 )
  382. 382 }
  383. 383
  384. 384● let mask = create_rect_mask(img.width, img.height, x, y, width, height)
  385. 385● inpaint_advanced(img, mask, radius=5)
  386. 386}