facing error in the below code. please help
1 view (last 30 days)
Show older comments
% Parameters
inputImage = imread('cameraman.tif'); % Load the input image
integrationTime = 0.1; % Integration time for photon counting
% Step 1: Initialization
[height, width] = size(inputImage);
H1 = exp(1i * 2 * pi * rand(height, width)); % Random phase mask 1
H2 = exp(1i * 2 * pi * rand(height, width)); % Random phase mask 2
% Step 2: Encryption
inputImage = double(inputImage);
S = inputImage .* H1; % Intermediate image
S_hat = fftshift(fft2(S)); % Spectrum of intermediate image
E_hat = nonlinearOperation(S_hat); % Nonlinear operation on the spectrum
E = ifft2(ifftshift(E_hat)); % Encrypted image
% Step 3: Photon Counting
photonCounts = countPhotons(E, integrationTime); % Photon counting simulation
% Step 4: Decryption
E_hat_restored = inverseNonlinearOperation(E_hat); % Inverse of nonlinear operation
S_hat_restored = E_hat_restored .* conj(H2); % Restored spectrum
I = ifft2(ifftshift(S_hat_restored)); % Decrypted image
% Display results
figure;
subplot(2, 2, 1); imshow(inputImage, []); title('Input Image');
subplot(2, 2, 2); imshow(abs(E), []); title('Encrypted Image');
subplot(2, 2, 3); plot(photonCounts); title('Photon Counts');
subplot(2, 2, 4); imshow(abs(I), []); title('Decrypted Image');
% Load the original input image and the decrypted image
originalImage = imread('cameraman.tif');
decryptedImage = imread('decryptedimage.jpg');
%{
Resize the images if they have different sizes
if ~isequal(size(originalImage), size(decryptedImage))
originalImage = imresize(originalImage, size(decryptedImage));
end
%}
% Convert images to double precision and scale to the same data range
originalImage = im2double(originalImage);
decryptedImage = im2double(decryptedImage);
% Calculate MSE
mse = mean((originalImage(:) - decryptedImage(:)).^2);
% Determine maximum pixel value
maxPixelValue = 1; % Modify accordingly based on the data range
% Calculate PSNR
psnr = 10 * log10((maxPixelValue^2) / mse);
% Display the PSNR value
fprintf('PSNR: %.2f dB\n', psnr);
% Functions
function E_hat = nonlinearOperation(S_hat)
% Perform desired nonlinear operation on the spectrum
% Example: Squaring the spectrum
E_hat = S_hat.^2;
end
function photonCounts = countPhotons(E, integrationTime)
% Simulate photon counting by sampling the intensity values
% and counting photons within the integration time
photonCounts = sum(E(:)) * integrationTime;
end
function E_hat_restored = inverseNonlinearOperation(E_hat)
% Perform inverse of the nonlinear operation on the spectrum
% Example: Taking the square root of the spectrum
E_hat_restored = sqrt(E_hat);
end
2 Comments
Answers (1)
Drishti
on 8 Oct 2024
Hi Anshika,
While reproducing the provided code on my end, I encountered the same issue of ‘decryptedimage.jpg does not exist’.
This error can be resolved by loading the decrypted image into the current work directory. If the decrypted image is getting generated from the provided code, then saving it will resolve the error. You can refer to the code snippet to save the image.
% Save the decrypted image
imwrite(abs(I), 'decryptedimage.jpg');
Also the empty ‘Photon Count’ plot can be rectified by utilizing the ‘intensity’ for the calculation of ‘Photon Count’.
Refer to the implementation below:
function photonCounts = countPhotons(E, integrationTime)
% Calculate intensity
intensity = abs(E).^2;
photonCounts = poissrnd(intensity * integrationTime);
end
For more information, you can refer to the MATLAB Documentation of ‘imwrite’ function:
I hope this resolves 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!