Alternatives to convolution

Convolution is not the only way to perform image filtering. There is also correlation, which gives us the same result. Filtering an image using correlation can be accomplished by using the MATLAB function called filter2, which performs, as its name implies, a two-dimensional filtering of two images. The first input in this case is a kernel (filter) and the second input is an image (or in a more general case a two-dimensional matrix). We will not go into detail here, just point out that one main difference between the two methods is that correlation does not need the kernel to be rotated. The border issue remains, having the same three approaches as in the case of convolution using conv2. A demonstration on the equivalence of the two functions is given if we type in the following commands:

>> img = imread('holiday_image2.bmp'),
>> img = img(51:210,321:440);
>> kernel = fspecial('average',3);
>> kernel180 = rot90(kernel,3);
>> conv_result = conv2(img,kernel180,'same'),
>> corr_result = filter2(kernel,img,'same'),
>> subplot(1,3,1),imshow(img),title('Original')
>> subplot(1,3,2),imshow(uint8(conv_result)),title('Blurred - conv2')
>> subplot(1,3,3),imshow(uint8(corr_result)),title('Blurred -
  filter2')

The result of the preceding code is displayed as follows:

Alternatives to convolution

Tip

In our example, the two kernels used for conv2 and filter2 are identical, since the averaging filter used is square (3x3) and all its elements are equal. The generalized process shown will be useful when we have a more complex kernel.

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

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