# High pass filtering FFT2 matlab

Tags:
1. Nov 14, 2018

### Taylor_1989

I am having a slight issue with my code and honestly cannot understand what I am doing wrong. I am currently trying to using a high pass filter in the frequency domain to form the following image.

So my approach so far after numerous of attempts which I wont list as I believe I am on the correct path, is to use a low pass filter matrix and then subtract from the original image, the issue is I cannot subtract due to the matrix of the low pass filter image being different from the original.

Code (Text):
%applying fouire transform shift to zero postion and fouire transform for
%2D
D = fftshift(fft2(A));
%defing the filter, Low pass filter
h = (1/9).*[1,1,1;1,1,1;1,1,1];
%apllying filter to the frequency domain
B1=filter2(h1,D);
B1 = uint8(round(B1));
G=ifft2(B1);
imshow(log(abs(G)))
The image being produced from this code is shown below

But when I run the code to subtract the I get the error code:

Code (Text):
%applying fouire transform shift to zero postion and fouire transform for
%2D
D = fftshift(fft2(A));
%defing the filter, Low pass filter
h = (1/9).*[1,1,1;1,1,1;1,1,1];
%apllying filter to the freqencey domian
B1=filter2(h1,D);
B1 = uint8(round(B1));
G=ifft2(B1);
E=imshow(log(abs(G)))
J = imresize(A,[246 300])
imsubtract(J,E)

Error code

Error using imsubtract (line 52)
X and Y must have the same size and class, or Y must be a scalar double.

Error in signla (line 84)
imsubtract(J,E)
As you can see I have tried to make the images the same size yet it still is not working and I am at a slight loss to why.

2. Nov 14, 2018

### RPinPA

I don't see where you've tried to make E be 246 x 300 or guaranteed that it has that size, so I'm not sure why that's your argument in the resize.

Two suggestions:
1. Put a breakpoint just before the IMSUBTRACT. Manually check size(J) and size(E).

2. Instead of using constants for the argument on that second-to-last line, do this: J = imresize(A, size(E))

3. Nov 15, 2018

### Taylor_1989

I have found that the images were of the same size but the class was not however when I subtract the images the outcome is not what is expected. So this leads me to either my method is wrong in the fact that I am using the wrong kernel or the enitre program is incorrect.

here is my code
Code (Text):

%figure;imagesc(A);colormap gray;colorbar;
%applying fouire transform shift to zero postion and fouire transform for
%2D
D = fftshift(fft2(A));
%defing the filter, Low pass filter
h = (1/9).*[1,1,1;1,1,1;1,1,1]
%apllying filter to the freqencey domian
B1=filter2(h1,D);
B1 = uint8(round(B1));
G=ifft2(B1);
E=(log((abs(G))));
%figure;imagesc(E);colormap gray;colorbar
%figure;imagesc(double(A)-E);colormap gray;colorbar
I2=im2double(A);
k=imsubtract(I2,E)
imshow(k)

ignore the comment out parts I was trying something, so I am not sure where to go from here. Any suggestions?

Last edited: Nov 15, 2018
4. Nov 15, 2018

### Taylor_1989

I have come to the conclusion that what I need to do is actually come how change the pixel values in the frequency domain to 0.

Here is my code but I can seem to get the values back into the image.

Code (Text):
%figure;imagesc(A);colormap gray;colorbar;
%applying fouire transform shift to zero postion and fouire transform for
%2D
D = fftshift(fft2(A));
E=log((abs(D)))
I=imagesc(E);colormap gray
%imcrop(I)
K=imcrop(E,[137.5,106.5,24,32])
imshow(K)
K(K<20)=0
imshow(K)

Last edited: Nov 15, 2018
5. Nov 15, 2018

### RPinPA

Unfortunately I don't have a copy of Matlab to experiment with at the moment. But here's my thought about the mathematics of what you're doing. You want to apply filter D to image A, $DA$. And then subtract that from the original image, $A - DA = (1 - D)A$ where "1" means the identity matrix.

So maybe you might try 1 - D as your high-pass filter? Or in Matlab, eye(3) - D?

BTW, this simple rectangle filter is probably not an optimal low-pass filter anyway. The FFT is a sinc function which has long tails, meaning it will leave a lot of high frequencies.