简体   繁体   中英

PHP Erode Image - NOT IMAGEMAGICK

Is here any ERODE (name from Paint Shop Pro) filter implementation in PHP/GDLIB? I was trying to find one, but no luck... in fact even ImageMagick doesn't have that filter...

I mean filter that converts this:

在此处输入图片说明

To this:

在此处输入图片说明

Other example to show that blur is not the same

在此处输入图片说明

Please note , that those are not manually generated images - they are live examples of ERODE filter in Paint Shop Pro 9

I had not seen this function until this question. I don't see it in ImageMagick (and it certainly is not a built-in PHP function). I liked what it did, so I wrote a couple functions that produce the same effect. This only works in a one-pixel radius. I have tried to write this as verbose as possible to make it easy for someone else to customize the code to his/her needs.

// Takes an image, (x,y) coordinate, and returns the luminosity
function imageluminosity(&$img, $x, $y)
{
    $rgb = imagecolorat($img, $x, $y);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8 ) 0xFF;
    $b = $rgb & 0xFF;
    // These values are based on standard pixel brightness per color.
    // 0.299+0.587+0.114 = 1
    return 0.299*$r + 0.587*$g + 0.114*$b;
}

// Degrades an image by enhancing the darker areas of the image.
// Does not alter the original image.
// Returns an eroded image.
// This works on EVERY pixel, so it takes a looooong time.
function imagedegrade(&$oimg)
{
    $img = imagecreatetruecolor(imagesx($oimg), imagesy($oimg));
    for($x=0; $x<imagesx($oimg); $x++)
    {
        for($y=0; $y<imagesy($oimg); $y++)
        {
            //Find the most luminosity in any neighboring pixel
            $olum = $blum = imageluminosity($oimg, $x, $y);
            if($x>0)
            {
                if($y>0)
                {
                    $tlum = imageluminosity($oimg, $x-1, $y-1);
                    if($tlum > $lum) $blum = $tlum;
                }
                $tlum = imageluminosity($oimg, $x-1, $y);
                if($tlum > $lum) $blum = $tlum;
                if($y<imagesy($oimg)-1)
                {
                    $tlum = imageluminosity($oimg, $x-1, $y+1);
                    if($tlum > $lum) $blum = $tlum;
                }
            }
            if($y>0)
            {
                $tlum = imageluminosity($oimg, $x1, $y-1);
                if($tlum > $lum) $blum = $tlum;
            }
            if($y<imagesy($oimg)-1)
            {
                $tlum = imageluminosity($oimg, $x1, $y+1);
                if($tlum > $lum) $blum = $tlum;
            }
            if($x<imagesx($oimg)-1)
            {
                if($y>0)
                {
                    $tlum = imageluminosity($oimg, $x+1, $y-1);
                    if($tlum > $lum) $blum = $tlum;
                }
                $tlum = imageluminosity($oimg, $x+1, $y);
                if($tlum > $lum) $blum = $tlum;
                if($y<imagesy($oimg)-1)
                {
                    $tlum = imageluminosity($oimg, $x+1, $y+1);
                    if($tlum > $lum) $blum = $tlum;
                }
            }
            // Get RGB from original image
            $color = imagecolorat($oimg, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            // Adjust luminosity to best luminosity of any neighboring pixel.
            if($olum != $blum)
            {
                $diff = $olum - $blum;
                $r = min(255, max(0, round($r + $diff * 0.299)));
                $g = min(255, max(0, round($g + $diff * 0.587)));
                $b = min(255, max(0, round($b + $diff * 0.114)));
            }
            // Get or allocate new color.
            $color = imagecolorexact($img, $r, $g, $b);
            if($color == -1)
                $color = imagecolorallocate($img, $r, $g, $b);
            imagesetpixel($img, $x, $y, $color);
        }
    }
    return $img;
}

I hope I didn't make any typos. The function I use is MUCH less verbose, but very cryptic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM