How to Linear Filtering Without Using Imfilter Function in MATLAB?
Edges can be sharpened, random noise can be reduced, and uneven illuminations can be corrected using a linear filtering technique. Correlating the image with the proper filter kernel completes the process.
The imfilter function calculates the value of each output pixel using double-precision floating-point arithmetic. If the result is outside the data type’s permitted range, Imfilter reduces it to that range. If the data type is an integer, imfilter rounds fractional values.
We now write linear filtering code in MATLAB without using the imfilter function after understanding the definitions of linear filtering and the imfilter function.
Syntax:
Filter = NewFilters (y,x,OldFilters)
Applying filters x and y to an entire financial time series object with a filter specification results in Filter = NewFilters (y,x,OldFilters).
The filter in “Direct Form II Transposed” implements the common difference equation. The financial time series object NewFilters contains the same data series (names) as the input OldFilters.
Example 1:
Matlab
% MATLAB code for read image X=imread( 'w3wiki.tif' ); figure,imshow(X); title( 'Original Image' ); corr=[0 0.6 0.6;-1 0.5 0.3; 0.5 0.1 0;]; %corr=[0.6 % 0.5 % 0.2]; %corr=ones(6,6)/36; |
Input image:
Based on the kernel size, pad the input image with zeroes. Additionally, the built-in Matlab function pad array can be utilized for array padding.
Matlab
% MATLAB code for linear filtering pad1= size(corr,1)-1; pad2= size(corr,2)-1; output=uint8(zeros(size(X))); if (size(corr,1)==1) Y=zeros(size(X,1),size(X,2)+pad2); n=0; m=floor(size(corr,2)/2); sz1=size(Y,1); sz2=size(Y,2)-pad2; elseif (size(corr,2)==1) Y=zeros(size(X,1)+pad1,size(X,2)); n=floor(size(corr,1)/2); m=0; sz1=size(Y,1)-pad1; sz2=size(Y,2); else Y=zeros(size(X,1)+pad1,size(X,2)+pad2); n=floor(size(corr,1)/2); m=floor(size(corr,2)/2); sz1=size(Y,1)-pad1; sz2=size(Y,2)-pad2; end for i=1:size(X,1) for j=1:size(X,2) Y(i+n,j+m)=X(i,j); end end szcorr1=size(corr,1); szcorr2=size(corr,2); for i=1:sz1 for j=1:sz2 sum=0; n=i; m=j; for a=1:szcorr1 for b=1:szcorr2 Calculated is the weighted sum of the neighbourhood pixels. sum=sum+(Y(n,m)*corr(a,b)); m =m+1; end m=j; n=n+1; end output(i,j)= sum; end end figure,imshow(output); title( 'After linear filtering' ); |
Output:
Ones(6,6)/36 for the correlation kernel;
Example 2:
Matlab
A=imread( 'Coins.tif' ); figure,imshow(A); title( 'Original Image' ); corr=[0 0.6 0.6;-1 0.5 0.3; 0.5 0.1 0;]; %corr=[0.6 % 0.5 % 0.2]; %corr=ones(6,6)/36; |
Input image:
Based on the kernel size, pad the input image with zeroes. Additionally, the built-in Matlab function padarray can be utilized for array padding.
Matlab
pad1= size(corr,1)-1; pad2= size(corr,2)-1; output=uint8(zeros(size(A))); if (size(corr,1)==1) B=zeros(size(A,1),size(A,2)+pad2); m=0; n=floor(size(corr,2)/2); sz1=size(B,1); sz2=size(B,2)-pad2; elseif (size(corr,2)==1) B=zeros(size(A,1)+pad1,size(A,2)); m=floor(size(corr,1)/2); n=0; sz1=size(B,1)-pad1; sz2=size(B,2); else B=zeros(size(B,1)+pad1,size(A,2)+pad2); m=floor(size(corr,1)/2); n=floor(size(corr,2)/2); sz1=size(B,1)-pad1; sz2=size(B,2)-pad2; end for i=1:size(A,1) for j=1:size(A,2) B(i+n,j+m)=A(i,j); end end szcorr1=size(corr,1); szcorr2=size(corr,2); for i=1:sz1 for j=1:sz2 sum=0; m=i; n=j; for x=1:szcorr1 for y=1:szcorr2 Calculated is the weighted sum of the neighbourhood pixels. sum=sum+(B(m,n)*corr(x,y)); n = n+1; end n=j; m=m+1; end output(i,j)= sum; end end figure,imshow(output); title( 'After linear filtering' ); |
Output: