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

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

Accepted Answer

Image Analyst
Image Analyst on 26 Dec 2012
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
  2 Comments
vipul utsav
vipul utsav on 27 Dec 2012
SNRdB = mean2(20 *log10(double(grayImage) ./ abs(noiseImage)));
why here 20log(s/n) (instead of 10log(s/n)) ?
And one another point, i have already known your suggestion before this post in forum ,but i want another method.
Image Analyst
Image Analyst on 27 Dec 2012
See Wikipedia - there are two definitions. Use whatever definition you want. I'm sorry the demo code I wrote for you was of no use to you. Good luck in your search for another method. I don't know what it might be so I won't spend time guessing again.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 26 Dec 2012
exp(3/40)*randn(M,N)*sqrt(signalstrength)

Community Treasure Hunt

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

Start Hunting!