image.process\color\adaptive_threshold.mbt
87.5% · 98 / 112 points · 320 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 自适应阈值 adaptive threshold - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 自适应阈值(纯 MoonBit) - 7
- 8
///| - 9
/// 自适应阈值(均值法)。以每个像素周围 block_size×block_size 区域的均值减去 C 作为局部阈值。 - 10
/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4),alpha 通道不变。 - 11
pub fn adaptive_threshold_mean( - 12
img : @types.Image, - 13
block_size : Int32, - 14
c : Int32, - 15
) -> @types.Image raise @types.LoadError { - 16
// 输入验证 - 17●
if img.width <= 0 || img.height <= 0 { - 18●
raise @types.LoadError::DecodeFailed( - 19
"adaptive_threshold_mean: 图像尺寸必须大于0", - 20
) - 21
} - 22
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 23●
raise @types.LoadError::DecodeFailed( - 24
"adaptive_threshold_mean 仅支持 channels=1/3/4", - 25
) - 26
} - 27
let expected_size = img.width * img.height * img.channels - 28●
if img.data.length() != expected_size { - 29○
raise @types.LoadError::DecodeFailed( - 30
"adaptive_threshold_mean: 数据大小不匹配,期望 " + - 31○
expected_size.to_string() + - 32
",实际 " + - 33○
img.data.length().to_string(), - 34
) - 35
} - 36
if block_size < 3 || block_size % 2 == 0 { - 37●
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数") - 38
} - 39
let ch = img.channels - 40●
let color_ch = if ch >= 3 { 3 } else { 1 } - 41
let w = img.width - 42
let h = img.height - 43
let n = w * h - 44●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 45
let half = block_size / 2 - 46
// 预计算像素 Int 数组 - 47●
let src : Array[Int32] = Array::make(n * ch, 0) - 48
for i in 0..<(n * ch) { - 49●
src[i] = img.data[i].to_int() - 50
} - 51
// 内部区域 count 恒为 block_size² - 52
let inner_count = block_size * block_size - 53
for c_idx in 0..<color_ch { - 54
// 内部区域:x in [half, w-half), y in [half, h-half) - 55●
for y in half..<(h - half) { - 56●
for x in half..<(w - half) { - 57●
let mut sum = 0 - 58
for dy in -half..<=half { - 59●
let row = (y + dy) * w - 60
for dx in -half..<=half { - 61●
sum += src[(row + x + dx) * ch + c_idx] - 62
} - 63
} - 64
let mean = sum / inner_count - 65
let threshold = mean - c - 66
let v = src[(y * w + x) * ch + c_idx] - 67
out[(y * w + x) * ch + c_idx] = if v > threshold { - 68●
b'\xFF' - 69
} else { - 70●
b'\x00' - 71
} - 72
} - 73
} - 74
// 边界区域 - 75
for y in 0..<h { - 76●
let y_inner = y >= half && y < h - half - 77
for x in 0..<w { - 78●
if y_inner && x >= half && x < w - half { - 79●
continue - 80
} - 81
let mut sum = 0 - 82
let mut count = 0 - 83
for dy in -half..<=half { - 84●
for dx in -half..<=half { - 85●
let ny = y + dy - 86
let nx = x + dx - 87
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 88●
sum += src[(ny * w + nx) * ch + c_idx] - 89
count += 1 - 90
} - 91
} - 92
} - 93◐
let mean = if count > 0 { sum / count } else { 0 } - 94
let threshold = mean - c - 95
let v = src[(y * w + x) * ch + c_idx] - 96
out[(y * w + x) * ch + c_idx] = if v > threshold { - 97●
b'\xFF' - 98
} else { - 99●
b'\x00' - 100
} - 101
} - 102
} - 103
} - 104
if ch == 4 { - 105●
for i in 0..<n { - 106●
out[i * 4 + 3] = img.data[i * 4 + 3] - 107
} - 108
} - 109
@types.Image::{ - 110
width: w, - 111
height: h, - 112
channels: ch, - 113●
data: Bytes::from_array(out), - 114
} - 115
} - 116
- 117
///| - 118
/// 自适应阈值(高斯加权法)。以每个像素周围 block_size×block_size 区域的高斯加权均值减去 C 作为局部阈值。 - 119
pub fn adaptive_threshold_gaussian( - 120
img : @types.Image, - 121
block_size : Int32, - 122
c : Int32, - 123
) -> @types.Image raise @types.LoadError { - 124
// 输入验证 - 125●
if img.width <= 0 || img.height <= 0 { - 126●
raise @types.LoadError::DecodeFailed( - 127
"adaptive_threshold_gaussian: 图像尺寸必须大于0", - 128
) - 129
} - 130
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"adaptive_threshold_gaussian 仅支持 channels=1/3/4", - 133
) - 134
} - 135
let expected_size = img.width * img.height * img.channels - 136●
if img.data.length() != expected_size { - 137○
raise @types.LoadError::DecodeFailed( - 138
"adaptive_threshold_gaussian: 数据大小不匹配,期望 " + - 139○
expected_size.to_string() + - 140
",实际 " + - 141○
img.data.length().to_string(), - 142
) - 143
} - 144
if block_size < 3 || block_size % 2 == 0 { - 145●
raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数") - 146
} - 147
let ch = img.channels - 148●
let color_ch = if ch >= 3 { 3 } else { 1 } - 149
let w = img.width - 150
let h = img.height - 151
let n = w * h - 152●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 153
let half = block_size / 2 - 154●
let sigma = Float::from_int(half) / 2.0F - 155
let two_sigma_sq = 2.0F * sigma * sigma - 156
// 预计算高斯权重(1D 扁平化) - 157●
let weights : Array[Float] = Array::make(block_size * block_size, 0.0F) - 158
let mut total_weight = 0.0F - 159
for dy in 0..<block_size { - 160●
for dx in 0..<block_size { - 161●
let fy = Float::from_int(dy - half) - 162●
let fx = Float::from_int(dx - half) - 163●
let wt = @math.expf(-(fx * fx + fy * fy) / two_sigma_sq) - 164
weights[dy * block_size + dx] = wt - 165
total_weight += wt - 166
} - 167
} - 168
// 预计算像素 Float 数组 - 169●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 170
for i in 0..<(n * ch) { - 171●
src[i] = Float::from_int(img.data[i].to_int()) - 172
} - 173
for c_idx in 0..<color_ch { - 174
// 内部区域:weight_sum 恒为 total_weight - 175●
for y in half..<(h - half) { - 176●
for x in half..<(w - half) { - 177●
let mut sum = 0.0F - 178
for dy in 0..<block_size { - 179●
let row = (y + dy - half) * w - 180
let wbase = dy * block_size - 181
for dx in 0..<block_size { - 182●
sum += src[(row + x + dx - half) * ch + c_idx] * weights[wbase + dx] - 183
} - 184
} - 185
let mean = sum / total_weight - 186●
let threshold = mean - Float::from_int(c) - 187
let v = src[(y * w + x) * ch + c_idx] - 188
out[(y * w + x) * ch + c_idx] = if v > threshold { - 189●
b'\xFF' - 190
} else { - 191●
b'\x00' - 192
} - 193
} - 194
} - 195
// 边界区域 - 196
for y in 0..<h { - 197●
let y_inner = y >= half && y < h - half - 198
for x in 0..<w { - 199●
if y_inner && x >= half && x < w - half { - 200●
continue - 201
} - 202
let mut sum = 0.0F - 203
let mut weight_sum = 0.0F - 204
for dy in -half..<=half { - 205●
for dx in -half..<=half { - 206●
let ny = y + dy - 207
let nx = x + dx - 208
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 209●
let weight = weights[(dy + half) * block_size + (dx + half)] - 210
sum += src[(ny * w + nx) * ch + c_idx] * weight - 211
weight_sum += weight - 212
} - 213
} - 214
} - 215◐
let mean = if weight_sum > 0.0F { sum / weight_sum } else { 0.0F } - 216●
let threshold = mean - Float::from_int(c) - 217
let v = src[(y * w + x) * ch + c_idx] - 218
out[(y * w + x) * ch + c_idx] = if v > threshold { - 219●
b'\xFF' - 220
} else { - 221●
b'\x00' - 222
} - 223
} - 224
} - 225
} - 226
if ch == 4 { - 227●
for i in 0..<n { - 228●
out[i * 4 + 3] = img.data[i * 4 + 3] - 229
} - 230
} - 231
@types.Image::{ - 232
width: w, - 233
height: h, - 234
channels: ch, - 235●
data: Bytes::from_array(out), - 236
} - 237
} - 238
- 239
///| - 240
/// Otsu 大津法自动阈值。自动计算最佳全局阈值并二值化。 - 241
pub fn threshold_otsu( - 242
img : @types.Image, - 243
) -> @types.Image raise @types.LoadError { - 244
// 输入验证 - 245●
if img.width <= 0 || img.height <= 0 { - 246●
raise @types.LoadError::DecodeFailed( - 247
"threshold_otsu: 图像尺寸必须大于0", - 248
) - 249
} - 250
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 251●
raise @types.LoadError::DecodeFailed( - 252
"threshold_otsu 仅支持 channels=1/3/4", - 253
) - 254
} - 255
let expected_size = img.width * img.height * img.channels - 256●
if img.data.length() != expected_size { - 257○
raise @types.LoadError::DecodeFailed( - 258
"threshold_otsu: 数据大小不匹配,期望 " + - 259○
expected_size.to_string() + - 260
",实际 " + - 261○
img.data.length().to_string(), - 262
) - 263
} - 264
let ch = img.channels - 265●
let color_ch = if ch >= 3 { 3 } else { 1 } - 266
let w = img.width - 267
let h = img.height - 268
let n = w * h - 269●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 270
for c_idx in 0..<color_ch { - 271●
let hist : Array[Int32] = Array::make(256, 0) - 272
for i in 0..<n { - 273●
hist[img.data[i * ch + c_idx].to_int()] += 1 - 274
} - 275
let mut total = 0 - 276
let mut sum = 0 - 277
for t in 0..<256 { - 278●
total += hist[t] - 279
sum += t * hist[t] - 280
} - 281
let mut best_t = 0 - 282
let mut best_var = -1.0F - 283
let mut w0 = 0 - 284
let mut sum0 = 0 - 285
for t in 0..<256 { - 286●
w0 += hist[t] - 287
sum0 += t * hist[t] - 288
if w0 == 0 || w0 == total { - 289●
continue - 290
} - 291
let w1 = total - w0 - 292
let sum1 = sum - sum0 - 293●
let mean0 = Float::from_int(sum0) / Float::from_int(w0) - 294●
let mean1 = Float::from_int(sum1) / Float::from_int(w1) - 295●
let var_between = Float::from_int(w0) * - 296●
Float::from_int(w1) * - 297
(mean0 - mean1) * - 298
(mean0 - mean1) - 299
if var_between > best_var { - 300●
best_var = var_between - 301
best_t = t - 302
} - 303
} - 304
for i in 0..<n { - 305●
let v = img.data[i * ch + c_idx].to_int() - 306●
out[i * ch + c_idx] = if v > best_t { b'\xFF' } else { b'\x00' } - 307
} - 308
} - 309
if ch == 4 { - 310●
for i in 0..<n { - 311●
out[i * 4 + 3] = img.data[i * 4 + 3] - 312
} - 313
} - 314
@types.Image::{ - 315
width: w, - 316
height: h, - 317
channels: ch, - 318●
data: Bytes::from_array(out), - 319
} - 320
}