clc;
close all;
clear;
workspace;
format longg;
format compact;
fontSize = 20;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
colorbar;
meanGL = mean2(grayImage)
defaultValue = 50;
titleBar = 'Enter a value';
userPrompt = 'Enter the standard deviation (it will not affect the SNR)';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end;
desiredStandardDeviation = str2num(cell2mat(caUserInput));
if isnan(desiredStandardDeviation)
desiredStandardDeviation = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', desiredStandardDeviation);
uiwait(warndlg(message));
end
desiredvariance = desiredStandardDeviation ^2;
noiseImage = 2 * meanGL + sqrt(desiredvariance)*randn(size(grayImage));
noiseImage = max(noiseImage, 0);
subplot(2, 3, 2);
imshow(noiseImage, []);
colorbar;
title('Noise Image', 'FontSize', fontSize);
SNR = mean2(double(grayImage) ./ noiseImage)
noisyImage = double(grayImage) + noiseImage;
subplot(2, 3, 3);
imshow(noisyImage, []);
colorbar;
caption = sprintf('Noise Image with SNR = %.4f', SNR);
title(caption, 'FontSize', fontSize);
message = sprintf('The SNR for these images is %.4f', SNR);
uiwait(helpdlg(message));
noiseImage2 = noiseImage * SNR / 0.5;
subplot(2, 3, 5);
imshow(noiseImage2, []);
colorbar;
title('Noise Image', 'FontSize', fontSize);
noisyImage2 = double(grayImage) + noiseImage2;
SNR2 = mean2(double(grayImage) ./ noiseImage2)
subplot(2, 3, 6);
imshow(noisyImage2, []);
colorbar;
caption = sprintf('Noise Image with SNR = %.4f', SNR2);
title(caption, 'FontSize', fontSize);
message = sprintf('The SNR for these images is %.4f', SNR2);
helpdlg(message);