0

I'm working on a project where I have to use DCT and inverse DCT on image for jpeg compression.I have posted below the program I am unable to find out the error in DCT and IDCT.

clc;close all;clear all;
image=imread('cameraman.tif');
    [h w] = size(image);
    image = double(image) - 128;
    block = zeros(b,b);
    b=8;
 image_t=zeros(size(image));
 for k=1:b:h
     for l=1:b:w
        image_t(k:k+b-1,l:l+b-1)= image(k:k+b-1,l:l+b-1);
        for u=1:b
            for v=1:b
                if u == 0
                    Cu = 1/sqrt(2);
                else
                    Cu = 1;
                end
                if v == 0
                    Cv = 1/sqrt(2);
                else
                    Cv = 1;
                end
                Res_sum=0;
                for x=1:b;
                    for y=1:b
                        Res_sum = Res_sum + ((image_t(x,y))*cos(((2*x)+1)*u*pi/(2*b))*cos(((2*y)+1)*v*pi/(2*b)));  
                    end
                end
                dct= (1/4)*Cu*Cv*Res_sum;
                block(u,v) = dct;

            end
        end
        image_comp(k:k+b-1,l:l+b-1)=block(u,v);
     end
 end
end

Inverse DCT program

for k=1:b:h
    for l=1:b:w
        for x=1:b
            for y=1:b
                Res_sum=0;
                for u=1:b
                    for v=1:b
                        if k==1
                             temp=double(sqrt(1/2)*qz(u,v)*cos(pi*(k-1)*(2*i-1)/(2*b))*cos(pi*(l-1)*(2*j-1)/(2*b));
                        else
                            temp=double(qz(u,v)*cos(pi*(k-1)*(2*i-1)/(2*b))*cos(pi*(l-1)*(2*j-1)/(2*b));
                        end
                        if l==1
                            temp=temp*sqrt(1/2);
                        end
                       Res_sum=Res_sum+temp;
                    end
                end
                Res_sum=Res_sum*(2/sqrt(b*b));
                image_t((k-1)*b+i,(l-1)*b+j)=Res_sum;
            end
        end
    end
end

where qz is after quantization. Thanks in advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
P.Bisoyi
  • 123
  • 1
  • 2
  • 10

0 Answers0