image.process\frequency\dct.mbt
90.7% · 78 / 86 points · 188 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 离散余弦变换 dct - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// DCT (离散余弦变换) 公共 API:dct_2d + idct_2d - 7
// 2D DCT-II 和 IDCT-III,用于频域分析和图像压缩 - 8
// 优化:预计算余弦表 + 可分离 1D DCT,O(N³) 而非 O(N⁴) - 9
- 10
///| - 11
/// 预计算 DCT 余弦表:cos_table[m][k] = cos((2*k+1)*m*π/(2*N)) - 12
/// 注意索引顺序:第一维是频率 m,第二维是空间 k - 13
fn dct_cos_table(n : Int32) -> Array[Array[Float]] { - 14●
let pi = 3.14159265358979F - 15●
let nf = Float::from_int(n) - 16●
let table : Array[Array[Float]] = Array::make(n, []) - 17
for m in 0..<n { - 18●
let row : Array[Float] = Array::make(n, 0.0F) - 19●
let mf = Float::from_int(m) - 20
for k in 0..<n { - 21●
let kf = Float::from_int(2 * k + 1) - 22●
row[k] = @math.cosf(pi * kf * mf / (2.0F * nf)) - 23
} - 24
table[m] = row - 25
} - 26
table - 27
} - 28
- 29
///| - 30
/// 预计算 DCT 归一化系数:C(0)=sqrt(1/N), C(k)=sqrt(2/N) - 31
fn dct_coeffs(n : Int32) -> Array[Float] { - 32●
let nf = Float::from_int(n) - 33●
let coeffs : Array[Float] = Array::make(n, 0.0F) - 34
for i in 0..<n { - 35●
coeffs[i] = if i == 0 { (1.0F / nf).sqrt() } else { (2.0F / nf).sqrt() } - 36
} - 37
coeffs - 38
} - 39
- 40
///| - 41
/// 1D DCT-II 正变换:对长度 n 的行向量执行 DCT - 42
/// 输入 src[0..n),输出 dst[0..n) - 43
/// 余弦查找表与系数 cos_table[k][n] = cos((2*k+1)*n*π/(2*N)), coeffs[n] = C(n) - 44
fn dct_1d( - 45
src : ArrayView[Float], - 46
dst : Array[Float], - 47
cos_table : Array[Array[Float]], - 48
coeffs : ArrayView[Float], - 49
n : Int32, - 50
) -> Unit { - 51●
for m in 0..<n { - 52●
let mut sum = 0.0F - 53
let cos_m = cos_table[m] - 54
for k in 0..<n { - 55●
sum += src[k] * cos_m[k] - 56
} - 57
dst[m] = coeffs[m] * sum - 58
} - 59
} - 60
- 61
///| - 62
/// 1D IDCT-III 逆变换:对长度 n 的频域向量执行 IDCT - 63
/// 输入 src[0..n)(频域),输出 dst[0..n)(空间域) - 64
/// 公式:dst[k] = sum_m C(m) * src[m] * cos((2*k+1)*m*π/(2*N)) - 65
fn idct_1d( - 66
src : ArrayView[Float], - 67
dst : Array[Float], - 68
cos_table : Array[Array[Float]], - 69
coeffs : ArrayView[Float], - 70
n : Int32, - 71
) -> Unit { - 72●
for k in 0..<n { - 73●
let mut sum = 0.0F - 74
for m in 0..<n { - 75●
sum += coeffs[m] * src[m] * cos_table[m][k] - 76
} - 77
dst[k] = sum - 78
} - 79
} - 80
- 81
///| - 82
/// 2D DCT-II。将灰度图变换为 DCT 系数矩阵。 - 83
/// 输入图像转为灰度,输出 N×M 的系数矩阵。 - 84
/// 优化:可分离 1D DCT(先行变换再列变换),O(N³) 而非 O(N⁴) - 85
pub fn dct_2d(img : @types.Image) -> Array[Array[Double]] { - 86●
let ch = img.channels - 87
let w = img.width - 88
let h = img.height - 89
let n = w * h - 90
// 提取灰度像素数据 - 91●
let src : Array[Float] = Array::make(n, 0.0F) - 92
for i in 0..<n { - 93●
let v = if ch == 1 { - 94●
Float::from_int(img.data[i].to_int()) - 95
} else { - 96○
let r = Float::from_int(img.data[i * ch].to_int()) - 97○
let g = Float::from_int(img.data[i * ch + 1].to_int()) - 98○
let b = Float::from_int(img.data[i * ch + 2].to_int()) - 99
r * 0.299F + g * 0.587F + b * 0.114F - 100
} - 101
src[i] = v - 102
} - 103
// 预计算余弦表和归一化系数 - 104●
let cos_w = dct_cos_table(w) - 105●
let cos_h = dct_cos_table(h) - 106●
let coeffs_w = dct_coeffs(w) - 107●
let coeffs_h = dct_coeffs(h) - 108
// 第一步:对每一行执行 1D DCT(宽度方向) - 109
// row_dct[y][u] = C_w(u) * sum_x src[y][x] * cos((2x+1)uπ/(2W)) - 110●
let row_dct : Array[Array[Float]] = Array::make(h, []) - 111●
let row_buf : Array[Float] = Array::make(w, 0.0F) - 112
for y in 0..<h { - 113●
let row_src : Array[Float] = Array::make(w, 0.0F) - 114
for x in 0..<w { - 115●
row_src[x] = src[y * w + x] - 116
} - 117●
dct_1d(row_src[:], row_buf, cos_w, coeffs_w[:], w) - 118●
row_dct[y] = Array::make(w, 0.0F) - 119
for u in 0..<w { - 120●
row_dct[y][u] = row_buf[u] - 121
} - 122
} - 123
// 第二步:对每一列执行 1D DCT(高度方向) - 124
// F(v,u) = C_h(v) * sum_y row_dct[y][u] * cos((2y+1)vπ/(2H)) - 125●
let result : Array[Array[Double]] = Array::make(h, []) - 126●
let col_buf : Array[Float] = Array::make(h, 0.0F) - 127
for u in 0..<w { - 128●
let col_src : Array[Float] = Array::make(h, 0.0F) - 129
for y in 0..<h { - 130●
col_src[y] = row_dct[y][u] - 131
} - 132●
dct_1d(col_src[:], col_buf, cos_h, coeffs_h[:], h) - 133
for v in 0..<h { - 134●
if result[v].length() == 0 { - 135●
result[v] = Array::make(w, 0.0) - 136
} - 137●
result[v][u] = col_buf[v].to_double() - 138
} - 139
} - 140
result - 141
} - 142
- 143
///| - 144
/// 2D IDCT-III。将 DCT 系数矩阵逆变换为灰度图。 - 145
/// 优化:可分离 1D IDCT(先列变换再行变换),O(N³) 而非 O(N⁴) - 146
pub fn idct_2d(coeffs : Array[Array[Double]]) -> @types.Image { - 147●
let h = coeffs.length() - 148◐
let w = if h == 0 { 0 } else { coeffs[0].length() } - 149
// 预计算余弦表和归一化系数 - 150●
let cos_w = dct_cos_table(w) - 151●
let cos_h = dct_cos_table(h) - 152●
let coeffs_w = dct_coeffs(w) - 153●
let coeffs_h = dct_coeffs(h) - 154
// 第一步:对每一列执行 1D IDCT(高度方向) - 155
// col_idct[y][u] = sum_v C_h(v) * coeffs[v][u] * cos((2y+1)vπ/(2H)) - 156●
let col_idct : Array[Array[Float]] = Array::make(h, []) - 157●
let col_buf : Array[Float] = Array::make(h, 0.0F) - 158
for u in 0..<w { - 159●
let col_src : Array[Float] = Array::make(h, 0.0F) - 160
for v in 0..<h { - 161●
col_src[v] = Float::from_double(coeffs[v][u]) - 162
} - 163●
idct_1d(col_src[:], col_buf, cos_h, coeffs_h[:], h) - 164
for y in 0..<h { - 165●
if col_idct[y].length() == 0 { - 166●
col_idct[y] = Array::make(w, 0.0F) - 167
} - 168
col_idct[y][u] = col_buf[y] - 169
} - 170
} - 171
// 第二步:对每一行执行 1D IDCT(宽度方向) - 172
// f(x,y) = sum_u C_w(u) * col_idct[y][u] * cos((2x+1)uπ/(2W)) - 173●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 174●
let row_buf : Array[Float] = Array::make(w, 0.0F) - 175
for y in 0..<h { - 176●
idct_1d(col_idct[y][:], row_buf, cos_w, coeffs_w[:], w) - 177
for x in 0..<w { - 178●
let val = (row_buf[x] + 0.5F).to_int().clamp(min=0, max=255) - 179●
out[y * w + x] = val.to_byte() - 180
} - 181
} - 182
@types.Image::{ - 183
width: w, - 184
height: h, - 185
channels: 1, - 186●
data: Bytes::from_array(out), - 187
} - 188
}