Gerchberg–Saxton Algorithm with error criterion and padding

3 views (last 30 days)
Hi, I am new to Gerchberg–Saxton Algorithm, I am writing the codes for this algorithm. Unfortunately, I am facing 2 problems that stopping me from completing the codes. Firstly, I need to insert an error criterion to compare the estimated value and the true value of the image but I'm not sure how to wite the codes for this part. Secondlly, I need to ensure that only at pixel locations where the amplitude is non-zero, the phase will have a value. At other pixel locations where the amplitude is 0, the phase will be 0 but I also not sure how to wite the codes for this part.as well. I hope that any of you can help me on this 2 parts, I really appriciate the helps! Thank you.
These are the codes for now:
clear all; close all; clc
%Read and display source image
imgA = imread('void.jpg');
imgA = rgb2gray(imgA);
figure(1); imshow(imgA); title('Image from Source');
%Convert source image to Grayscale
imgB = imread('void2.jpg');
imgB = rgb2gray(imgB);
figure(2); imshow(imgB); title('Grayscale Image');
%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
imgA = im2double(imgA);
imgB = im2double(imgB);
error = [];
iteration = 200;
image_void = fftshift(imgB);
image_void = ifft2(image_void);
image_void = fftshift(image_void);
%Decompose into amplitude and phase (image is converted into double)
for i=1:iteration
image_fft_recon = abs(imgA) .* exp(1i*angle(image_void));
image_recon = fftshift(image_fft_recon);
image_recon = fft2(image_recon);
image_recon = fftshift(image_recon);
image_fft_recon2 = abs(imgB) .* exp(1i*angle(image_recon));
image_void = fftshift(image_fft_recon2);
image_void = ifft2(image_void);
image_void = fftshift(image_void);
end
figure(3);
imagesc(abs(image_void)),colorbar;
title('exponential img');
figure(4);
imagesc(abs(image_fft_recon)), colorbar;
title('constructed img');
  4 Comments
Walter Roberson
Walter Roberson on 20 Aug 2020
Then the previous ones are not needed anymore and can be closed?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!