Time for action – applying averaging filters in images

We will start off with an easy-to-follow example, so that all the theory described previously is demonstrated. For our purposes, we will be using one of the images from the previous chapters. In this example, we will also introduce some new MATLAB functions, to facilitate your understanding. Let's start:

  1. First, we load our image, which is holiday_image2.bmp:
    >> img = imread('holiday_image2.bmp'),
  2. Then, we generate our convolution kernel, using function fspecial and then rotate it 180 degrees:
    >> kernel = fspecial('average',3);
    >> kernel = rot90(kernel,2)
  3. The output of the code will be as follows:
    kernel =
        0.1111    0.1111    0.1111
        0.1111    0.1111    0.1111
        0.1111    0.1111    0.1111
  4. Now, it is time to use the three different ways of convolving our image:
    >> con1 = conv2(img,kernel);          % Default usage ('full')
    >> con2 = conv2(img,kernel,'same'),   % convolution using
      'same'
    >> con3 = conv2(img,kernel,'valid'),  % convolution using
      'valid'
  5. In the previous step, you probably got a warning saying:
    Warning: CONV2 on values of class UINT8 is obsolete.
    Use CONV2(DOUBLE(A),DOUBLE(B)) or CONV2(SINGLE(A),SINGLE(B))
     instead.
  6. This actually means that UNIT8 type will not be supported by conv2 in the future. To be on the safe side, you might want to use the suggestion by MATLAB and convert your image to single prior to convolving it:
    >> img = single(img);
    >> kernel = fspecial('average',3);   % Create 3x3 averaging
      kernel
    >> con1 = conv2(img,kernel);         % Default usage ('full')
    >> con2 = conv2(img,kernel,'same'),  % convolution using 'same'
    >> con3 = conv2(img,kernel,'valid'), % convolution using
      'valid'
  7. Now, we can show our results in one figure, along with the original image. This time, we are going to use an empty matrix as the second argument in imshow, to avoid having to convert our results to UNIT8:
    >> figure;subplot(2,2,1),imshow(img,[]),title('Original')
    >> subplot(2,2,2),imshow(con1,[]),title('full')
    >> subplot(2,2,3),imshow(con2,[]),title('same')
    >> subplot(2,2,4),imshow(con3,[]),title('valid')

    Time for action – applying averaging filters in images
  8. It is obvious that the three results are identical, but there is a small detail. Their size is not. So let's see if we got what we expected. In the Workspace window, you can see the difference in sizes:
    Time for action – applying averaging filters in images
  9. Let's now discuss the physical, qualitative meaning of averaging an image. What does it exactly do? The answer is; it performs blurring of the image. To examine this effect, we can crop the tower from our original and averaged image and display the result. The tower can be cropped using the following coordinates:
    >> tower_original = img(51:210,321:440);
    >> tower_blurred = con2(51:210,321:440); figure
    >> subplot(1,2,1),imshow(tower_original),title('Original
      tower')
    >> subplot(1,2,2),imshow(tower_blurred),title('Blurred tower')
  10. The original image and the blurred image are as follows:
    Time for action – applying averaging filters in images

What just happened?

The process described in the previous example demonstrated the usage of convolution in its various implementations, using the averaging kernel produced using fspecial. This function is designed to generate kernels for popular filtering tasks, as we will further analyze in the following sections. In our case, we created a 3x3 kernel of values equal to 1/9 (which is almost equal to 0.1111, hence the result in step 2). Then, the three different choices of convolution were applied and the results were displayed along with the original image. Of course, a detail such as the size of the borders cannot be easily observed in full scale, so we observed the difference in the sizes of the results. Finally, we displayed a part of the original image next to the same part of the same convolution result, to prove that the result of the averaging process is a blurring of the image.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset