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