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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c