How to do a Fourier analysis with a mask?

37 views (last 30 days)
William
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!

Answers (2)

Walter Roberson
Walter Roberson on 7 Sep 2022
Use
[A,MAP,ALPHA] = imread(...)
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
  7 Comments
William
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';
This_image=double(imread(img));
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';
[A,MAP,ALPHA] = imread(img);
mask = ALPHA > 0.5;
This_image=double(imread(img));
im_norm = This_image-mean(This_image(mask));
matrix_rand=rand(size(im_norm,1), size(im_norm,2));
% random factor
for c=1:size(matrix_rand,2)
if sum(mask(:,c)==1)~=0
matrix_rand(mask(:,c)==1,c)=fft(matrix_rand(mask(:,c)==1,c));
end
end
for r=1:size(matrix_rand,1)
if sum(mask(r,:)==1)~=0
matrix_rand(r,mask(r,:)==1) = fft(matrix_rand(r,mask(r,:)==1));
end
end
for c=1:size(matrix_rand,2)
if sum(mask(:,c)==1)~=0
phase_random (mask(:,c)==1,c)= angle(matrix_rand(mask(:,c)==1,c));
end
end
%% do FFT seperately
for rgb=1:3
for c=1:size(matrix_rand,2)
if sum(mask(:,c)==1)~=0
fft_results(mask(:,c)==1,c,rgb)=fft(im_norm(mask(:,c)==1,c,rgb));
end
end
for r=1:size(matrix_rand,1)
if sum(mask(r,:)==1)~=0
fft_results(r,mask(r,:)==1,rgb)=fft(fft_results(r,mask(r,:)==1,rgb));
end
end
end
for rgb=1:3
for c=1:size(matrix_rand,2)
if sum(mask(:,c)==1)~=0
power(mask(:,c)==1,c,rgb)=abs(fft_results(mask(:,c)==1,c,rgb));
phase(mask(:,c)==1,c,rgb)=angle(fft_results(mask(:,c)==1,c,rgb));
end
end
end
% But how to resconstruct through ifft?
This is the demo image:

Sign in to comment.


Bruno Luong
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.
  1 Comment
William
William on 14 Sep 2022
Edited: William on 14 Sep 2022
Thank you for your reply!
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';
[A,MAP,ALPHA] = imread(img);
mask = ALPHA > 0.5;
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';
This_image=double(imread(img));
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))
%%%%

Sign in to comment.

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!