Discrete cosine transform (DCT) of an image

user2827482 picture user2827482 · Sep 29, 2013 · Viewed 11k times · Source

I work on a function in Matlab that calculates the DCT (discrete cosine transform) of an image. I don't know what is not working in my code, but I got an output image with the same number. I want to use this formula for my DCT.

Any ideas please.

    function image_comp = dctII(image, b)
    [h w] = size(image);
    image = double(image) - 128;
    block = zeros(b,b);

 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

Answer

mikeTronix picture mikeTronix · Sep 29, 2013

In the inner loop over x and y, you are not reading from the correct place in image_t. You have copied the local block into a location with k,l as the upper left corner for use in processing, but in the inner loop you are always reading from the same block that starts at 1,1 as the upper left corner in image_t.