High pass filtering FFT2 matlab

In summary: But I suspect you know that and are just trying to get any low-pass filter to work before you proceed to a more sophisticated one.In summary, the conversation discusses the issue of using a high pass filter in the frequency domain to form an image. The approach of using a low pass filter and subtracting it from the original image is discussed, but the code for subtracting the images is not working. The individual tries to troubleshoot the issue by changing image sizes and using different kernels, but is still unable to get the desired outcome. The conversation then moves on to trying to change the pixel values in the frequency domain to 0, but the individual is having trouble getting the values back into the image. Suggestions are given to try using
  • #1
Taylor_1989
402
14
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.

lf.png


So my approach so far after numerous of attempts which I won't 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:
%reading in the image
A=imread('coins.png');
%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
coins.png

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

Code:
%reading in the image
A=imread('coins.png');
%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.
 

Attachments

  • lf.png
    lf.png
    25 KB · Views: 1,420
  • coins.png
    coins.png
    5.4 KB · Views: 979
Physics news on Phys.org
  • #2
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
RPinPA said:
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))

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:
%reading in the image
A=imread('coins.png');
%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:
  • #4
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:
%reading in the image
A=imread('coins.png');
%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:
  • #5
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.
 

Related to High pass filtering FFT2 matlab

1. What is high pass filtering in FFT2 matlab?

High pass filtering in FFT2 matlab is a process of removing low frequency components from an image or signal while retaining the high frequency components. It is used to enhance the edges and fine details in an image or signal.

2. How does high pass filtering work in FFT2 matlab?

In FFT2 matlab, high pass filtering works by transforming the image or signal into the frequency domain using the Fast Fourier Transform (FFT) algorithm. The low frequency components are then removed by applying a high pass filter, and the resulting signal is transformed back to the spatial domain using the Inverse Fast Fourier Transform (IFFT).

3. What is the importance of high pass filtering in FFT2 matlab?

High pass filtering in FFT2 matlab is important because it helps to remove noise and blur from an image or signal, making it clearer and more defined. It is also useful for feature extraction and analysis in various fields such as image processing, signal processing, and data analysis.

4. How do I perform high pass filtering in FFT2 matlab?

To perform high pass filtering in FFT2 matlab, you can use the built-in functions such as fft2, ifft2, and the various filter functions such as fspecial and imfilter. These functions can be used to apply different types of high pass filters, such as Gaussian, Butterworth, and Laplacian filters, to the image or signal.

5. What are the limitations of high pass filtering in FFT2 matlab?

One limitation of high pass filtering in FFT2 matlab is that it can cause ringing artifacts around sharp edges in the image or signal. This can be mitigated by using different types of filters or by applying a small amount of low pass filtering after the high pass filtering. Additionally, high pass filtering may not be effective in removing noise or blur if the signal contains very low frequency components.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
872
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Electrical Engineering
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top