image.process\feature\histogram.mbt
97.9% · 143 / 146 points · 400 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 直方图 histogram - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 直方图操作(纯 MoonBit):均衡化 + 归一化 - 7
- 8
///| - 9
/// 计算灰度直方图。返回 256 个 bin 的计数数组。 - 10
/// 彩色图像先转灰度(BT.601)再计算。 - 11
/// 优化:直接从 Bytes 读取,避免额外数组分配 - 12
pub fn histogram(img : @types.Image) -> Array[Int32] raise @types.LoadError { - 13
// 输入验证 - 14●
if img.width <= 0 || img.height <= 0 { - 15●
raise @types.LoadError::DecodeFailed("histogram: 图像尺寸必须大于0") - 16
} - 17
if img.channels < 1 || img.channels > 4 { - 18●
raise @types.LoadError::DecodeFailed( - 19
"histogram: 通道数必须在1-4之间", - 20
) - 21
} - 22
let expected_size = img.width * img.height * img.channels - 23●
if img.data.length() != expected_size { - 24●
raise @types.LoadError::DecodeFailed( - 25
"histogram: 数据大小不匹配,期望 " + - 26●
expected_size.to_string() + - 27
",实际 " + - 28●
img.data.length().to_string(), - 29
) - 30
} - 31
- 32●
let hist : Array[Int32] = Array::make(256, 0) - 33
let n = img.width * img.height - 34
let ch = img.channels - 35
let data = img.data - 36
for i in 0..<n { - 37●
let base = i * ch - 38
let v = if ch == 1 { - 39●
data[base].to_int() - 40
} else { - 41●
let r = data[base].to_int() - 42●
let g = data[base + 1].to_int() - 43●
let b = data[base + 2].to_int() - 44
(r * 299 + g * 587 + b * 114) / 1000 - 45
} - 46
hist[v] += 1 - 47
} - 48
hist - 49
} - 50
- 51
///| - 52
/// 直方图均衡化。基于灰度直方图计算映射 LUT,应用到所有颜色通道。 - 53
/// alpha 通道不变。 - 54
pub fn histogram_equalize( - 55
img : @types.Image, - 56
) -> @types.Image raise @types.LoadError { - 57
// 输入验证 - 58●
if img.width <= 0 || img.height <= 0 { - 59●
raise @types.LoadError::DecodeFailed( - 60
"histogram_equalize: 图像尺寸必须大于0", - 61
) - 62
} - 63
if img.channels < 1 || img.channels > 4 { - 64●
raise @types.LoadError::DecodeFailed( - 65
"histogram_equalize: 通道数必须在1-4之间", - 66
) - 67
} - 68
let expected_size = img.width * img.height * img.channels - 69●
if img.data.length() != expected_size { - 70●
raise @types.LoadError::DecodeFailed( - 71
"histogram_equalize: 数据大小不匹配,期望 " + - 72●
expected_size.to_string() + - 73
",实际 " + - 74●
img.data.length().to_string(), - 75
) - 76
} - 77
- 78●
let hist = histogram(img) - 79
let n = img.width * img.height - 80
// 计算 CDF - 81●
let cdf : Array[Int32] = Array::make(256, 0) - 82
let mut cum = 0 - 83
for i in 0..<256 { - 84●
cum += hist[i] - 85
cdf[i] = cum - 86
} - 87
// 找到 CDF_min(第一个非零值) - 88
let mut cdf_min = 0 - 89
for i in 0..<256 { - 90●
if cdf[i] > 0 { - 91●
cdf_min = cdf[i] - 92
break - 93
} - 94
} - 95
// 计算映射 LUT - 96●
let lut : Array[Byte] = Array::make(256, b'\x00') - 97
let denom = n - cdf_min - 98
if denom <= 0 { - 99
// 所有像素相同,返回原图 - 100●
return img - 101
} - 102
for i in 0..<256 { - 103●
let mapped = (cdf[i] - cdf_min) * 255 / denom - 104●
lut[i] = mapped.clamp(min=0, max=255).to_byte() - 105
} - 106
// 应用 LUT - 107
let ch = img.channels - 108●
let color_ch = if ch >= 3 { 3 } else { ch } - 109●
let data_int : Array[Int32] = Array::make(n * ch, 0) - 110
for i in 0..<(n * ch) { - 111●
data_int[i] = img.data[i].to_int() - 112
} - 113●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 114
for i in 0..<n { - 115●
for c in 0..<color_ch { - 116●
out[i * ch + c] = lut[data_int[i * ch + c]] - 117
} - 118
if ch == 4 { - 119●
out[i * ch + 3] = img.data[i * ch + 3] - 120
} - 121
} - 122
@types.Image::{ - 123
width: img.width, - 124
height: img.height, - 125
channels: ch, - 126●
data: Bytes::from_array(out), - 127
} - 128
} - 129
- 130
///| - 131
/// 直方图归一化(线性拉伸)。将像素值范围线性映射到 [0, 255]。 - 132
/// alpha 通道不变。 - 133
pub fn histogram_normalize( - 134
img : @types.Image, - 135
) -> @types.Image raise @types.LoadError { - 136
// 输入验证 - 137●
if img.width <= 0 || img.height <= 0 { - 138●
raise @types.LoadError::DecodeFailed( - 139
"histogram_normalize: 图像尺寸必须大于0", - 140
) - 141
} - 142
if img.channels < 1 || img.channels > 4 { - 143○
raise @types.LoadError::DecodeFailed( - 144
"histogram_normalize: 通道数必须在1-4之间", - 145
) - 146
} - 147
let expected_size = img.width * img.height * img.channels - 148●
if img.data.length() != expected_size { - 149●
raise @types.LoadError::DecodeFailed( - 150
"histogram_normalize: 数据大小不匹配,期望 " + - 151●
expected_size.to_string() + - 152
",实际 " + - 153●
img.data.length().to_string(), - 154
) - 155
} - 156
- 157
let ch = img.channels - 158●
let color_ch = if ch >= 3 { 3 } else { ch } - 159
let n = img.width * img.height - 160●
let data_int : Array[Int32] = Array::make(n * ch, 0) - 161
for i in 0..<(n * ch) { - 162●
data_int[i] = img.data[i].to_int() - 163
} - 164
// 找到颜色通道的最小值和最大值 - 165
let mut min_val = 255 - 166
let mut max_val = 0 - 167
for i in 0..<n { - 168●
for c in 0..<color_ch { - 169●
let v = data_int[i * ch + c] - 170
if v < min_val { - 171●
min_val = v - 172
} - 173
if v > max_val { - 174●
max_val = v - 175
} - 176
} - 177
} - 178
let range = max_val - min_val - 179
if range == 0 { - 180●
return img - 181
} - 182
// 应用线性拉伸 - 183●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 184
for i in 0..<n { - 185●
for c in 0..<color_ch { - 186●
let v = data_int[i * ch + c] - 187
let scaled = (v - min_val) * 255 / range - 188●
out[i * ch + c] = scaled.clamp(min=0, max=255).to_byte() - 189
} - 190
if ch == 4 { - 191●
out[i * ch + 3] = img.data[i * ch + 3] - 192
} - 193
} - 194
@types.Image::{ - 195
width: img.width, - 196
height: img.height, - 197
channels: ch, - 198●
data: Bytes::from_array(out), - 199
} - 200
} - 201
- 202
///| - 203
/// 直方图比较方法 - 204
pub(all) enum HistCompareMethod { - 205
Correlation // 相关性 - 206
ChiSquare // 卡方 - 207
Bhattacharyya // 巴氏距离 - 208
} - 209
- 210
///| - 211
/// 比较两个直方图的相似度。返回值越大表示越相似(Correlation/ChiSquare), - 212
/// 或距离越小越相似(Bhattacharyya)。 - 213
pub fn compare_hist( - 214
hist1 : Array[Int32], - 215
hist2 : Array[Int32], - 216
compare_method : HistCompareMethod, - 217
) -> Float raise @types.LoadError { - 218
// 参数验证 - 219●
if hist1.length() == 0 { - 220●
raise @types.LoadError::DecodeFailed( - 221
"compare_hist: hist1数组不能为空", - 222
) - 223
} - 224●
if hist2.length() == 0 { - 225●
raise @types.LoadError::DecodeFailed( - 226
"compare_hist: hist2数组不能为空", - 227
) - 228
} - 229●
if hist1.length() != hist2.length() { - 230●
raise @types.LoadError::DecodeFailed( - 231
"compare_hist: hist1和hist2长度必须相同", - 232
) - 233
} - 234
- 235●
let n = hist1.length() - 236●
match compare_method { - 237●
Correlation => { - 238
let mut sum1 = 0.0F - 239
let mut sum2 = 0.0F - 240
let mut sum12 = 0.0F - 241
let mut sum1_sq = 0.0F - 242
let mut sum2_sq = 0.0F - 243
for i in 0..<n { - 244●
let h1 = Float::from_int(hist1[i]) - 245●
let h2 = Float::from_int(hist2[i]) - 246
sum1 += h1 - 247
sum2 += h2 - 248
sum12 += h1 * h2 - 249
sum1_sq += h1 * h1 - 250
sum2_sq += h2 * h2 - 251
} - 252●
let nf = Float::from_int(n) - 253
let num = nf * sum12 - sum1 * sum2 - 254●
let den = ((nf * sum1_sq - sum1 * sum1) * (nf * sum2_sq - sum2 * sum2)).sqrt() - 255
if den == 0.0F { - 256●
0.0F - 257
} else { - 258●
num / den - 259
} - 260
} - 261●
ChiSquare => { - 262
let mut chi = 0.0F - 263
for i in 0..<n { - 264●
let h1 = Float::from_int(hist1[i]) - 265●
let h2 = Float::from_int(hist2[i]) - 266
let diff = h1 - h2 - 267
let sum = h1 + h2 - 268
if sum > 0.0F { - 269●
chi += diff * diff / sum - 270
} - 271
} - 272
chi - 273
} - 274●
Bhattacharyya => { - 275
let mut bc = 0.0F - 276
let mut s1 = 0.0F - 277
let mut s2 = 0.0F - 278
for i in 0..<n { - 279●
let h1 = Float::from_int(hist1[i]) - 280●
let h2 = Float::from_int(hist2[i]) - 281●
bc += (h1 * h2).sqrt() - 282
s1 += h1 - 283
s2 += h2 - 284
} - 285
if s1 > 0.0F && s2 > 0.0F { - 286●
bc / (s1 * s2).sqrt() - 287
} else { - 288●
0.0F - 289
} - 290
} - 291
} - 292
} - 293
- 294
///| - 295
/// 直方图匹配(规定化)。将输入图像的直方图匹配到参考图像的直方图, - 296
/// 通过构建 LUT 实现灰度值映射。彩色图像对每个通道独立匹配。 - 297
pub fn histogram_matching( - 298
img : @types.Image, - 299
reference : @types.Image, - 300
) -> @types.Image raise @types.LoadError { - 301
// 输入验证 - 302●
if img.width <= 0 || img.height <= 0 { - 303●
raise @types.LoadError::DecodeFailed( - 304
"histogram_matching: 图像尺寸必须大于0", - 305
) - 306
} - 307
if img.channels < 1 || img.channels > 4 { - 308○
raise @types.LoadError::DecodeFailed( - 309
"histogram_matching: 通道数必须在1-4之间", - 310
) - 311
} - 312
let expected_size = img.width * img.height * img.channels - 313●
if img.data.length() != expected_size { - 314●
raise @types.LoadError::DecodeFailed( - 315
"histogram_matching: 数据大小不匹配,期望 " + - 316●
expected_size.to_string() + - 317
",实际 " + - 318●
img.data.length().to_string(), - 319
) - 320
} - 321
if reference.width <= 0 || reference.height <= 0 { - 322●
raise @types.LoadError::DecodeFailed( - 323
"histogram_matching: 参考图像尺寸必须大于0", - 324
) - 325
} - 326
if reference.channels < 1 || reference.channels > 4 { - 327○
raise @types.LoadError::DecodeFailed( - 328
"histogram_matching: 参考图像通道数必须在1-4之间", - 329
) - 330
} - 331
let ref_expected_size = reference.width * - 332
reference.height * - 333
reference.channels - 334●
if reference.data.length() != ref_expected_size { - 335●
raise @types.LoadError::DecodeFailed( - 336
"histogram_matching: 参考图像数据大小不匹配,期望 " + - 337●
ref_expected_size.to_string() + - 338
",实际 " + - 339●
reference.data.length().to_string(), - 340
) - 341
} - 342
- 343
let ch = img.channels - 344●
let color_ch = if ch >= 3 { 3 } else { ch } - 345
let n = img.width * img.height - 346●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 347
for c in 0..<color_ch { - 348●
let hist_in : Array[Int32] = Array::make(256, 0) - 349●
let hist_ref : Array[Int32] = Array::make(256, 0) - 350
for i in 0..<n { - 351●
hist_in[img.data[i * ch + c].to_int()] += 1 - 352
} - 353
let n_ref = reference.width * reference.height - 354
let ref_ch = reference.channels - 355●
let ref_color_ch = if ref_ch >= 3 { 3 } else { ref_ch } - 356
if c < ref_color_ch { - 357●
for i in 0..<n_ref { - 358●
hist_ref[reference.data[i * ref_ch + c].to_int()] += 1 - 359
} - 360
} - 361●
let cdf_in : Array[Float] = Array::make(256, 0.0F) - 362●
let cdf_ref : Array[Float] = Array::make(256, 0.0F) - 363
let mut cum_in = 0 - 364
let mut cum_ref = 0 - 365
for i in 0..<256 { - 366●
cum_in += hist_in[i] - 367●
cdf_in[i] = Float::from_int(cum_in) / Float::from_int(n) - 368
cum_ref += hist_ref[i] - 369●
cdf_ref[i] = Float::from_int(cum_ref) / Float::from_int(n_ref) - 370
} - 371●
let lut : Array[Byte] = Array::make(256, b'\x00') - 372
for i in 0..<256 { - 373●
let target = cdf_in[i] - 374
let mut best_j = 0 - 375
let mut best_diff = 1.0F - 376
for j in 0..<256 { - 377●
let diff = (cdf_ref[j] - target).abs() - 378
if diff < best_diff { - 379●
best_diff = diff - 380
best_j = j - 381
} - 382
} - 383●
lut[i] = best_j.to_byte() - 384
} - 385
for i in 0..<n { - 386●
out[i * ch + c] = lut[img.data[i * ch + c].to_int()] - 387
} - 388
} - 389
if ch == 4 { - 390●
for i in 0..<n { - 391●
out[i * ch + 3] = img.data[i * ch + 3] - 392
} - 393
} - 394
@types.Image::{ - 395
width: img.width, - 396
height: img.height, - 397
channels: ch, - 398●
data: Bytes::from_array(out), - 399
} - 400
}