# How to do a Fourier analysis with a mask?

37 views (last 30 days)
William on 7 Sep 2022
Edited: William on 14 Sep 2022
I want to exclude the transparent parts of PNG pictures in Fourier analysis.
I just don't know how to use the alpha mask given by imread to do Fourier analysis analysis through fft2 (which needs the input images being rectangular) appropriately when the transparent part is not rectangle.
Do you know how to exclude transparent parts which are not rectangles of PNG picture?
Thank you very much!

Walter Roberson on 7 Sep 2022
Use
syntax. You can then threshold based on alpha to determine whether to include a pixel or not.
However... your fft2 depends upon the image being rectangular. It is not going to be easy to ignore the pixels outside the mask when you use fft2 for phase analysis.
Also, we do not recommend naming a variable image due to the conflict with the important MATLAB function named image
William on 14 Sep 2022
Edited: William on 14 Sep 2022
I think I found a way when I noticed that fft2 works by simply calling the function fft. But I met a problem at the reconstruction step through ifft. Because this step needs math knowledge to modify the reconstruction equation, which I am really bad at. Codes are shown following.
These are the codes for scrambling the phase of pictures without transparent parts and keeping power of each frequency unchanged:
%%%%
img='picture.jpg';
im_norm = This_image-mean(This_image(:));
phase_random = angle(fft2(rand(size(im_norm,1), size(im_norm,2))));
for rgb=1:3
im_random_phase(:,:,rgb)=ifft2(abs(fft2(im_norm(:,:,rgb))).*exp(sqrt(-1)*(angle(fft2(im_norm(:,:,rgb)))+phase_random)));
end
results_image= real(im_random_phase)+mean(This_image(:));
imshow(unit8(results_image))
%%%%
These are codes modified for scrambling the phase of pictures with transparent parts:
img='ballon.PNG';
matrix_rand=rand(size(im_norm,1), size(im_norm,2));
% random factor
for c=1:size(matrix_rand,2)
end
end
for r=1:size(matrix_rand,1)
end
end
for c=1:size(matrix_rand,2)
end
end
%% do FFT seperately
for rgb=1:3
for c=1:size(matrix_rand,2)
end
end
for r=1:size(matrix_rand,1)
end
end
end
for rgb=1:3
for c=1:size(matrix_rand,2)
end
end
end
% But how to resconstruct through ifft?
This is the demo image:

Bruno Luong on 14 Sep 2022
Edited: Bruno Luong on 14 Sep 2022
Discrete Fourier transform always has unwanted artefact on finite region, due to boundary wrapping jumps, even for rectangular region.
The way people overcome is to multiply the data by window functions https://en.wikipedia.org/wiki/Window_function to smooth out the boundary, then take the FFT to get a less affectef spectrum.
You could do the same with you disk-region image by (1) multiplying the image with a tensorial of two window functions then (2) taking the FFT on the bounding box rectangle to find the spectrum.
William on 14 Sep 2022
Edited: William on 14 Sep 2022
The reason why I have this question is that I need to scramble the phase of pictures (excluing transparent parts) and keep power of each frequency unchanged.
The mask we can get from a PNG image is also discrete. So there are no window functions, I think.
img='ballon.PNG';
Though your idea looks really reasonable, I don't know what exacty it means because of my poor math.
Do you know how to modify my original codes to implement your ideas?
These are the codes for scrambling the phase of pictures without transparent parts and keeping power of each frequency unchanged:
%%%%
img='picture.jpg';
im_norm = This_image-mean(This_image(:));
phase_random = angle(fft2(rand(size(im_norm,1), size(im_norm,2))));
for rgb=1:3
im_random_phase(:,:,rgb)=ifft2(abs(fft2(im_norm(:,:,rgb))).*exp(sqrt(-1)*(angle(fft2(im_norm(:,:,rgb)))+phase_random)));
end
results_image= real(im_random_phase)+mean(This_image(:));
imshow(unit8(results_image))
%%%%