(原創) 如何計算SNR (signal-to-ratio)? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)

本文详细介绍了一种信号噪声比(SNR)的计算方法,并通过C++代码实现,展示了如何对比原始图像与加噪图像,计算其SNR值。代码中包含了图像灰度转换、信号均值与方差计算等关键步骤。

SNR公式如下

 1ExpandedBlockStart.gifContractedBlock.gif/**//* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : SNR.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / C++/CLI
 6InBlock.gifDescription : Demo how to compute SNR
 7InBlock.gifRelease     : 12/20/2006 1.0
 8ExpandedBlockEnd.gif*/

 9None.gif#include "stdafx.h"
10None.gif#include <iostream>
11None.gif#include <cmath>
12None.gif
13None.gifusing namespace System::Drawing;
14None.gifusing namespace System::Drawing::Imaging;
15None.gifusing namespace std;
16None.gif
17None.gif// Convert RGB to gray level int
18None.gifint colorToInt(Color);
19None.gif
20None.gif// Calculate signal to ratio
21None.gifdouble SNR(Bitmap^, Bitmap^);
22None.gif
23ExpandedBlockStart.gifContractedBlock.gifint main() dot.gif{
24InBlock.gif  // Read lena.jpg
25InBlock.gif  Bitmap^ oriImg = gcnew Bitmap("lena.jpg");
26InBlock.gif  // Declare Gaussian image for lena.jpg
27InBlock.gif  Bitmap^ noiseImg = gcnew Bitmap("lena_gaussian.jpg");
28InBlock.gif
29InBlock.gif  double snr = SNR(oriImg, noiseImg);
30InBlock.gif
31InBlock.gif  cout << "SNR of image:" << snr << endl;
32InBlock.gif
33InBlock.gif  return 0;
34ExpandedBlockEnd.gif}

35None.gif
36None.gif// Convert RGB to gray level int
37ExpandedBlockStart.gifContractedBlock.gifint colorToInt(Color color) dot.gif{
38InBlock.gif  return (color.R + color.G + color.B) / 3;
39ExpandedBlockEnd.gif}

40None.gif
41None.gif
42None.gif// Calculate signal to ratio
43ExpandedBlockStart.gifContractedBlock.gifdouble SNR(Bitmap^ oriImg, Bitmap^ noiseImg) dot.gif{
44InBlock.gif
45InBlock.gif  // ||n||
46InBlock.gif  int n = oriImg->Height * oriImg->Width;
47InBlock.gif
48InBlock.gif  // compute mean of signal
49InBlock.gif  double sumU = 0.0;
50ExpandedSubBlockStart.gifContractedSubBlock.gif  for(int y = 0; y != oriImg->Height; ++y) dot.gif{
51ExpandedSubBlockStart.gifContractedSubBlock.gif    for(int x = 0; x != oriImg->Width; ++x) dot.gif{
52InBlock.gif      sumU += (double)colorToInt(oriImg->GetPixel(x, y));
53ExpandedSubBlockEnd.gif    }

54ExpandedSubBlockEnd.gif  }

55InBlock.gif  double u = (double)sumU / n;
56InBlock.gif  
57InBlock.gif  // compute variance of signal
58InBlock.gif  double diffVS = 0.0, sumVS  = 0.0;
59ExpandedSubBlockStart.gifContractedSubBlock.gif  for (int y = 0; y != oriImg->Height; ++y) dot.gif{
60ExpandedSubBlockStart.gifContractedSubBlock.gif    for (int x = 0; x != oriImg->Width; ++x) dot.gif{
61InBlock.gif      diffVS = (double)colorToInt(oriImg->GetPixel(x, y)) - u;
62InBlock.gif      sumVS += diffVS * diffVS;
63ExpandedSubBlockEnd.gif    }

64ExpandedSubBlockEnd.gif  }

65InBlock.gif  double VS = (double)sumVS / n;
66InBlock.gif
67InBlock.gif  // compute mean of noise
68InBlock.gif  double sumN = 0.0;
69ExpandedSubBlockStart.gifContractedSubBlock.gif  for (int y = 0; y != noiseImg->Height; ++y) dot.gif{
70ExpandedSubBlockStart.gifContractedSubBlock.gif    for (int x = 0; x != noiseImg->Width; ++x) dot.gif{
71InBlock.gif      sumN += (colorToInt(noiseImg->GetPixel(x, y)) - colorToInt(oriImg->GetPixel(x, y)));
72ExpandedSubBlockEnd.gif    }

73ExpandedSubBlockEnd.gif  }

74InBlock.gif  double un = (double)sumN / n;
75InBlock.gif
76InBlock.gif  // compute variance of noise
77InBlock.gif  double diffVN = 0.0, sumVN = 0.0;
78ExpandedSubBlockStart.gifContractedSubBlock.gif  for (int y = 0; y != noiseImg->Height; ++y) dot.gif{
79ExpandedSubBlockStart.gifContractedSubBlock.gif    for (int x = 0; x != noiseImg->Width; ++x) dot.gif{
80InBlock.gif      diffVN = (double)colorToInt(noiseImg->GetPixel(x, y)) -colorToInt(oriImg->GetPixel(x, y))- un;
81InBlock.gif      sumVN += diffVN * diffVN;
82ExpandedSubBlockEnd.gif    }

83ExpandedSubBlockEnd.gif  }

84InBlock.gif  double VN = (double)sumVN / n;
85InBlock.gif
86InBlock.gif  return 20 * log10(sqrt(VS) / sqrt(VN));
87ExpandedBlockEnd.gif}


執行結果

None.gifSNR of image:0.16249
None.gif請按任意鍵繼續 . . .


原圖



Noise圖片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值