0
votes

I have an assignment to filter in the frequency domain. It gives me various filters to use in part of the question but I am just trying to figure out how to add even one. I'm still learning Matlab and image processing in general.

Assignment Question: Use index2gray to convert “tees.tif” to gray level image. Filter the gray level image in the frequency domain using 2D fft (fft2), after performing the operation you can use 2D ifft (ifft2) to display the filtered image in the spatial domain for:

a- Rectangular low pass filter using cutoff frequency of (uc=N/8,vc=M/8) where N and M are the row and column sizes of the image.

My Current code is:

[I,cmap] = imread('trees.tif');
img = ind2gray(I,cmap);
[rows columns] = size(img);
imshow(img);
fftO = fft2(double(img));
shifted = fftshift(fftO);
logged = log(1+abs(shifted));
imshow(logged,[]);

If I understand this correctly, I have the grey level image in the frequency domain and now need to filter it. I'm confused about the filtering part. I need to make a rectangular low pass filter using cutoff frequencies. How do I go about adding cutoff frequencies? I assume I will be using a Gaussian or Butterworth filter and have the filter size be equal to the image.

After I figure out this filter thing, I should be able to do (H is filter)

 filtered = logged.*H; 
 invert = real(ifft2(filtered));
 imshow(invert);

Anyone know how I need to proceed with the filter section?

1
In the frequency domain, I believe that a rectangular low-pass filter should basically just be 1 for frequencies below your cutoff, and 0 for frequencies above, so you should be able to construct H from that. Since it's an assignment, I'm going to stop there.KevinMc
I was looking to use a fspecial() predefined but this I am now thinking I will have to create my matrix/filter instead of using something predefined with parameters. Is this correct?Chad

1 Answers

1
votes

KevinMc essentially told you what the answer is. fspecial certainly allows you to define certain 2D filters, but it doesn't have rectangular filters. You can create your own though! You create a filter / mask that is the same size as your image where the centre of this mask is a rectangle with height N/8 and width M/8. Once you do this, you simply multiply with your image in the frequency domain and you then take the ifft2 as you have specified in your code.

You've got the code correct... you just need to create the mask! As such, use meshgrid to generate a 2D grid of (x,y) co-ordinates, then use Boolean conditions to find those pixels that span between -N/8 to N/8 for the height and -M/8 to M/8 for the width, making sure that the centre of the mask is the origin (0,0). Therefore:

[X,Y] = meshgrid(1:columns, 1:rows);

H = (X - floor(columns/2) >= -columns/8) & (X - floor(columns/2) <= columns/8) & ...
       (Y - floor(rows/2) >= -rows/8) & (Y - floor(rows/2) <= rows/8);

H = double(H);

The last line of code is important, as we need to cast to double so that you can convolve / filter your image by multiplying with the frequency domain version of the image. You can only multiply things in MATLAB provided that they are of the same type. H is a logical after applying the Boolean conditions, so you need to convert to double before you proceed.

As an example, let's say rows = 200 and cols = 200. This means that for the rectangular filter, this should span from horizontal frequencies of -25 to 25, and the same for the vertical frequencies. This means that we should get a square of 50 x 50. If we run this code, this is the image I get:

enter image description here

As such, just use those two lines of code, and your mask will be stored in H. You can then use this to filter your image.

Good luck!