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.
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.