0
votes

I'm looking to implement my own Matlab function that can be used to compute image filtering with a 3x3 kernel.

It has to be like this: function [ output_args ] = fFilter( img, mask ) where img is a original image and mask is a kernel (for example B = [1,1,1;1,4,1;1,1,1] )

I'm not supposed to use any in-built functions from Image Processing Toolbox.

I have to use this

formula

where:

s is an image after filter

p is an image before filter

M is a kernel

and N is 1 if sum(sum(M)) == 0 else N = sum(sum(M))

I'm new to MATLAB and this is like black magic for me -_-

1

1 Answers

0
votes

This should do the work (Wasn't verified):

function [ mO ] = ImageFilter( mI, mMask )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here

numRows = size(mI, 1);
numCols = size(mI, 2);

% Assuming Odd number of Rows / Columns
maskRadius = floor(siez(mMask, 1) / 2);

sumMask = sum(mMask(:));

if(sumMask ~= 0)
    mMask(:) = mMask / sumMask;
end

mO = zeros([numRows, numCols]);

for jj = 1:numCols
    for ii = 1:numRows
        for kk = -maskRadius:maskRadius
            nn = kk + 1; %<! Mask Index
            colIdx = min(max(1, jj + kk), numCols); %<! Replicate Boundary
            for ll = -maskRadius:maskRadius
                mm = ll + 1; %<! Mask Index
                rowIdx = min(max(1, ii + ll), numRows); %<! Replicate Boundary
                mO(ii, jj) = mO(ii, jj) + (mMask(mm, nn) * mI(rowIdx, colIdx));
            end
        end
    end
end


end

The above is classic Correlation (Image Filtering) with Replicate Boundary Condition.