3

I'm trying to find 2D DCT(The Discrete Cosine Transform) of an image using php. I have used the following equation to find it. enter image description here

enter image description here

enter image description here

Here is the code I have used to find the DCT.

function getGray($img,$x,$y){
    $col = imagecolorsforindex($img, imagecolorat($img,$x,$y));
    return intval($col['red']*0.3 + $col['green']*0.59 + $col['blue']*0.11);
}

function  initCoefficients() {
  for ($i=1;$i<$this->size;$i++) 
    {
    $c[$i]=1;
    }
    $c[0]=1/sqrt(2.0);
return $c;
}

function applyDCT($imagePath) {
    $img = $this->readImageTo($imagePath, 32, 32);
    $color=array();

    for($i=0;$i<32;$i++)
    {
        for($j=0;$j<32;$j++)
        {
            $color[]=$this->getGray($img,$i,$j);
        }
    }

    $N=32;


    $c=$this->initCoefficients();
    $sum=0;
    for ($u=0;$u<$N;$u++) {
    for ($v=0;$v<$N;$v++) {

        for ($i=0;$i<$N;$i++) {
            for ($j=0;$j<$N;$j++) {
                $sum += ($c[$i]*$c[$j])*cos(((2*$i+1)*$u*pi()/(2.0*$N)))*cos(((2*$j+1)*$v*pi()/(2.0*$N)))*($color[$i][$j]);
          }
        }

        $sum *=sqrt(2/$N)*sqrt(2/$N);
        $F[$u][$v] = $sum;
      }
    }
    return $F;
}

The image size is 32*32. My problem is, once I call the applyDCT()function it gives an array which has all the element values as 0.

eg:-

Array ( [0] => Array (... ,3 => 0 [4] => 0 [5] => 0,...

I think problem is in my calculation. What I am doing wrong? Please help me Thank you.

Tharu
  • 353
  • 1
  • 2
  • 11
  • shouldn't there be `$M` instead `$N` in second for loop and `sqrt(2/$N);` ? – viral Jul 03 '15 at 06:39
  • $M and $N are the same size(32). So, I hope it is not a problem – Tharu Jul 03 '15 at 06:44
  • $color is not good. You're initializing it in a 1D but you call it in your math operation as if it was a 2D array. On your math operation, replace $color[$i][$j] by $color[$i*32+$j] and it should work I guess... Or be sure that your $color array is formed as you expect, maybe by applying it a var_dump() call – niconoe Jul 03 '15 at 11:50
  • @niconoe I changed $color[]=$this->getGray($img,$i,$j) in applyDCT function into $color[$i][$j]=$this->getGray($img,$i,$j). Now it is giving some values. Why you said to replace $color[$i][$j] by $color[$i*32+$j] in math operation? Now also should I do this? – Tharu Jul 06 '15 at 12:26
  • Nope, this trick was only if you planned to keep $color as a one dimensional array. The $i*32+$j is a trick for conversion from 2D to 1D numerical indexes arrays (current line * nb columns + current column), but you don't need that anymore since your $color is now a 2D array ;). Do you want me to make an answer so you can accept it and solve your question? Regards, – niconoe Jul 06 '15 at 16:53
  • @niconoe Thank you. I got it – Tharu Jul 07 '15 at 04:45

1 Answers1

1

Here is the answer baseed on the comments:

$color is not good. You're initializing it in a 1D but you call it in your math operation as if it was a 2D array. On your math operation, replace $color[$i][$j] by $color[$i*32+$j] and it should work I guess... Or be sure that your $color array is formed as you expect

niconoe
  • 1,191
  • 1
  • 11
  • 25