0

I'm new to Matlab. I'm really just getting started into the actual computer science classes in my major. So, please keep that in mind. The goal is to create and apply a gaussian filter to this specific image using no built-in functions. So far, I have this bit of code to create a kernel. We were playing around with different sigma values, and then produced a visualization of the kernel.

    f = imread( 'input.png');

    sig = 5;
    hw = floor (2.5 * sig - .5);

    w = zeros(hw*2+1, hw*2+1);

    for r = 1:size(w,1)
        for c = 1:size(w,2)
            w(r,c) = exp(-1 * ((c - (hw+1))^2 + (r-hw)^2) / (2 * sig^2));
        end
    end

    imagesc(w);
    colormap jet;

My problem comes when actually applying it. I'm really not sure on what to do. He gave us the following code as a guide, but I'm still stuck.

    for r = 1:R
        for c = 1:C
            for r1 = 
                for c1 = 
                   temp = temp + f() + w();
                end
            end
        end
    end

If anyone could point me in the right direction, it would be greatly appreciated. Thank you.

  • What do you consider a "built-in"? Technically `floor`, `imread`, `zeros`, etc. are all builtins – Suever Feb 17 '17 at 01:34
  • Sorry, I should've been more specific. He just says, "Do not use any built-in functions for building Gaussian filter nor performing convolution." We can't use any filter functions, convolution functions, etc. It's hard to explain because I don't have much Matlab knowledge. We have only learned basic functions, so that's all we're allowed to use. – user7578205 Feb 17 '17 at 02:34

2 Answers2

3

Assuming it is a tutorial problem given to you in the class, I will give you hints to proceed.

  • Outer two loops should be used to traverse over the entire image. You need to put proper R and C depending upon the size of kernel you are using. (You have to avoid the boundary such that your kernel doesn't go outside the image.)
  • Before the beginning of inner two loops, you must reset your temp to zero, r1 and c1 must vary from 1 to size of the kernel.
  • f() and w() should be multiplied and added to the temp to effectively do convolution. Now your task is to work out the appropriate indices used for f and w.
  • And you need to update the current pixel value with temp at appropriate place.

I hope this makes sense.

Cheers,

Sukuya
  • 120
  • 7
1

If you're willing to use a Fast Fourier Transform, everything becomes easy:

  1. Fourier transform your image (component per component)
  2. Multiply every transformed component element-by-element with a matrix containing your filter in frequency space.
  3. Fourier transform back, and drop the small complex part your filter introduced.

Of course, if you consider fft a forbidden builtin, this doesn't qualify.

Sukuya
  • 120
  • 7
rubenvb
  • 74,642
  • 33
  • 187
  • 332