MATLAB: How can create matrix(image matrix) of 3db snr(signal to noise ratio) in matlab

image matrixImage Processing Toolboxsnr

how can create matrix(image matrix) of 3db snr(signal to noise ratio) in matlab?

Best Answer

Well since this sounds like homework I can't give you the answer outright, but I think this demo will help you. You can enter in different noise amounts and it will add that noise to an image and calculate the SNR in dB for you.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.

folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.

subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Noise-Free Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.


[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 3, 4);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize);
xlim([0 255]); % Scale x axis manually.

while true
% Add noise to this image.
% Ask user for a number.
defaultValue = 16;
titleBar = 'Enter a noise value';
userPrompt = 'Enter the noise value';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput)
break;
end; % Bail out if they clicked Cancel.
v = str2double(cell2mat(caUserInput));
% Check for a valid integer.
if isnan(v)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
v = defaultValue;
message = sprintf('I said it had to be an number.\nI will use %f and continue.', v);
uiwait(warndlg(message));
end
noiseImage = v * randn(size(grayImage));
% Display the original gray scale image.
subplot(2, 3, 2);
imshow(noiseImage, []);
title('Noise-Only Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = hist(noiseImage(:), 256);
subplot(2, 3, 5);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Noise-Only Image', 'FontSize', fontSize);
% Add it to the image.
imageWithNoise = double(grayImage) + noiseImage;
subplot(2, 3, 3);
imshow(imageWithNoise, []);
title('Image with Noise Added', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = hist(imageWithNoise(:), 256);
subplot(2, 3, 6);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Noisy Image', 'FontSize', fontSize);
% Make range at least 255, bigger if necessary.
xl = xlim;
if xl < 255
xlim([0 255]); % Scale x axis manually.
end
% Calculate SNR over all pixels and then take the mean.
SNRdB = mean2(20 *log10(double(grayImage) ./ abs(noiseImage)));
message = sprintf('The SNR = %.2f dB', SNRdB)
promptMessage = sprintf('%s\n\nDo you want to Continue processing,\nor Cancel to abort processing?', message);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end