How to Fourier Phase Scramble a specific area of an image?

조회 수: 18 (최근 30일)
Neuro
Neuro 2023년 4월 7일
댓글: Neuro 2023년 5월 4일
Hi. I have a set of face images that I want to Fourier Phase scramble, excluding the pixels outside of the face contour. This is a sample image (see below): All the pixels outside the fase contour have the same RGB values. I need to Fourier Phase scramble only the pixels inside the contour; in other words, the pixels that belong to the face. Could anyone help me with the code? I'm not very experienced at image processing! Thanks so much in advance!
  댓글 수: 2
Image Analyst
Image Analyst 2023년 4월 7일
The masking is easy. Can you give us your phase scrambling code?
Neuro
Neuro 2023년 5월 4일
Thanks! How can you apply the mask so both the original photo and the scrambled one have that gray in the background?
Here is the code, as shared by @Nayan
im = imread('image.jpg');
% Convert the image to grayscale
im_gray = im2gray(im);
% Compute the Fourier transform of the image
im_fft = fft2(double(im_gray));
% Extract the magnitude and phase information
im_mag = abs(im_fft);
im_phase = angle(im_fft);
% Randomize the phase information
[N,M] = size(im_phase);
rand_phase = exp(2*pi*1i*rand(N,M));
im_phase_scrambled = im_phase.*rand_phase;
% Combine the randomized phase information with the original magnitude information
im_scrambled_fft = im_mag.*exp(1i*im_phase_scrambled);
% Compute the inverse Fourier transform to obtain the phase-scrambled image
im_scrambled = ifft2(im_scrambled_fft);
% Convert the phase-scrambled image back to uint8 data type
im_scrambled = uint8(real(im_scrambled));
% Display the original and phase-scrambled images side by side
subplot(1,2,1), imshow(im_gray), title('Original');
subplot(1,2,2), imshow(im_scrambled), title('Phase-scrambled');

댓글을 달려면 로그인하십시오.

채택된 답변

Nayan
Nayan 2023년 4월 11일
Hi,
As I understand you need to perform phase scrambling on the image. Phase scrambling is a technique used in signal processing to disrupt the phase information of a signal while preserving its power spectrum. In Matlab, you can perform phase scrambling on a signal using the following basic steps :-
  1. Read the image using "imread(filename)".
  2. Calculate the FFT of the image using fft2(X)
  3. Extract the magnitude and phase information from the obtained FFT.
  4. Scramble the phase by multiplying with a random phase.
  5. Recreate the FFT by multiplying the random phase with the magnitude of the original image.
  6. Taking in IFFT using ifft2(x) will provide the phase scrambled image.
Hope this helps!
Refer the following code sinppet for help.
im = imread('image_file_name');
% Convert the image to grayscale
im_gray = rgb2gray(im);
% Compute the Fourier transform of the image
im_fft = fft2(double(im_gray));
% Extract the magnitude and phase information
im_mag = abs(im_fft);
im_phase = angle(im_fft);
% Randomize the phase information
[N,M] = size(im_phase);
rand_phase = exp(2*pi*1i*rand(N,M));
im_phase_scrambled = im_phase.*rand_phase;
% Combine the randomized phase information with the original magnitude information
im_scrambled_fft = im_mag.*exp(1i*im_phase_scrambled);
% Compute the inverse Fourier transform to obtain the phase-scrambled image
im_scrambled = ifft2(im_scrambled_fft);
% Convert the phase-scrambled image back to uint8 data type
im_scrambled = uint8(real(im_scrambled));
% Display the original and phase-scrambled images side by side
subplot(1,2,1), imshow(im_gray), title('Original');
subplot(1,2,2), imshow(im_scrambled), title('Phase-scrambled');
  댓글 수: 1
Neuro
Neuro 2023년 5월 4일
Thanks so much for the code! I had use the function im2gray instead of rgb2gray (see line 3). But it should be the same, right?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by