image.process\color\clahe.mbt
90.8% · 69 / 76 points · 181 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # CLAHE 自适应直方图均衡 clahe - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// CLAHE: 对比度受限自适应直方图均衡(纯 MoonBit) - 7
- 8
///| - 9
/// CLAHE(Contrast Limited Adaptive Histogram Equalization)。 - 10
/// 将图像分成 tile_size × tile_size 的块,每块做直方图均衡,用双线性插值平滑拼接。 - 11
/// clip_limit 控制对比度增强强度,值越大增强越强(典型值 2.0~4.0)。 - 12
/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4),alpha 通道不变。 - 13
pub fn clahe( - 14
img : @types.Image, - 15
tile_size : Int32, - 16
clip_limit : Float, - 17
) -> @types.Image raise @types.LoadError { - 18
// 输入验证 - 19●
if img.width <= 0 || img.height <= 0 { - 20●
raise @types.LoadError::DecodeFailed("clahe: 图像尺寸必须大于0") - 21
} - 22
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 23●
raise @types.LoadError::DecodeFailed("clahe 仅支持 channels=1/3/4") - 24
} - 25
let expected_size = img.width * img.height * img.channels - 26●
if img.data.length() != expected_size { - 27○
raise @types.LoadError::DecodeFailed( - 28
"clahe: 数据大小不匹配,期望 " + - 29○
expected_size.to_string() + - 30
",实际 " + - 31○
img.data.length().to_string(), - 32
) - 33
} - 34
if tile_size < 2 { - 35●
raise @types.LoadError::DecodeFailed("tile_size 必须 >= 2") - 36
} - 37
if clip_limit <= 0.0 { - 38●
raise @types.LoadError::DecodeFailed("clip_limit 必须大于0") - 39
} - 40
let ch = img.channels - 41●
let color_ch = if ch >= 3 { 3 } else { 1 } - 42
let n = img.width * img.height - 43●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 44
for c in 0..<color_ch { - 45●
clahe_channel(img, out, c, ch, tile_size, clip_limit) - 46
} - 47
if ch == 4 { - 48●
for i in 0..<n { - 49●
out[i * 4 + 3] = img.data[i * 4 + 3] - 50
} - 51
} - 52
@types.Image::{ - 53
width: img.width, - 54
height: img.height, - 55
channels: ch, - 56●
data: Bytes::from_array(out), - 57
} - 58
} - 59
- 60
///| - 61
fn clahe_channel( - 62
img : @types.Image, - 63
out : Array[Byte], - 64
channel : Int32, - 65
ch : Int32, - 66
tile_size : Int32, - 67
clip_limit : Float, - 68
) -> Unit { - 69●
let w = img.width - 70
let h = img.height - 71
let n = w * h - 72
// 预计算 to_int 数组 - 73●
let data_int : Array[Int32] = Array::make(n * ch, 0) - 74
for i in 0..<(n * ch) { - 75●
data_int[i] = img.data[i].to_int() - 76
} - 77
// 块数 - 78
let ntx = (w + tile_size - 1) / tile_size - 79
let nty = (h + tile_size - 1) / tile_size - 80
let n_tiles = ntx * nty - 81●
let luts : Array[Array[Int32]] = Array::make(n_tiles, Array::make(256, 0)) - 82
// 计算每块的直方图和 LUT - 83
for ty in 0..<nty { - 84●
for tx in 0..<ntx { - 85●
let hist : Array[Int32] = Array::make(256, 0) - 86
let x0 = tx * tile_size - 87
let y0 = ty * tile_size - 88●
let x1 = if x0 + tile_size < w { x0 + tile_size } else { w } - 89●
let y1 = if y0 + tile_size < h { y0 + tile_size } else { h } - 90
let tile_pixels = (x1 - x0) * (y1 - y0) - 91
for y in y0..<y1 { - 92●
let row_base = y * w - 93
for x in x0..<x1 { - 94●
let v = data_int[(row_base + x) * ch + channel] - 95
hist[v] += 1 - 96
} - 97
} - 98
// 裁剪直方图 - 99●
let clip_count = (Float::from_int(tile_pixels) * clip_limit / 256.0).to_int() - 100
let mut excess = 0 - 101
for i in 0..<256 { - 102●
if hist[i] > clip_count { - 103●
excess += hist[i] - clip_count - 104
hist[i] = clip_count - 105
} - 106
} - 107
// 均匀分配多余的像素 - 108
let redistribute = excess / 256 - 109
let mut remainder = excess - redistribute * 256 - 110
for i in 0..<256 { - 111●
hist[i] += redistribute - 112
if remainder > 0 { - 113●
hist[i] += 1 - 114
remainder -= 1 - 115
} - 116
} - 117
// 计算 CDF 并归一化到 0-255 - 118
let lut = luts[ty * ntx + tx] - 119
let mut cdf = 0 - 120
for i in 0..<256 { - 121●
cdf += hist[i] - 122
lut[i] = if tile_pixels > 0 { - 123●
@types.clamp_int(cdf * 255 / tile_pixels, 0, 255) - 124
} else { - 125○
i - 126
} - 127
} - 128
} - 129
} - 130
// 预计算 tile 坐标和权重 - 131●
let inv_tile_size = 1.0F / Float::from_int(tile_size) - 132●
let tx_arr : Array[Int32] = Array::make(w, 0) - 133●
let wx_arr : Array[Float] = Array::make(w, 0.0F) - 134
for x in 0..<w { - 135●
let fx = Float::from_int(x) * inv_tile_size - 0.5F - 136●
let t = fx.to_int() - 137
tx_arr[x] = t - 138●
wx_arr[x] = fx - Float::from_int(t) - 139
} - 140●
let ty_arr : Array[Int32] = Array::make(h, 0) - 141●
let wy_arr : Array[Float] = Array::make(h, 0.0F) - 142
for y in 0..<h { - 143●
let fy = Float::from_int(y) * inv_tile_size - 0.5F - 144●
let t = fy.to_int() - 145
ty_arr[y] = t - 146●
wy_arr[y] = fy - Float::from_int(t) - 147
} - 148
// 双线性插值应用 LUT - 149
for y in 0..<h { - 150●
let ty = ty_arr[y] - 151
let wy = wy_arr[y] - 152
let row_base = y * w - 153
let wy_inv = 1.0F - wy - 154
for x in 0..<w { - 155●
let tx = tx_arr[x] - 156
let wx = wx_arr[x] - 157
let v = data_int[(row_base + x) * ch + channel] - 158
// 4 个相邻块 - 159●
let t00 = tile_clamp(tx, ty, ntx, nty) - 160●
let t01 = tile_clamp(tx + 1, ty, ntx, nty) - 161●
let t10 = tile_clamp(tx, ty + 1, ntx, nty) - 162●
let t11 = tile_clamp(tx + 1, ty + 1, ntx, nty) - 163●
let v00 = Float::from_int(luts[t00][v]) - 164●
let v01 = Float::from_int(luts[t01][v]) - 165●
let v10 = Float::from_int(luts[t10][v]) - 166●
let v11 = Float::from_int(luts[t11][v]) - 167
let wx_inv = 1.0F - wx - 168
let top = v00 * wx_inv + v01 * wx - 169
let bot = v10 * wx_inv + v11 * wx - 170●
let result = @types.clamp_int((top * wy_inv + bot * wy).to_int(), 0, 255) - 171●
out[(row_base + x) * ch + channel] = result.to_byte() - 172
} - 173
} - 174
} - 175
- 176
///| - 177
fn tile_clamp(tx : Int32, ty : Int32, ntx : Int32, nty : Int32) -> Int32 { - 178◐
let cx = if tx < 0 { 0 } else if tx >= ntx { ntx - 1 } else { tx } - 179◐
let cy = if ty < 0 { 0 } else if ty >= nty { nty - 1 } else { ty } - 180
cy * ntx + cx - 181
}