离散余弦变换C#

时间:2013-06-20 23:32:33

标签: c# image-processing graphics dct

我制作了一个可以在C#中执行DCT的程序。

这是我的代码:

public void TransformToDCT(int xpos, int ypos)
{
    double[,] cof = new double[8, 8];
    int[,] rows = new int[8, 8];
    int c1=1004 /* cos(pi/16) << 10 */,
        s1=200 /* sin(pi/16) */,
        c3=851 /* cos(3pi/16) << 10 */,
        s3=569 /* sin(3pi/16) << 10 */,
        r2c6=554 /* sqrt(2)*cos(6pi/16) << 10 */,
        r2s6=1337 /* sqrt(2)*sin(6pi/16) << 10 */,
        r2=181; /* sqrt(2) << 7*/

    int x0,x1,x2,x3,x4,x5,x6,x7,x8;
    /* transform rows */
    for (int i = 0; i < 8; i++)
    {
        x0 = src.GetRoundPixel(xpos + 0, ypos + i);
        x1 = src.GetRoundPixel(xpos + 1, ypos + i);
        x2 = src.GetRoundPixel(xpos + 2, ypos + i);
        x3 = src.GetRoundPixel(xpos + 3, ypos + i);
        x4 = src.GetRoundPixel(xpos + 4, ypos + i);
        x5 = src.GetRoundPixel(xpos + 5, ypos + i);
        x6 = src.GetRoundPixel(xpos + 6, ypos + i);
        x7 = src.GetRoundPixel(xpos + 7, ypos + i);

        /* Stage 1 */
        x8 = x7 + x0;
        x0 -= x7;
        x7 = x1 + x6;
        x1 -= x6;
        x6 = x2 + x5;
        x2 -= x5;
        x5 = x3 + x4;
        x3 -= x4;

        /* Stage 2 */
        x4 = x8 + x5;
        x8 -= x5;
        x5 = x7 + x6;
        x7 -= x6;
        x6 = c1 * (x1 + x2);
        x2 = (-s1 - c1) * x2 + x6;
        x1 = (s1 - c1) * x1 + x6;
        x6 = c3 * (x0 + x3);
        x3 = (-s3 - c3) * x3 + x6;
        x0 = (s3 - c3) * x0 + x6;

        /* Stage 3 */
        x6 = x4 + x5;
        x4 -= x5;
        x5 = r2c6 * (x7 + x8);
        x7 = (-r2s6 - r2c6) * x7 + x5;
        x8 = (r2s6 - r2c6) * x8 + x5;
        x5 = x0 + x2;
        x0 -= x2;
        x2 = x3 + x1;
        x3 -= x1;

        /* Stage 4 and output */
        rows[i,0] = x6;
        rows[i,4] = x4;
        rows[i,2] = x8 >> 10;
        rows[i,6] = x7 >> 10;
        rows[i,7] = (x2 - x5) >> 10;
        rows[i,1] = (x2 + x5) >> 10;
        rows[i,3] = (x3 * r2) >> 17;
        rows[i,5] = (x0 * r2) >> 17;
    }

    /* transform columns */
    for (int i = 0; i < 8; i++)
    {
        x0 = rows[0,i];
        x1 = rows[1,i];
        x2 = rows[2,i];
        x3 = rows[3,i];
        x4 = rows[4,i];
        x5 = rows[5,i];
        x6 = rows[6,i];
        x7 = rows[7,i];

        /* Stage 1 */
        x8 = x7 + x0;
        x0 -= x7;
        x7 = x1 + x6;
        x1 -= x6;
        x6 = x2 + x5;
        x2 -= x5;
        x5 = x3 + x4;
        x3 -= x4;

        /* Stage 2 */
        x4 = x8 + x5;
        x8 -= x5;
        x5 = x7 + x6;
        x7 -= x6;
        x6 = c1 * (x1 + x2);
        x2 = (-s1 - c1) * x2 + x6;
        x1 = (s1 - c1) * x1 + x6;
        x6 = c3 * (x0 + x3);
        x3 = (-s3 - c3) * x3 + x6;
        x0 = (s3 - c3) * x0 + x6;

        /* Stage 3 */
        x6 = x4 + x5;
        x4 -= x5;
        x5 = r2c6 * (x7 + x8);
        x7 = (-r2s6 - r2c6) * x7 + x5;
        x8 = (r2s6 - r2c6) * x8 + x5;
        x5 = x0 + x2;
        x0 -= x2;
        x2 = x3 + x1;
        x3 -= x1;

        /* Stage 4 and output */
        cof[0, i] = (double)((x6 + 16) >> 3);
        cof[4, i] = (double)((x4 + 16) >> 3);
        cof[2, i] = (double)((x8 + 16384) >> 13);
        cof[6, i] = (double)((x7 + 16384) >> 13);
        cof[7, i] = (double)((x2 - x5 + 16384) >> 13);
        cof[1, i] = (double)((x2 + x5 + 16384) >> 13);
        cof[3, i] = (double)(((x3 >> 8) * r2 + 8192) >> 12);
        cof[5, i] = (double)(((x0 >> 8) * r2 + 8192) >> 12);


    }

    SetCoeff(cof, xpos, ypos);

    meanDC[blok] = cof[0, 0];
    if (cof[0, 0] > maxDC) maxDC = cof[0, 0];
    varian[blok] = Varian(cof);
    blok++;

}

这部分是为了获得c [0,0] DCT:

public double GetMeanDC(int Blok)
{
    return meanDC[Blok];
}

这部分是为了获得平均图像DC DCT系数:

public double GetMeanImage()
{
    double r = 0;
    for (int i = 0; i < totalBlok; i++)
    {
        r = r + meanDC[i];
    }
    r = r / (double)totalBlok;
    return r;
}

问题出在主程序中。 我想在这个等式中计算指数:x = exp( - ((miuN - miu)^ 2)),但我的程序总是返回0。

这是主程序中的代码:

miu = oriDCT.GetMeanImage();
miuN = oriDCT.GetMeanDC(blok) / oriDCT.GetMaxDC();
double deltamiu = (miuN - miu) * (miuN - miu);
double expon = (double)Math.Exp(-(deltamiu));

任何人都可以帮我解决这个问题吗? 谢谢!

0 个答案:

没有答案