2
votes

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

It'll be something like: "out = myfilter(input_image, my_3x3_kernel)" where output size is identical to input image size.

However, I'm not supposed to use any in-built image filtering functions like imfilter(), filter2(). conv2() etc.

I'm really new to MATLAB and am very lost.

I was also told that input filter kernels have fixed size of 5x5 but I can use zero-padding to the image? Not sure what that means either so a little help would go a long way in helping me understand this better.

Thanks!

1

1 Answers

3
votes

Simple approach.

Generate an "image":

n = 50;
myImage = rand(n,n);
myBiggerImage = zeros(n+2, n+2);
myBiggerImage(2:end-1, 2:end-1) = myImage; % padded copy of the image
myKernel = [1 2 1; 2 4 2; 1 2 1]; % for example - this is a 3x3 kernel
myFilteredImage = zeros(n,n);  % space for the result

Now we need to somehow multiply "the right" elements. The boring way is this:

for ii = 1 to n
  for jj = 1 to n
    s = 0;
    for kk = 1 to 3
      for ll = 1 to 3
        s = s + myBiggerImage(ii+kk, jj+ll) * myKernel(kk,ll);
      end
    end
    myFilteredImage(ii,jj) = s;
  end
end    

Now you can do things to "vectorize" this. But do you think you could figure that part out?