PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] What is wrong with this code on Sobel filter?

 October 10, 2022     gd, image-processing, php, sobel     No comments   

Issue

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



Answered By - Mark Setchell
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing