what is wrong whith my script?

I need to calculate the SNR but this code wrong
code:
for i=0:(size(im,1)-1)
for j=0:(size(im,2)-1)
som1 = som1+im(i,j)^2;
som2 = som2+(noisy_im(i,j)-im(i,j))^2;
end
SNR = 10*log10(som1/som2)
end
where is the error?

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2014 年 7 月 17 日
編集済み: Andrei Bobrov 2014 年 7 月 17 日

0 投票

It's MATLAB
n = size(im);
SNR = zeros(n(1),1);
som1 = 0;
som2 = 0;
for ii=1:n(1)
for jj=1:n(2)
som1 = som1+im(ii,jj)^2;
som2 = som2+(noisy_im(ii,jj)-im(ii,jj))^2;
end
SNR(ii) = 10*log10(som1/som2);
end
vectorize variant:
a = sum(im.^2,2);
b = sum((noisy_im-im).^2,2);
SNR = 10*log10(cumsum(a)./cumsum(b));

2 件のコメント

Andrei Bobrov
Andrei Bobrov 2014 年 7 月 17 日
corrected
Image Analyst
Image Analyst 2014 年 7 月 17 日
They do different things. The loop version gives the SNR of every row (but not really) while the vectorized is of the whole image. I said "not really" because you're not reinitializing som1 and som2 to 0 at the beginning of each row. So it's some sort of cumulative SNR, which is hard to interpret.

サインインしてコメントする。

Image Analyst
Image Analyst 2014 年 7 月 17 日

0 投票

Your first error is starting with 0 as the index. In MATLAB indices start with 1. The next error is that SNR gets overwritten in each row so it will end up with only a single value, not an array like you would get if you had an index for SNR. The SNR can be pulled out of both loops and be calculated after the loops exit.
You appear to want to calculate the peak signal to noise ratio. There is a function in the Image Processing Toolbox for that:
SNR = psnr(noisy_im, im);

3 件のコメント

nahla
nahla 2014 年 7 月 17 日
I'm debutant
I need to calculate the SNR of an image then
if the SNR < threshold I denoise the image else I do any thing but the problem here is to calculate the SNR of an image I should denoise it by using fitler!
the question: there is an other solution to test the image noise before his amelioration?
Image Analyst
Image Analyst 2014 年 7 月 17 日
But you already have the "noise free" image - you called "im". So why do you need to denoise anything?
nahla
nahla 2014 年 7 月 18 日
in the first I denoised my original image "noisy_im" to get the filtred image "im" then I calculate the psnr(noisy_im,im) I do all that to evaluate the quality of the original image "noisy_im". I saw that it is not logic that I denoise the image to evaluate its quality. So I want to know if i can evaluate image quality without denoising it? and then if the image has a bad quality I will denoise it. else I will keep it. i hope that is clear now

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2014 年 7 月 17 日

コメント済み:

2014 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by