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
// 自适应阈值(纯 MoonBit)
///|
/// 自适应阈值(均值法)。以每个像素周围 block_size×block_size 区域的均值减去 C 作为局部阈值。
/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4),alpha 通道不变。
pub fn adaptive_threshold_mean(
img : @types.Image,
block_size : Int,
c : Int,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_mean: 图像尺寸必须大于0",
)
}
if img.channels != 1 && img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_mean 仅支持 channels=1/3/4",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_mean: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if block_size < 3 || block_size % 2 == 0 {
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
}
let ch = img.channels
let color_ch = if ch >= 3 { 3 } else { 1 }
let w = img.width
let h = img.height
let n = w * h
let out : Array[Byte] = Array::make(n * ch, b'\x00')
let half = block_size / 2
// 预计算像素 Int 数组
let src : Array[Int] = Array::make(n * ch, 0)
for i in 0..<(n * ch) {
src[i] = img.data[i].to_int()
}
// 内部区域 count 恒为 block_size²
let inner_count = block_size * block_size
for c_idx in 0..<color_ch {
// 内部区域:x in [half, w-half), y in [half, h-half)
for y in half..<(h - half) {
for x in half..<(w - half) {
let mut sum = 0
for dy in -half..<=half {
let row = (y + dy) * w
for dx in -half..<=half {
sum += src[(row + x + dx) * ch + c_idx]
}
}
let mean = sum / inner_count
let threshold = mean - c
let v = src[(y * w + x) * ch + c_idx]
out[(y * w + x) * ch + c_idx] = if v > threshold {
b'\xFF'
} else {
b'\x00'
}
}
}
// 边界区域
for y in 0..<h {
let y_inner = y >= half && y < h - half
for x in 0..<w {
if y_inner && x >= half && x < w - half {
continue
}
let mut sum = 0
let mut count = 0
for dy in -half..<=half {
for dx in -half..<=half {
let ny = y + dy
let nx = x + dx
if ny >= 0 && ny < h && nx >= 0 && nx < w {
sum += src[(ny * w + nx) * ch + c_idx]
count += 1
}
}
}
let mean = if count > 0 { sum / count } else { 0 }
let threshold = mean - c
let v = src[(y * w + x) * ch + c_idx]
out[(y * w + x) * ch + c_idx] = if v > threshold {
b'\xFF'
} else {
b'\x00'
}
}
}
}
if ch == 4 {
for i in 0..<n {
out[i * 4 + 3] = img.data[i * 4 + 3]
}
}
@types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 自适应阈值(高斯加权法)。以每个像素周围 block_size×block_size 区域的高斯加权均值减去 C 作为局部阈值。
pub fn adaptive_threshold_gaussian(
img : @types.Image,
block_size : Int,
c : Int,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_gaussian: 图像尺寸必须大于0",
)
}
if img.channels != 1 && img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_gaussian 仅支持 channels=1/3/4",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_gaussian: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if block_size < 3 || block_size % 2 == 0 {
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
}
let ch = img.channels
let color_ch = if ch >= 3 { 3 } else { 1 }
let w = img.width
let h = img.height
let n = w * h
let out : Array[Byte] = Array::make(n * ch, b'\x00')
let half = block_size / 2
let sigma = Float::from_int(half) / 2.0F
let two_sigma_sq = 2.0F * sigma * sigma
// 预计算高斯权重(1D 扁平化)
let weights : Array[Float] = Array::make(block_size * block_size, 0.0F)
let mut total_weight = 0.0F
for dy in 0..<block_size {
for dx in 0..<block_size {
let fy = Float::from_int(dy - half)
let fx = Float::from_int(dx - half)
let wt = @math.expf(-(fx * fx + fy * fy) / two_sigma_sq)
weights[dy * block_size + dx] = wt
total_weight += wt
}
}
// 预计算像素 Float 数组
let src : Array[Float] = Array::make(n * ch, 0.0F)
for i in 0..<(n * ch) {
src[i] = Float::from_int(img.data[i].to_int())
}
for c_idx in 0..<color_ch {
// 内部区域:weight_sum 恒为 total_weight
for y in half..<(h - half) {
for x in half..<(w - half) {
let mut sum = 0.0F
for dy in 0..<block_size {
let row = (y + dy - half) * w
let wbase = dy * block_size
for dx in 0..<block_size {
sum += src[(row + x + dx - half) * ch + c_idx] * weights[wbase + dx]
}
}
let mean = sum / total_weight
let threshold = mean - Float::from_int(c)
let v = src[(y * w + x) * ch + c_idx]
out[(y * w + x) * ch + c_idx] = if v > threshold {
b'\xFF'
} else {
b'\x00'
}
}
}
// 边界区域
for y in 0..<h {
let y_inner = y >= half && y < h - half
for x in 0..<w {
if y_inner && x >= half && x < w - half {
continue
}
let mut sum = 0.0F
let mut weight_sum = 0.0F
for dy in -half..<=half {
for dx in -half..<=half {
let ny = y + dy
let nx = x + dx
if ny >= 0 && ny < h && nx >= 0 && nx < w {
let weight = weights[(dy + half) * block_size + (dx + half)]
sum += src[(ny * w + nx) * ch + c_idx] * weight
weight_sum += weight
}
}
}
let mean = if weight_sum > 0.0F { sum / weight_sum } else { 0.0F }
let threshold = mean - Float::from_int(c)
let v = src[(y * w + x) * ch + c_idx]
out[(y * w + x) * ch + c_idx] = if v > threshold {
b'\xFF'
} else {
b'\x00'
}
}
}
}
if ch == 4 {
for i in 0..<n {
out[i * 4 + 3] = img.data[i * 4 + 3]
}
}
@types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// Otsu 大津法自动阈值。自动计算最佳全局阈值并二值化。
pub fn threshold_otsu(
img : @types.Image,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"threshold_otsu: 图像尺寸必须大于0",
)
}
if img.channels != 1 && img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"threshold_otsu 仅支持 channels=1/3/4",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"threshold_otsu: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
let ch = img.channels
let color_ch = if ch >= 3 { 3 } else { 1 }
let w = img.width
let h = img.height
let n = w * h
let out : Array[Byte] = Array::make(n * ch, b'\x00')
for c_idx in 0..<color_ch {
let hist : Array[Int] = Array::make(256, 0)
for i in 0..<n {
hist[img.data[i * ch + c_idx].to_int()] += 1
}
let mut total = 0
let mut sum = 0
for t in 0..<256 {
total += hist[t]
sum += t * hist[t]
}
let mut best_t = 0
let mut best_var = -1.0F
let mut w0 = 0
let mut sum0 = 0
for t in 0..<256 {
w0 += hist[t]
sum0 += t * hist[t]
if w0 == 0 || w0 == total {
continue
}
let w1 = total - w0
let sum1 = sum - sum0
let mean0 = Float::from_int(sum0) / Float::from_int(w0)
let mean1 = Float::from_int(sum1) / Float::from_int(w1)
let var_between = Float::from_int(w0) *
Float::from_int(w1) *
(mean0 - mean1) *
(mean0 - mean1)
if var_between > best_var {
best_var = var_between
best_t = t
}
}
for i in 0..<n {
let v = img.data[i * ch + c_idx].to_int()
out[i * ch + c_idx] = if v > best_t { b'\xFF' } else { b'\x00' }
}
}
if ch == 4 {
for i in 0..<n {
out[i * 4 + 3] = img.data[i * 4 + 3]
}
}
@types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}