php imagick 像素迭代器与下一个和上一个像素进行比较

php imagick pixel iterator compare to next and previous pixels

$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) {

如何将此像素与上下像素和左右像素进行比较,我将使用我已经了解的 isSimilar

getNextIteratorRow/getPreviousIteratorRow 也许吧? 他们的像素相似吗?

}
    }
    $imageIterator->syncIterator(); 
}

这就是我如何从当前像素导航到上方、下方、左侧和右侧像素的问题。

非常感谢帮助。

这是最好通过编写一些代码来解决的问题之一。

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);
}

请注意,这将是非常低效的,也就是非常慢。它可能(但不保证)更快地实施您作为 FX 操作员尝试做的任何事情。 Example here and full documentation here.