Random phase generator inside a iteration loop
3 views (last 30 days)
Show older comments
I am writing a phase retrieval codes, I need a random phase generator inside an iteration loop, but i don't really know how to code it inside my codes. The criteria to exit the loop is that the retrieved phase from source image is equal to the phase of the target image. May I get some help on this? Thank you.
These are my codes:
clear all; close all; clc; workspace;
%Read and import images
imgA = imread('image1.jpg');
imgB = imread('image2.jpg');
%-------------------Make both images the same size-----------------------%
%Get the size of image A
[rowsimgA colsimgA numberOfColorChannelsimgA] = size(imgA);
%Get the size of image B
[rowsimgB colsimgB numberOfColorChannelsimgB] = size(imgB);
%Check if lateral sizes match
if rowsimgB ~= rowsimgA || colsimgA ~= colsimgB
imgB = imresize(imgB, [rowsimgA colsimgA]);
end
%Display images
figure, imshow(imgA)
title('First Image')
figure, imshow(imgB)
title('Second Image')
%Convert images to double
imgA = im2double(imgA);
imgB = im2double(imgB);
%2D FFT
fftA = fft2(imgA);
fftB = fft2(imgB);
%Decompose images into amplitude only
imgA_amp = abs(fftA);
imgB_amp = abs(fftB);
%Decompose images into phase only
imgA_phase = angle(fftA);
imgB_phase = angle(fftB);
%Display images with amplitude only
figure, imshow(abs(fftA));
title('Amplitude of first image')
figure, imshow(abs(fftB));
title('Amplitude of second image')
%Display images with phase only
figure, imshow(angle(fftA));
title('Phase of first image')
figure, imshow(angle(fftB));
title('Phase of second image')
%Switch the amplitude and phase of both images
out1 = imgA_amp .* exp(imgB_phase);
out2 = imgB_amp .* exp(imgA_phase);
%Inverse FFT
out1 = real(ifft2(out1));
out2 = real(ifft2(out2));
%Display the images
figure;
imshow(out1, []);
figure;
imshow(out2, []);
0 Comments
Answers (1)
Deepak
on 18 Oct 2024
From my understanding, you are working on a phase retrieval algorithm in MATLAB, which involves manipulating the phase of images to match a target image’s phase.
You want to incorporate a random phase generator within an iterative loop. The loop should continue running until the phase derived from your source image matches the phase of the target image closely enough.
To achieve this, we can add a while loop to iteratively generate random phases until the retrieved phase matches the target phase within a specified tolerance. Each iteration produces a new random phase using “rand(size(imgA_phase))”, and the code checks the phase difference between the retrieved and target images. The loop exists when this difference is within the desired tolerance.
Please find below the MATLAB code to accomplish this task:
clear all; close all; clc; workspace;
imgA = imread('image1.jpg');
imgB = imread('image2.jpg');
% Make both images the same size
[rowsimgA, colsimgA, ~] = size(imgA);
[rowsimgB, colsimgB, ~] = size(imgB);
if rowsimgB ~= rowsimgA || colsimgA ~= colsimgB
imgB = imresize(imgB, [rowsimgA colsimgA]);
end
% Convert images to double
imgA = im2double(imgA);
imgB = im2double(imgB);
% 2D FFT
fftA = fft2(imgA);
fftB = fft2(imgB);
% Decompose images into amplitude and phase
imgA_amp = abs(fftA);
imgB_amp = abs(fftB);
imgA_phase = angle(fftA);
imgB_phase = angle(fftB);
maxIter = 1000;
tolerance = 1e-2; % Tolerance for phase matching
iteration = 0;
phaseMatched = false;
while ~phaseMatched && iteration < maxIter
iteration = iteration + 1;
% Generate random phase
randomPhase = 2 * pi * rand(size(imgA_phase));
% Switch the amplitude of imgA with the random phase
combinedFFT = imgA_amp .* exp(1i * randomPhase);
% Inverse FFT to get the retrieved image
retrievedImg = real(ifft2(combinedFFT));
% Compute the phase of the retrieved image
retrievedPhase = angle(fft2(retrievedImg));
% Check if retrieved phase is approximately equal to target phase
phaseDifference = angle(exp(1i * (retrievedPhase - imgB_phase)));
if max(abs(phaseDifference(:))) < tolerance
phaseMatched = true;
end
end
if phaseMatched
disp(['Phase matched in ', num2str(iteration), ' iterations.']);
else
disp('Maximum iterations reached without matching phase.');
end
figure;
imshow(retrievedImg, []);
title('Retrieved Image with Random Phase');
Please find attached the documentation of functions used for reference:
I hope this assists in resolving the issue.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!