Monday, July 09, 2007

Image convolution

Convolution :
--------------------
Image convolution plays an important role in computer graphics applications.
Convolution of images allows for image alterations that enable the creation of new images from old ones.
Convolution also allows for important features such as edge detection, with many widespread uses.

The convolution of an image is a simple process by which the pixel of an image is multiplied by a kernel, or masked, to create a new pixel value. Convolution is commonly referred to as filtering.

Details :
First, for a given pixel (x,y), we give a weight to each of the surrounding pixels. It may be thought of as giving a number telling how important the pixel is to us. The number may be any integer or floating point number, though I usually stick to floating point since floats will accept integers as well.

The kernel or mask that contains the filter may actually be any size (3x3, 5x5, 7x7); however, 3x3 is very common. Since the process for each is the same, I will concentrate only on the 3x3 kernels. (kernel contains values filled by user).

Second, the actual process of convolution involves getting each pixel near a given pixel (x,y), and multiplying each of the pixel's channels by the weighted kernel value. This means that for a 3x3 kernel, we would multiply the pixels like so:

the pixel value at (x,y) is calculated as follows :

(x-1,y-1) * kernel_value[row0][col0] +
(x ,y-1) * kernel_value[row0][col1] +
(x+1,y-1) * kernel_value[row0][col2] +
(x-1,y ) * kernel_value[row1][col0] +
(x ,y ) * kernel_value[row1][col1] +
(x+1,y ) * kernel_value[row1][col2] +
(x-1,y+1) * kernel_value[row2][col0] +
(x ,y+1) * kernel_value[row2][col1] +
(x+1,y+1) * kernel_value[row2][col2]


neighbour pixels for the given pixel (x, y) :


(x-1,y-1) (x,y-1) (x+1, y-1)

(x-1, y) (x,y) (x+1,y)

(x-1,y+1) (x, y+ 1) (x+1, y+1 )


Kernel matrix :

kernel_value[row0][col0] kernel_value[row0][col1] kernel_value[row0][col2]

kernel_value[row1][col0] kernel_value[row1][col1] kernel_value[row1][col2]

kernel_value[row2][col0] kernel_value[row2][col1] kernel_value[row2][col2]



multiply the neighbour pixels with the kernel matrix :

No comments: