Monday, March 19, 2012

Neighborhood processing using MATLAB

Correlation operation: correlation is the same as convolution without the mirroring (flipping) of the mask before the sums of products are computed.

Convolution operation: convolution is a widely used mathematical operator that processes an image by computing—for each pixel—a weighted sum of the values of that pixel and its neighbors.

Convolution with masks is a very versatile image processing method. Depending on the choice of mask coefficients, entirely different results can be obtained, for example, image blurring, image sharpening, or edge detection.

Linear Filters: Here the resulting output pixel is computed as a sum of products of the pixel values and mask coefficients in the pixel’s neighborhood in the original image. E.g.: mean filter.

Nonlinear Filters: Here the resulting output pixel is selected from an ordered (ranked) sequence of pixel values in the pixel’s neighborhood in the original image. E.g.: median filter, the max and min filters.

%Perform convolution operation using MATLAB
>> a = [0 0 0 1 0 0 0]; % signal

>> f = [1 2 3 4 5]; % filter
>> g = imfilter(a,f,'full','conv')
g =
     0     0     0     1     2     3     4     5     0     0     0
>> g = imfilter(a,f,'same','conv')

g =
     0     1     2     3     4     5     0

%Perform correlation operation on the same filter
>> h = imfilter(a,f,'full','corr')

h =
     0     0     0     5     4     3     2     1     0     0     0

%Perform correlation operation on the 2D signal

>> x = [140 108 94;89 99 125;121 134 221] % signal
x =
   140   108    94
    89    99   125
   121   134   221

>> y = [-1 0 1;-2 0 2;-1 0 1] % filter mask
y =
    -1     0     1
    -2     0     2
    -1     0     1

>> z = imfilter(x,y,'corr')
z =
   315   -56  -315
   440   126  -440
   367   236  -367

To perform convolution,weuse the same technique as in correlation. The difference here is that the filter matrix is rotated 180^o before performing the sum of products.

>> z2 = imfilter(x,y,'conv')
z2 =
  -315    56   315
  -440  -126   440
  -367  -236   367

%Generate a mean (average) filter

>> fn = fspecial('average')
fn =
    0.1111    0.1111    0.1111
    0.1111    0.1111    0.1111
    0.1111    0.1111    0.1111

%Apply the mean filter to the original image

>> I = imread('cameraman.tif');
>> figure, subplot(1,2,1), imshow(I), title('Original Image');

>> I_new = imfilter(I,fn);
>> subplot(1,2,2), imshow(I_new), title('Filtered Image');

The below figure shows the effects of the mean filter operation.


%Generate non-uniform mean filter
>> fn2 = [1 2 1; 2 4 2; 1 2 1]

fn2 =
     1     2     1
     2     4     2
     1     2     1
>> fn2 = fn2 * (1/16)
fn2 =

    0.0625    0.1250    0.0625
    0.1250    0.2500    0.1250
    0.0625    0.1250    0.0625

%Apply the non-uniform mean filter to the original image to compare with the uniform one.
>> I_new2 = imfilter(I,fn2);
>> figure, subplot(1,2,1), imshow(I_new), title('Uniform Average');
>> subplot(1,2,2), imshow(I_new2), title('Non-uniform Average');

The below figures show the difference in subjective quality evaluation between two outputs.


The Gaussian filter is similar to the nonuniform averaging filter in that the coefficients are not equivalent. The coefficient values, however, are not a function of their distance from the center pixel, but instead are modeled from the Gaussian curve.

%Generate the Gaussian filter and draw the filter as 3D graph

>> fn_gau = fspecial('gaussian',9,1.5);
>> figure, bar3(fn_gau,'b'), title(Gaussian filter as a 3D graph');

%Apply the Gaussian filter to the original image

>>I_new3 = imfilter(I,fn_gau);
>> figure
subplot(1,3,1), imshow(I), title('Original Image');
subplot(1,3,2), imshow(I_new), title('Average Filter');
subplot(1,3,3), imshow(I_new3), title('Gaussian Filter');

The below figure shows the subjective quality difference between two filtered images.


References

  1. Oge Marques, Practical Image and Video Processing Using MATLAB, Wiley-IEEE Press, September 2011.

No comments:

Post a Comment

Mounting USB drives in Windows Subsystem for Linux

Windows Subsystem for Linux can use (mount): SD card USB drives CD drives (CDFS) Network drives UNC paths Local storage / drives Drives form...