Time for action – how much blurring is enough

Just like we did in previous chapters, we will write a custom function that incorporates a combination of MATLAB functions to make our lives easier. This time, our function will perform image blurring, hence will be called BlurImage.m:

function [output] = BlurImage(input,kernel_choice,kernel_size,method)

% Function for image blurring
% Inputs:
%        input - Input image
%        kernel_choice – User's choice of filter
%         (1: disk
%          2: average
%          3: gaussian)
%        kernel_size – User's choice of kernel size
%                   ([radius] for disk,
%          [rows, columns] for average,
%          [rows, columns, standard deviation] for Gaussian)
%        method – User's choice of filtering method
%       (1: correlation
%        2: convolution)
% Output:
%        output - Output image (after bluring)

switch kernel_choice
  case 1
    kernel = fspecial('disk',kernel_size);
case 2
    kernel = fspecial('average',kernel_size);
case 3
    kernel = fspecial('gaussian',kernel_size);
end

switch method
  case 1
    output = imfilter(input,kernel,'conv'),
  case 2
    output = imfilter(input,kernel,'corr'),
end

Now we can test our code. Let's filter the same image using three different filters, using two different sizes:

  1. First, we will load and crop our image:
    >> img = imread('holiday_image2.bmp'),
    >> img = img(51:180,321:440);
  2. Then, we will apply the three filters with selected size 3x3:
    >> f1 = BlurImage(img,1,1,1);
    >> f2 = BlurImage(img,2,[3,3],1);
    >> f3 = BlurImage(img,3,[3,3,1.5],1);
  3. Let's do the same for kernels of size 5x5:
    >> f4 = BlurImage(img,1,2,1);
    >> f5 = BlurImage(img,2,[5,5],1);
    >> f6 = BlurImage(img,3,[5,5,1.5],1);
  4. Finally, we will display all images in the same figure, next to the original:
    >> subplot(2,4,1),imshow(img),title('Original')
    >> subplot(2,4,2),imshow(f1),title('Blur by disk of radius 1')
    >> subplot(2,4,3),imshow(f2),title('Blur by 3x3 averaging
      kernel')
    >> subplot(2,4,4),imshow(f3),title('Blur by 3x3 Gaussian
      kernel')
    >> subplot(2,4,6),imshow(f4),title('Blur by disk of radius 2')
    >> subplot(2,4,7),imshow(f5),title('Blur by 5x5 averaging
      kernel')
    >> subplot(2,4,8),imshow(f6),title('Blur by 5x5 Gaussian
      kernel')
  5. The result will be as follows:

    Time for action – how much blurring is enough

What just happened?

We have just created a tool that can be useful for the one-step blurring of images. All three blurring methods are included and you have the choice of filter parameters. The three methods were then demonstrated, for different kernel sizes, and the effect they have on an image became obvious. From this example, the pros and cons of each choice are not very apparent. The only thing that is very apparent is that they all cause a loss of detail, which could be useful in special cases.

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

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