views:

655

answers:

4

I know this isn't very relevant to programming, but I need to know how a convolution matrix works for a PHP GD function. I've searched a lot on Google, but can't find anything that explains it well.

+1  A: 

The operation replaces each pixel with the weighted average of the pixels around it, where the weights are given by the matrix. Here's an example convolution matrix:

1 1 1
1 1 1
1 1 1

What this does is replace each pixel with the average value of the 3x3 block centered on that pixel. Here's another:

0 0 0
0 1 0
0 0 0

This matrix doesn't do anything, it gives you the original back.

The weights can be negative, too. This matrix subtracts the average value of the pixels next to a pixel:

 0 -1  0
-1  4 -1
 0 -1  0

Convolution matrices allow you to do fine-tuned blurring and sharpening effects. You can tune the directionality and the frequency response of filters using a convolution matrix, if it's large enough. However, it's usually used for quick-n-dirty blurring and sharpening.

Dietrich Epp
What is ment by the "value" of the pixel? The x/y coords, or the colour values?
Hussain
+1  A: 

I don't know specifically for PHP, but in general a convolution matrix is used to implement certain kinds of image processing effects.

A simple example taken from the PHP manual on GD http://www.php.net/manual/en/function.imageconvolution.php:

Let's say you have a matrix like this:

$M = array(array( 2,  0,  0),
           array( 0, -1,  0),
           array( 0,  0, -1));

When you apply that convolution matrix to an image, then for each pixel located at (x,y) in the image, the corresponding pixel in the output becomes:

$I = $in_image;
$out_image[x,y] = $I[x-1,y-1]*$M[0][0] + $I[x,y-1]*$M[0][1] + $I[x+1,y-1]*$M[0][2]
                + $I[x-1,y]  *$M[1][0] + $I[x,y]  *$M[1][1] + $I[x+1,y]  *$M[1][2]
                + $I[x-1,y+1]*$M[2][0] + $I[x,y+1]*$M[2][1] + $I[x+1,y+1]*$M[2][2];

In other words, the convolution matrix is used to compute each result pixel as a linear combination of the source pixel and the pixels surrounding it.

The divisor parameter is used to divide the whole result by something (this is usually the sum of all the values in the matrix) and the offset is used to add a constant term to the final output value.

DK
A: 

Convolution matrix works on each channel of the pixel of the image independently.
So you have to learn about channels first.
Then, you can "play" with this matrix interactively if you have a Photoshop: go to the menu Filters->Other->Custom. The central item of the matrix represents current pixel, all other represent surrounding pixels. And don't forget about the "Scale" value at the bottom.
I think, you can easily understand the way this matrix works.
You may have a look at this thing here. It describes the convolution matrix as well.

avp
+1  A: 

I went into a load of the theory behind convolution filters here:

http://stackoverflow.com/questions/1696113/how-do-i-gaussian-blur-an-image-without-using-any-in-built-gaussian-functions/1696200#1696200

Goz