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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题