gpt4 book ai didi

在我的 DCT/IDCT 代码中找不到错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:46 29 4
gpt4 key购买 nike

我正在对 512 * 512 原始图像进行 DCT/IDCT,

但是我的程序可以输出只有 PSNR 17.54db 的图像,

我已经多次检查我的公式,有人可以帮我解决这个问题吗?

This is original picture

After IDCT

#include <stdio.h>
#include <math.h>
#define N 512

double COS[8][8], C[8];
unsigned char pic[N][N];
double dct[N][N], idct[N][N];

void init() {
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++)
COS[i][j] = cos((2 * i + 1) * j * acos(-1) / 16.0);
if (i) C[i] = 1;
else C[i] = 1 / sqrt(2);
}
}

void DCT() {
freopen("dct.raw", "wb", stdout);
int r, c, i, j, x, y;
for (r = 0; r < 64; r++)
for (c = 0; c < 64; c++)
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++) {
double sum = 0;
for (x = 0; x < 8; x++)
for (y = 0; y < 8; y++)
sum += (pic[r * 8 + x][c * 8 + y] - 128) * COS[x][i] * COS[y][j];
sum *= C[i] * C[j] * 0.25;
if (sum < 0) sum = 0;
if (sum > 255) sum = 255;
dct[r * 8 + i][c * 8 + j] = sum;
}
for (r = 0; r < N; r++)
for (c = 0; c < N; c++)
putchar(dct[r][c]);
}

void IDCT() {
freopen("idct.raw", "wb", stdout);
int r, c, i, j, x, y;
for (r = 0; r < 64; r++)
for (c = 0; c < 64; c++)
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++) {
double sum = 0;
for (x = 0; x < 8; x++)
for (y = 0; y < 8; y++)
sum += C[x] * C[y] * dct[r * 8 + x][c * 8 + y] * COS[i][x] * COS[j][y];
sum *= 0.25;
sum += 128;
if (sum < 0) sum = 0;
if (sum > 255) sum = 255;
idct[r * 8 + i][c * 8 + j] = sum;
}
for (r = 0; r < N; r++)
for (c = 0; c < N; c++)
putchar(idct[r][c]);
}

int main() {
freopen("Lena.raw", "rb", stdin);
int r, c;
for (r = 0; r < N; r++)
for (c = 0; c < N; c++)
scanf("%c", &pic[r][c]);
init();
DCT();
IDCT();
return 0;
}

最佳答案

问题是 DCT 代码中的这些行

      if (sum < 0) sum = 0;
if (sum > 255) sum = 255;

您将 DCT 值限制为 0 <= 值 <= 255

他们可能不是。例如,会有负系数。尝试删除这些行,看看是否有帮助

关于在我的 DCT/IDCT 代码中找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776068/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com