Chris Tine
Chris Tine

Reputation: 21

DCT equation in openCV

I write JPEG compression in Scilab (equivalent of MATLAB) using function imdct. In this function is used function DCT from openCV and I don't know which equation is used in dct function.

lenna by imdct

lenna by my_function

You can see lenna by imdct which is internal function and lenna by my_function is my function in scilab.

I add my code in scilab

function vystup = dct_rovnice(vstup)

[M,N] = size(vstup) 

 for u=1:M 
     for v=1:N
        cos_celkem = 0; 

        for m=1:M 
            for n=1:N 
                pom = double(vstup(m,n)); 
                cos_citatel1 = cos(((2*m) * u * %pi)/(2*M));
                cos_citatel2 = cos(((2*n) * v * %pi)/(2*N));
                cos_celkem = cos_celkem + (pom * cos_citatel1 * cos_citatel2);
            end
        end

        c_u = 0;
        c_v = 0;

        if u == 1 then 
            c_u = 1 / sqrt(2);
        else
            c_u = 1;
        end  

        if v == 1 then 
            c_v = 1 / sqrt(2);
        else
            c_v = 1;
        end  

        vystup(u,v) = (2/sqrt(n*m)) * c_u * c_v * cos_celkem; 
    end
end

endfunction

function vystup = dct_prevod(vstup)

Y = vstup(:,:,1); 

Cb = vstup(:,:,2);

Cr = vstup(:,:,3);

[rows,columns]=size(vstup)

vystup = zeros(rows,columns,3)


for y=1:8:rows-7
    for x=1:8:columns-7 
        blok_Y = Y(y:y+7,x:x+7) 
        blok_Cb = Cb(y:y+7,x:x+7) 
        blok_Cr = Cr(y:y+7,x:x+7) 
        blok_dct_Y = dct_rovnice(blok_Y) 
        blok_dct_Cb = dct_rovnice(blok_Cb)  
        blok_dct_Cr = dct_rovnice(blok_Cr) 
        vystup(y:y+7,x:x+7,1)= blok_dct_Y 
        vystup(y:y+7,x:x+7,2)= blok_dct_Cb
        vystup(y:y+7,x:x+7,3)= blok_dct_Cr 
    end
end
vystup = uint8(vystup) 
endfunction

You can see equation I used EQUATION

Upvotes: 1

Views: 360

Answers (2)

DaBler
DaBler

Reputation: 2852

The issue seems to be in the use of different normalization of the resulting coefficients.

The OpenCV library uses this equation for a forward transform (N=8, in your case):

forward transform

The basis g is defined as

transform basis

where

lambda

(Sorry for the ugly images, but SO does not provide any support for typesetting equations.)

Upvotes: 1

user5694329
user5694329

Reputation: 412

Take care there are several definitions of the dct function (DCT-I, DCT-II, DCT-III and DCT-IV normalized and un-normmalized)

Moreover have you tried the Scilab builtin function dct (from FFTW) which can be applied straightforward to images.

Upvotes: 0

Related Questions