所以我正在尝试使用这个 https://www.nayuki.io/res/fast-discrete-cosine-transform-algorithms/NaiveDct.cs 在我的图像处理作业中,我们应该对图片应用 DCT。 (8x8 块)
static public double[,] Dct(double[,] array)
{
double[,] dct = new double[N, M];
double factor = Math.PI / (N * M);
//dct
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
double sum = 0;
for (int k = 0; k < M; k++)
{
for (int l = 0; l < N; l++)
{
sum += array[k, l] * Math.Cos((k * M + l + 0.5) * (i * M + j) * factor);
}
}
dct[i, j] = (int) sum;
}
}
return dct;
}
但尝试恢复“色块”(在本例中,它用于 rgb 颜色的“蓝色”值)。 使用此代码:
static public double[,] inverseDct(double[,] array)
{
double[,] colorBlock = new double[N, M];
double factor = Math.PI / (N * M);
//dct
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
double sum = array[0, 0] / 2;
for (int k = 0; k < M; k++)
{
int l = 0;
if (k == 0) l = 1;
for (; l < N; l++)
{
sum += array[k, l] * Math.Cos((k * M + l) * ((i * M + j) + 0.5) * factor);
}
}
colorBlock[i, j] = (int) sum;
}
}
return colorBlock;
}
它似乎无法正常工作,因为(上图中的蓝色值块)的输出现在是这样的:
我在这里发布了我的整个代码: https://pastebin.com/XbCE3kBK
如何从 DCT 块取回我的“color”值? (做逆变换?)
[DCT(u,v)] = [CC(i,j)] * EE{i=0..M-1;j=0..N-1} ( [INPUT(i,j)] * [cosinus1(u,v,i,j)] )
[INPUT(i,j)] = * EE{u=0..M-1;v=0..N-1} ( [CC(u,v)] * [DCT(u,v)] * [cosinus2(i,j,u,v)] )
CC = (2/M)^0,5 * (2/N)^0,5 * C(i)*C(j) ~ (2/M)^0,5 * (2/N)^0,5 * C(u)*C(v)
i=0 -> C(i)=1/2 | i>0 ->; C(i)=1
j=0 -> C(j)=1/2 | j>0 -> C(j)=1 (i,j | u,v)
[cosinus1(u,v,i,j)] = cos(PI/(2M)*(2i+1)*u) * cos(PI/(2N)*(2j+1)*v)
[cosinus2(i,j,u,v)] = cos(PI/(2M)*(2u+1)*i) * cos(PI/(2N)*(2v+1)*j)
Constant CC (M = 8, N = 8)
i=0, j=0 : CC(i,j) = (1/M)^0.5 * (1/M)^0.5 = 0,125
i=0, j>0 : CC(i,j) = (1/M)^0.5 * (2/M)^0.5 = 0,177
i>0, j=0 : CC(i,j) = (2/M)^0.5 * (1/M)^0.5 = 0,177
i>0, j>0 : CC(i,j) = (2/M)^0.5 * (2/M)^0.5 = 0,25
另请参阅: