jvitasek
jvitasek

Reputation: 810

Sharpen an image with a linear filter in PHP

I would like to read an image and sharpen it using a linear filter, which is this matrix:

H =  [-0.5 -0.5 -0.5; -0.5 5 -0.5; -0.5 -0.5 -0.5]

I know about ImageMagick, is there any possibility to do just that?

Upvotes: 1

Views: 604

Answers (1)

Richard87
Richard87

Reputation: 1622

What you are after is applying a Convolution filter to an image, for that you can use PHP's built in imageconvolution() php.net function.

For usage, see Magilvia's example on php.net modified for your purpose:

<?php 
    $sharpenMatrix = array 
    ( 
        array(-0.5, -0.5, -0.5), 
        array(-0.5, 5, -0.5), 
        array(-0.5, -0.5, -0.5) 
    ); 

    // calculate the sharpen divisor 
    $divisor = array_sum(array_map('array_sum', $sharpenMatrix));            

    $offset = 0; 

    // apply the matrix 
    imageconvolution($img, $sharpenMatrix, $divisor, $offset); 
?>

$img is a PHP image the convolution matrix (sharpening filter) is applied on

Upvotes: 2

Related Questions