Let's go to uncomfort zone

Wednesday, 6 December 2017

Concept of blurring images explained.

Hello friends! have you ever been curious to know that how blurring of images is done?. In photo editing apps, we just click on blur button and magically we get our image blurred. how it is done? what techniques they use? If you don’t have any idea then this complete article is for you. Let’s start. Before understanding what is blurring, it’s necessary to understand what exactly is image-

What is an image?
An image is made up of its smallest elements called pixels. Each pixel in the image have pixel value or you can call intensity value, intensity value can be between 0 to 255. So, image is just a MxN order matrix of intensity values where m, n are number of rows and columns respectively. MxN matrix of intensity values is same as MxN resolution image eg 1280x720, 640x480 etc. Image given below will explain it more clearly:
Above explanation was about grayscale images, not about colored images. Colored images have same phenomenon as, but value of a pixel depends upon color spaces eg. RGB, HSV etc. Usually RGB color space is used in images which you capture and share in daily life, pixel values of RGB images look like [128,230,90], [255,0,21] etc. Sometimes images contain transparency, for transparency we use alpha channel.
In this article, I will explain concept of blurring using grayscale image to get you easily understand everything and we will use 350x340 grayscale image shown in image above. let's talk about blurring. 
    We want to blur our image, but how can we do it? What changes should we make in image?. If you think in reverse then you will understand that we should change our pixel values in such a way that our image will look like blurred. If you change pixel values randomly then your image will be spoiled. So, we need a particular way or technique to change our pixel values. There are more than one blurring techniques used to blur images eg. mean blur/Averaging, median blur, Gaussian blur etc. we can also use our own/custom way to blur images. In this article we will blur our image using mean blur technique. Because it will be easy to understand for you. All techniques are quite same, but they use different-different kernels. If you don’t know what is kernel don’t worry, you will understand later on. Beware that this kernel is not related to OS. 

here you have to understand one more thing called convolution. So, what is convolution?

What is convolution?
I am going to explain convolution by example. So, We have two matrices A and B. I just put random elements in A and B matrices.
Warning: Remember we are understanding convolution, so don’t mix it with matrix of pixel values.

As you can see in image, we have flipped our matrix B. Now multiply all elements of flipped matrix with corresponding elements of matrix A and add them, like this:
1*20 + 2*5 + 3*12 + 4*4 + 5*7 + 6*3 + 7*10 + 8*19 + 9*11 = 456
Now replace [2,2] element in matrix A (which is 5) by 456.

In this process, we just changed element at center position [2,2] in matrix A and we call this process convolution. It’s not necessary to use 3x3 order matrix only, you can also use other square matrices of 5x5,7x7 etc.

Blurring

Now it’s time to understand our target topic which is blurring. Our image(img1) has 350 rows and 340 columns, that means number of pixels in our image is 350*340 = 119,000 pixels. So, to blur the image we have to change all 119,000 pixel values by blurring technique and convolution is used for it. We know that each pixel in image have 8 pixels around and they all 8+1=9 in combine make 3x3 matrix. Number of our pixels is 119,000, so there will be 119,000 matrices of 3x3 order. We will apply convolution for all 119,000 matrices with common matrix called kernel. Kernel is just a special matrix used to get our pixel values changed by using convolution in such a way that our image will be blurred. Above in explanation of convolution, we used two matrices A and B. So, kernel is our B matrix. We will use 3x3 averaging kernel for blurring which you can see in image below.
We can also use 5x5 or 7x7 kernel, but we are using 3x3 in this case. The process will look like this:
This image shows process for just 2 pixel values, but the same process will be applied for all 119,000 pixels. As you can see in image, when we deal with boundary pixel values, that time we reflect opposite rows and columns elements. All pixel values get changed one by one and the they make a new image, the new image will be our blurred image. You can see below:
So, this is how we blur our whole image, but you see in image editors that they provide facility to blur a specific area. Let me tell you how it works:
In image editing apps, size of the round shaped tool is fixed(eg. MxN) before you click to blur. So, when you click to blur somewhere on image the image editor app selects area of MxN and apply convolution for all the pixels of selected area. After that you get your area blurred, but this process takes time in milliseconds that’s why we cannot feel that something is happening behind.
     
I hope you understood whole process of blurring, but one more question should raise in your mind that how it is done in programming? If you want knowledge about it then you can ask me in comments, i will give answer to your question for sure.

Happy learning :)

6 comments:

  1. Excellent!!! , One of the best article till this time

    ReplyDelete
  2. Thank you . To the point ,just how i like it.

    ReplyDelete
  3. Replies
    1. Thank you brother... Yes you can write program for blurring and it's very easy. If know Python or Java or C++ then you learn image processing library like OpenCV, you can read our article on OpenCV here: http://www.calloftechies.com/2018/01/how-to-get-started-with-opencv.html.

      There is one famous language called MATLAB, you can use it for image processing. Image processing is really an interesting field.

      In future I will write more articles about images, you can subscribe our blog by clicking on bell icon at bottom right corner

      Delete