MATLAB High pass filtering FFT2 matlab

Click For Summary
The discussion revolves around issues faced while implementing a high-pass filter using FFT in MATLAB. The user is attempting to subtract a low-pass filtered image from the original but encounters size and class mismatch errors during the subtraction process. Suggestions include checking the sizes of the images before subtraction and resizing the images to ensure compatibility. Additionally, there are recommendations to modify the filtering approach, such as using the identity matrix to create a high-pass filter. The user acknowledges the need for adjustments in their filtering method and expresses uncertainty about the effectiveness of their current approach.
Taylor_1989
Messages
400
Reaction score
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,518
  • coins.png
    coins.png
    5.4 KB · Views: 1,100
Physics news on Phys.org
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))
 
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:
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:
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
25K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K