Search code examples
phpimage-processinggdsobel

What is wrong with this code on Sobel filter?


I am trying to implement a sobel filter in PHP GD but something is wrong with my code:

$gd = imagecreatefrompng('base.png');
$width = imagesx($gd);
$height = imagesx($gd);

for($i=1; $i<$width-1;$i++){
    for($j=1;$j<$height-1; $j++){
        $pixelMatrix[0][0]= getColor($gd, $i-1,$j-1);
        $pixelMatrix[0][1]= getColor($gd, $i-1,$j);
        $pixelMatrix[0][2]= getColor($gd, $i-1,$j+1);
        $pixelMatrix[1][0]= getColor($gd, $i,$j-1);
        $pixelMatrix[1][2]= getColor($gd, $i,$j+1);
        $pixelMatrix[2][0]= getColor($gd, $i+1,$j-1);
        $pixelMatrix[2][1]= getColor($gd, $i+1,$j);
        $pixelMatrix[2][2]= getColor($gd, $i+1,$j+1);

        $edge=(int) convolution($pixelMatrix);
        if($edge>255) $edge = 255;
        imagesetpixel($gd, $i, $j, imagecolorallocate($gd,$edge,$edge,$edge));
    }

}

function getColor($gd, $x, $y){
    $rgb = @imagecolorat($gd, $x, $y);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    return round($r * 0.3 + $g * 0.59 + $b * 0.11); // gray
}

function convolution($pixelMatrix){

    $gy=($pixelMatrix[0][0]*-1)+($pixelMatrix[0][1]*-2)+($pixelMatrix[0][2]*-1)+($pixelMatrix[2][0])+($pixelMatrix[2][1]*2)+($pixelMatrix[2][2]*1);
    $gx=($pixelMatrix[0][0])+($pixelMatrix[0][2]*-1)+($pixelMatrix[1][0]*2)+($pixelMatrix[1][2]*-2)+($pixelMatrix[2][0])+($pixelMatrix[2][2]*-1);
    return sqrt(pow($gy,2)+pow($gx,2));

}


// send PNG to browser
header("Content-type: image/png");
imagepng($gd);

Base image:

enter image description here

Correct result image:

enter image description here

My result:

enter image description here

For this image the edge contains an int from 0-990 so I limit it to 255. If i remove the cap all I get is noise. Im guessing the error is in translating the edge into RGB values (I dont understand that part) in imagesetpixel, no?


Solution

  • There are a few things wrong, as marked up in comments, but the bulk of your code is pretty correct.

    Main things are:

    • Input image may be palettised, so make true colour
    • Cannot do Sobel in place - you need output image
    • You have a typo where obtaining the height

    And that is mainly it, I think!

    $gd = imagecreatefrompng('base.png');
    imagepalettetotruecolor($gd);        // IN CASE PALETTISED
    $width = imagesx($gd);
    $height = imagesy($gd);              // NOT imagesx()
    $result=imagecreatetruecolor($width,$height); // CREATE OUTPUT IMAGE
    
    for($i=1; $i<$width-1;$i++){
        for($j=1;$j<$height-1; $j++){
            $pixelMatrix[0][0]= getColor($gd, $i-1,$j-1);
            $pixelMatrix[0][1]= getColor($gd, $i-1,$j);
            $pixelMatrix[0][2]= getColor($gd, $i-1,$j+1);
            $pixelMatrix[1][0]= getColor($gd, $i,$j-1);
            $pixelMatrix[1][2]= getColor($gd, $i,$j+1);
            $pixelMatrix[2][0]= getColor($gd, $i+1,$j-1);
            $pixelMatrix[2][1]= getColor($gd, $i+1,$j);
            $pixelMatrix[2][2]= getColor($gd, $i+1,$j+1);
    
            $edge=(int) convolution($pixelMatrix);
            if($edge>255) $edge = 255;
            imagesetpixel($result, $i, $j, imagecolorallocate($result,$edge,$edge,$edge));
        }
    }
    
    imagepng($result,"result.png");
    
    function getColor($gd, $x, $y){
        $rgb = @imagecolorat($gd, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        return round($r * 0.3 + $g * 0.59 + $b * 0.11); // gray
    }
    
    function convolution($pixelMatrix){
    
        $gy=($pixelMatrix[0][0]*-1)+($pixelMatrix[0][1]*-2)+($pixelMatrix[0][2]*-1)+($pixelMatrix[2][0])+($pixelMatrix[2][1]*2)+($pixelMatrix[2][2]*1);
        $gx=($pixelMatrix[0][0])+($pixelMatrix[0][2]*-1)+($pixelMatrix[1][0]*2)+($pixelMatrix[1][2]*-2)+($pixelMatrix[2][0])+($pixelMatrix[2][2]*-1);
        return sqrt(pow($gy,2)+pow($gx,2));
    
    }
    

    enter image description here