Java 关于在图像上实现逆DCT的问题
我试图在图像上实现DCT,作为理解整个JPEG压缩管道(Java)的一部分。我可以成功地实现前向DCT。然而,我在反向DCT中面临着一些问题。我们将非常感谢您的帮助Java 关于在图像上实现逆DCT的问题,java,image-processing,dct,Java,Image Processing,Dct,我试图在图像上实现DCT,作为理解整个JPEG压缩管道(Java)的一部分。我可以成功地实现前向DCT。然而,我在反向DCT中面临着一些问题。我们将非常感谢您的帮助 public BufferedImage Inverse_DCT (BufferedImage img) { int width = img.getWidth(); int height = img.getHeight(); BufferedImage OutputImage = new BufferedIm
public BufferedImage Inverse_DCT (BufferedImage img)
{
int width = img.getWidth();
int height = img.getHeight();
BufferedImage OutputImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// ------------------- IDCT Implementation ------------------------------
double [][]Cos_Basis = new double [8][8]; // 8X8 Cosine Basis Implementation
double [] CoEff = new double [8];
// ------------------ Pre Compute The Kernel ---------------------------
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
Cos_Basis[i][j] = Math.cos((2*i+1)*j*3.14159f/16.0f); // COSINE KERNEL
}
if (i==0)
CoEff[i]=1/(Math.sqrt(2));
else
CoEff[i]=1;
}
// -------------------- IDCT Code --------------------------------------
for (int row=0;row<(height/8);row++)
{
for (int col=0;col<(width/8);col++)
{
for (int i=0;i<8;i++) // Block Row
{
for (int j=0;j<8;j++) // Block Col
{
double sumR = 0;double sumG = 0;double sumB = 0;
for (int x=0;x<8;x++)
{
for (int y=0;y<8;y++)
{
sumR += (CoEff[x])*(CoEff[y])*((((img.getRGB((col*8+y),(row*8+x))>>16) & 0xFF))*(Cos_Basis[i][x])*(Cos_Basis[j][y]));
sumG += (CoEff[x])*(CoEff[y])*((((img.getRGB((col*8+y),(row*8+x))>>8) & 0xFF))*(Cos_Basis[i][x])*(Cos_Basis[j][y]));
sumB += (CoEff[x])*(CoEff[y])*((((img.getRGB((col*8+y),(row*8+x))>>0) & 0xFF))*(Cos_Basis[i][x])*(Cos_Basis[j][y]));
}
}
sumR *= 0.25f; sumR += 128;
sumG *= 0.25f; sumG += 128;
sumB *= 0.25f; sumB += 128;
if (sumR<0) sumR=0; if (sumG<0) sumG=0;if (sumB<0) sumB=0;
if (sumR>255) sumR=255; if (sumG>255) sumG=255;if (sumB>255) sumB=255;
//System.out.println("SumR : "+sumR+" SumG:"+sumG+" SumB:"+sumB);
// Assign Output Image
int pix = 0x00000000 | (((int)(sumR) & 0xff) << 16) | (((int)(sumG) & 0xff) << 8) | ((int)(sumB) & 0xff);
//System.out.println("PIXEL : "+pix);
OutputImage.setRGB((col*8+j),(row*8+i),pix);
}
}
}
}
return OutputImage;
}
public BufferedImage Inverse\u DCT(BufferedImage img)
{
int width=img.getWidth();
int height=img.getHeight();
BuffereImage OutputImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
//------------------IDCT的实施------------------------------
double[]Cos_基=新的double[8][8];//8X8余弦基实现
双[]系数=新双[8];
//---------预计算内核---------------------------
对于(int i=0;i问题在我发布后几个小时由我解决,我通过以下方式修复了此问题;现在它工作正常
public BufferedImage Inverse_DCT (BufferedImage img){
int width = img.getWidth();
int height = img.getHeight();
BufferedImage OutputImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// ------------------- IDCT Implementation ------------------------------
double [][]Cos_Basis = new double [8][8]; // 8X8 Cosine Basis Implementation
//double [] CoEff = new double [8];
double Cu,Cv;
// ------------------ Pre Compute The Kernel ---------------------------
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
Cos_Basis[i][j] = Math.cos((2*i+1)*j*3.14159f/16.0f); // COSINE KERNEL
}
//if (i==0)
// CoEff[i]=1/(Math.sqrt(2));
//else
// CoEff[i]=1;
}
// -------------------- IDCT Code --------------------------------------
for (int row=0;row<(height);row+=8)
{
for (int col=0;col<(width);col+=8)
{
for (int i=0;i<8;i++) // Block Row
{
for (int j=0;j<8;j++) // Block Col
{
double sumR = 0;double sumG = 0;double sumB = 0;
for (int x=0;x<8;x++)
{
for (int y=0;y<8;y++)
{
if (x==0) {Cu = (1/(Math.sqrt(2)));}
else { Cu = 1;}
if (y==0) {Cv = (1/(Math.sqrt(2)));}
else { Cv = 1;}
sumR += (Cu)*(Cv)*((((img.getRGB((col+y),(row+x))>>16) & 0xFF))*(Cos_Basis[i][x])*(Cos_Basis[j][y]));
sumG += (Cu)*(Cv)*((((img.getRGB((col+y),(row+x))>>8) & 0xFF))*(Cos_Basis[i][x])*(Cos_Basis[j][y]));
sumB += (Cu)*(Cv)*((((img.getRGB((col+y),(row+x))>>0) & 0xFF))*(Cos_Basis[i][x])*(Cos_Basis[j][y]));
}
}
sumR *= 0.25f;
sumG *= 0.25f;
sumB *= 0.25f;
if (sumR<0) sumR=0; if (sumG<0) sumG=0;if (sumB<0) sumB=0;
if (sumR>255) sumR=255; if (sumG>255) sumG=255;if (sumB>255) sumB=255;
//System.out.println("SumR : "+sumR+" SumG:"+sumG+" SumB:"+sumB);
// Assign Output Image
int pix = 0xff000000 | (((int)(sumR) & 0xff) << 16) | (((int)(sumG) & 0xff) << 8) | ((int)(sumB) & 0xff);
//System.out.println("PIXEL : "+pix);
OutputImage.setRGB((col+j),(row+i),pix);
}
}
}
}
return OutputImage;
}
public BufferedImage Inverse\u DCT(BufferedImage img){
int width=img.getWidth();
int height=img.getHeight();
BuffereImage OutputImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
//------------------IDCT的实施------------------------------
double[]Cos_基=新的double[8][8];//8X8余弦基实现
//双[]系数=新双[8];
双Cu,Cv;
//---------预计算内核---------------------------
对于(int i=0;i