dotaer1993 2015-06-04 00:15 采纳率: 100%
浏览 32
已采纳

php imagick像素迭代器与下一个和上一个像素相比

$imagick = new \Imagick(realpath($imagePath));
$imageIterator = $imagick->getPixelIterator();

foreach ($imageIterator as $row => $pixels) { /* Loop through pixel rows */
    foreach ($pixels as $column => $pixel) { 
         if ($column % 2) {

How do I compare this pixel to the one above and below and the one to the left and right, I will be using isSimilar which I already understand

getNextIteratorRow/getPreviousIteratorRow maybe? Is their something similar for pixels?

}
    }
    $imageIterator->syncIterator(); 
}

That is the question how do I navigate from the current pixel to the one above, below, left and right.

Assistance much appreciated.

  • 写回答

1条回答 默认 最新

  • douchuose2514 2015-06-05 10:53
    关注

    This is one of those problems that is probably best solved by just writing some code.

    function processPixel($pixeLeft, $pixelMiddle, $pixelRight, $pixelAbove, $rowBelow)
    {
        //Whatever it is you're doing.
    }
    
    function processRow($rowAbove, $rowMiddle, $rowBelow)
    {
        for ($x = 0; $x < count($pixels); $x++) {
            $pixelAbove = null;
            $pixelBelow = null;
            $pixeLeft = null;
            $pixelRight = null;
            $pixelMiddle = $pixels[0];
    
            if (($x - 1) >= 0) {
                $pixelLeft = $pixels[$x - 1];
            }
    
            if (($x + 1) < count($pixels)) {
                $pixelRight = $pixels[$x + 1];
            }
    
            if ($rowAbove != null) {
                $pixelAbove = $rowAbove[$x];
            }
            if ($rowBelow != null) {
                $rowBelow = $rowBelow[$x];
            }
    
            processPixel($pixeLeft, $pixelMiddle, $pixelRight, $pixelAbove, $rowBelow)
        }
    }
    
    function processImageIterator($imageIterator)
    {
        $rowAbove = null;
        $rowMiddle = null;
        $rowBelow = null;
    
        foreach ($imageIterator as $rowPixels) {
    
            $rowBelow = $rowPixels;
    
            foreach ($imageIterator as $rowPixels) {
                processRow($rowAbove, $rowMiddle, $rowBelow);
            }
    
            $rowAbove = $rowMiddle;
            $rowMiddle = $rowBelow;
        }
    
        // Finish last row with middle on the last row
        processRow($rowAbove, $rowMiddle, null);
    
        //Finish row of pixel below last row. This may not be necessary
        //depending on your algorithm,
        $rowAbove = $rowMiddle;
        processRow($rowAbove, null, null);
    }
    

    Just to note, this will be highly non-performant, aka slow as heck. It may (but not guaranteed) be faster to implement whatever it is you're trying to do as an FX operator. Example here and full documentation here.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测