MATLAB Answers

Code take forever to run and output

1 view (last 30 days)
HI, when i was running my code, only the first 2 figure shown up (Figure 1 and Figure 2), the others are not showing up. I run the code and leave it for more than an hour but only the first 2 figure shown up. The code run for more than an hour to run and it does not output the rest of the figures. May I know what are the problems?
codes:
clear all; close all; clc
%Read and display source image
imgA = imread('test1.jpg');
imgA = rgb2gray(imgA);
figure(1); imshow(imgA); title('Image from Source');
%Convert source image to Grayscale
imgB = imread('test2.jpg');
imgB = rgb2gray(imgB);
figure(2); imshow(imgB); title('Image from Intensity');
%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 = 468;
iteration = 1100;
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
if error>464
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);
%RMSE Error
error = [error; (1/sqrt(rowsimgA*colsimgA)*sqrt(sum(sum(abs(image_recon)-abs(imgB)).^2)))];
else
g=1;
break;
end
end
fprintf('No. of iterations required to meet the error threshold is %i\n',i)
figure (5)
i = 1:1:length(error);
plot(i,(error'));
title('Error');
grid on
% Verification of amplitude and phase values
C1=abs(image_fft_recon./image_fft_recon);
C2=imgA./imgA;
if 'C1=C2'
disp ('Pixel Locations where the amplitude is non-zero the phase has a value')
else
disp('answers not verified')
end
figure(3);
imagesc(abs(image_void)),colorbar;
title('exponential img');
figure(4);
imagesc(abs(image_fft_recon)), colorbar;
title('constructed img');
phase_image=(abs(image_fft_recon));
figure(6),plot(phase_image), title('Phase diagram');

  0 Comments

Sign in to comment.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 30 Sep 2020
Edited: KALYAN ACHARJYA on 30 Sep 2020
Sir, I have tested the code with low iteration numbers, say 10 and it is working perfectly. Yes if you consider the iteration number to be higher, as described in code 1100, it seems that the code may take longer time of execution
iteration = 1100;
Alos please note on the error condition within the for loop, as all plot data are depends on the variables described within the if condition.
if error>464

  3 Comments

Soong Wei Kang
Soong Wei Kang on 30 Sep 2020
Hi sir, thank you for your reply. I am not too sure is it something wrong about the images. I tried the code with different images with the same amout of the iteration(1100) it runs perfectly and the outputs are shown within few seconds, i do not understand why it takes forever to run with these 2 images.
Soong Wei Kang
Soong Wei Kang on 30 Sep 2020
These are the 2 images that I tried and the code run perfectly with 1100 iterations, but with the images i posted earlier it takes forever to run. May I know why is this happening?
KALYAN ACHARJYA
KALYAN ACHARJYA on 30 Sep 2020
Yes, these pictures quite smaller than the original pictures. I am glad that the problem has been resolved.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!