i need matrix n which generated

snr=mean2(I./n)=0.5 ;where I is original image matrix and n is a noise matrix of same size of I.
if i have 'I',i need matrix n which generated using n=mm+sqrt(vv)*randn(size(I)) such i can get snr=0.5.

2 comentarios

José-Luis
José-Luis el 3 de En. de 2013
What have you tried so far?
Image Analyst
Image Analyst el 3 de En. de 2013
Lots of things (click on his name), but he has never asked the right questions until now. He may have it right now, but I'm not sure.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 3 de En. de 2013
Editada: Image Analyst el 3 de En. de 2013

0 votos

If n = 2*I, on average, then the SNR will be 1/2. So what do you get if you scale n so that the mean of n equals twice the mean of I? Give that a shot.
[Edit/addition]
Haven't heard from you so I guess you're having trouble. Here, run this code for a demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% 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 Grayscale 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')
colorbar;
% Get the mean of the image
meanGL = mean2(grayImage)
% Specify the variance, it can be anything at all. It doesn't matter.
% Ask user for a number.
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; % Bail out if they clicked Cancel.
desiredStandardDeviation = str2num(cell2mat(caUserInput));
% Check for a valid number.
if isnan(desiredStandardDeviation)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
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;
% Create the noise image using his formula:
noiseImage = 2 * meanGL + sqrt(desiredvariance)*randn(size(grayImage));
% Don't allow noise to be negative
noiseImage = max(noiseImage, 0);
% Display the image.
subplot(2, 3, 2);
imshow(noiseImage, []);
colorbar;
title('Noise Image', 'FontSize', fontSize);
% Calculate the SNR
SNR = mean2(double(grayImage) ./ noiseImage)
% Add them together
noisyImage = double(grayImage) + noiseImage;
% Display the image.
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));
% Because it's random, you can't make it exactly 0.5 until you know what it actually is.
noiseImage2 = noiseImage * SNR / 0.5;
% Display the image.
subplot(2, 3, 5);
imshow(noiseImage2, []);
colorbar;
title('Noise Image', 'FontSize', fontSize);
% Add them together
noisyImage2 = double(grayImage) + noiseImage2;
% Now it should be 0.5
% Calculate the SNR
SNR2 = mean2(double(grayImage) ./ noiseImage2)
% Display the image.
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);

7 comentarios

vipul utsav
vipul utsav el 5 de En. de 2013
if i want snr=0.9 then how can i generated n using
mean+sqrt(variance)*randn(size(grayImage));?
because in your above code if i change variance in
noiseImage = 2 * meanGL + sqrt(desiredvariance)*randn(size(grayImage)); then
snr remains 0.5 same.(not depends on variance),but noise is depends on variance,
so i need noise matrix 'n' such that i can get my desired snr value('n' is depends on variance and it is generated using mean+sqrt(variance)*randn(size(grayImage)))
You need to study to code so you understand it. See the line:
noiseImage2 = noiseImage * SNR / 0.5;
? That 0.5 was your your desired SNR. Change it to whatever you want.
noiseImage2 = noiseImage * SNR / 0.8;
i have studied your whole code,,then i got noiseImage2 using above line that gives me 0.8 SNR. it is ok
But,this is a fix matrix which gives 0.8 SNR, if i find variance of noiseImage2 and then i generate noiseImage of variance equal to noiseImage2(1086.72) using
noiseImage = 2 * meanGL + sqrt(desiredvariance)*randn(size(grayImage))
then it gives me a 0.5 SNR,but i want 0.8 SNR,
i hope you understand my comment and you would give me right solution
Regarding your "Answer" below (which is not really an Answer, is it, it's really a comment to my Answer)...
You still do not understand the formulas. See that 2? Recall that that 2 came from you wanting an SNR of 1/2. So it should be 1/0.8. Here's what the lines should be to be more general.
% Create the noise image using his formula:
desiredSNR = 0.8;
noiseImage = meanGL/desiredSNR + sqrt(desiredvariance)*randn(size(grayImage));
% Don't allow noise to be negative
noiseImage = max(noiseImage, 0.01);
and then later on down in the code:
noiseImage2 = noiseImage * SNR / desiredSNR;
This is what happens when you give a very very specific case and don't let us know in the beginning what factors are supposed to be variable/general. In the future, let us know what factors are to be held constant and what factors might you want to vary. Thanks.
vipul utsav
vipul utsav el 6 de En. de 2013
Editada: vipul utsav el 6 de En. de 2013
if any element in noiseImage is '0' then SNR becomes infinite, so can i neglect this pixel?
and if i neglect it,it definitely effect on mean and variance of noiseImage but how much amount it effects on variance of noiseImage?
Image Analyst
Image Analyst el 6 de En. de 2013
I had that case and I got inf, so you can see that I changed the min allowable value to 0.01. Doing that didn't affect the SNR much as compared to ignoring that pixel totally because there were so few pixels it affected.
Image Analyst
Image Analyst el 7 de En. de 2013
You might try checking out this File Exchange submission: http://www.mathworks.com/matlabcentral/fileexchange/25645

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by