dongtang1997 2015-07-03 06:36
浏览 95
已采纳

如何使用php计算图像的2D DCT?

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.

  • 写回答

1条回答 默认 最新

  • doucan1979 2015-07-07 08:33
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器