Ooer.com by Chris Neale

Convolution Filters In GD

The PHP function imagecolorat() returns an integer representing the colour we need when used on a true colour image. Once we have this number we can extract the seperate channel integers.

<?php
$t1
= imagecolorat($source,$x-1,$y-1);
$p[0]['r'] = ($t1 >> 16) & 0xFF;
$p[0]['g'] = ($t1 >> 8) & 0xFF;
$p[0]['b'] = $t1 & 0xFF;
?>

In this example we are extracting the pixel information for the top left pixel ($t1) of our matrix (-1,-1). $p is an array used to hold the values of the colours. $p[0] equates to the top left, $p[1] to the top middle, and so on. This is the same index as we use for the convolution matrix array, so the code is relatively simple. Each element of the array has 3 sub-elements, 'r', 'g', and 'b', which corresponding to the red, green and blue constituents of the pixel.

Once we have the colours extracted we will need to set up an array and two variables to do the convolution process. The convolution matrix is an array of nine values. These value can be set differently to acheive different effects. In this example we will use a matrix for Gaussian Blurring. Aside from the array we need two more numbers. One is a factor by which we divide the number resulting from the convolution in order to get back to a value we can use as a colour, and the other is a constant we add on at the end if the convolution process can result in a negative value.

<?php
$m
= array(1,2,1,2,4,2,1,2,1);
$f = 16;
$c = 0;
?>

Actually convolving the convolution matrix with the extracted colour information is an easy process. All we have to do is multiply the value in the convolution array with the value in the same position in the colour array, divide by the factor we set up, and add on our constant. We need to do this for each of our three colour channels, and then recombine the channel values and draw the pixel to the destination image.

<?php
$pixel_r
= ((($p[0]['r']*$m[0]) + ($p[1]['r']*$m[1]) + ($p[2]['r']*$m[2]) + ($p[3]['r']*$m[3]) + ($p[4]['r']*$m[4]) + ($p[5]['r']*$m[5]) + ($p[6]['r']*$m[6]) + ($p[7]['r']*$m[7]) + ($p[8]['r']*$m[8]))/$f) + $c;
?>

The example only represents the code required to convolve the red channel.

<?php
$col
= imagecolorallocate($image,$pixel_r,$pixel_g,$pixel_b);
imagesetpixel($image,$x,$y,$col);
?>

Finally, once we have a completed image resource, we output it to the browser.

<?php
header
("Content-Type: image/jpeg");
imageJPEG($image);
?>

There are many different types of convolutions. The output of the code here can be changed dramatically simply by altering the matrix array set up. In this example we have created a Gaussian Blur filter, but the same code can be used to create a smoothing filter, edge detection filters, and even a Lapascian Emboss filter.
Move to page: 1 2

Comments

Comments are not currently being accepted for this article.
Sidebar
Published:
17/01/2007
Views:
2396
Author:
Chris Neale
Labels:
Print: