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

时间:2015-06-04 00:15:57

标签: 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) {

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

getNextIteratorRow / getPreviousIteratorRow也许? 它们的像素类似吗?

}
    }
    $imageIterator->syncIterator(); 
}

这就是我如何从当前像素导航到上,下,左,右的像素。

非常感谢。

1 个答案:

答案 0 :(得分:0)

这是其中一个问题,可能只需编写一些代码就可以解决。

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和完整documentation here