This site uses cookies to analyze traffic and improve service.

By using this site, you agree to this use. See details.

# Basic Image Manipulation

### Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

## Filtering

A lot of image processing algorithms rely on the convolution between a kernel (typicaly a 3x3 or 5x5 matrix) and an image. This may sound scary to some of you but that's not as difficult as it sounds:

Let's take a 3x3 matrix as our kernel. For each pixel, the filter multiplies the current pixel value and the other 8 surrounding pixels by the kernel corresponding value. Then it adds the result to get the value of the current pixel. Let's see an example:

In this example, the value of each pixel is equal to the double of the pixel that was located above it (e.g. 92 = 46 x 2).

## Blur

A simple blur can be done using this kernel:

This is called the Box Blur. Each pixel is computed as the average of the surrounding pixels.

And here is the kernel for the Gaussian Blur:

As you can see, it's a weighted mean of the surrounding pixels that gives more weight to the pixel near the current pixel.

## Edge detection

There are multiple ways to do edge detection. We will present the Sobel Operator here.

The Sobel Operator uses two kernels (one for each direction): Kx=⎡⎢⎣−101−202−101⎤⎥⎦${K}_{x}=\left[\begin{array}{ccc}-1& 0& 1\\ -2& 0& 2\\ -1& 0& 1\end{array}\right]$ and Ky=⎡⎢⎣−1−2−1000121⎤⎥⎦${K}_{y}=\left[\begin{array}{ccc}-1& -2& -1\\ 0& 0& 0\\ 1& 2& 1\end{array}\right]$.

We compute the convolution between the image (converted in black and white) and the two kernels separately. That gives us, for each pixel, the values magx$ma{g}_{x}$ and magy$ma{g}_{y}$. The value of the current pixel is set at √mag2x+mag2y$\sqrt{ma{g}_{x}^{2}+ma{g}_{y}^{2}}$.