</div><!--end: blogTitle 博客的标题和副标题 -->
<div id="navigator">
<div class="blogStats">
<div id="blog_stats">
随笔-466
文章-9
评论-7
</div><!--end: blogStats -->
</div><!--end: navigator 博客导航栏 -->
PSNR 的公式很容易搜到。
http://www.360doc.com/content/12/0605/21/4129998_216244993.shtml
http://blog.sina.com.cn/s/blog_455c7a600101ytgo.html
峰值信噪比经常用作图像压缩等领域中信号重建质量的测量方法,它常简单地通过均方差(MSE)进行定义。两个m×n单色图像I和K,如果一个为另外一个的噪声近似,那么它们的的均方差定义为:
峰值信噪比定义为:
代码实现(参考:http://stackoverflow.com/questions/29428308/snr-of-an-image-in-c-using-opencv)
double getPSNR(const Mat& I1, const Mat& I2)
{
Mat s1;
absdiff(I1, I2, s1); // |I1 - I2|
s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits
s1 = s1.mul(s1); // |I1 - I2|^2
Scalar s = sum(s1); // sum elements per channel
<span style="color: #0000ff;">double</span> sse = s.val[<span style="color: #800080;">0</span>] + s.val[<span style="color: #800080;">1</span>] + s.val[<span style="color: #800080;">2</span>]; <span style="color: #008000;">//</span><span style="color: #008000;"> sum channels</span>
<span style="color: #0000ff;">if</span>( sse <= 1e-<span style="color: #800080;">10</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> for small values return zero</span>
<span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
</span><span style="color: #0000ff;">double</span> mse =sse /(<span style="color: #0000ff;">double</span>)(I1.channels() *<span style="color: #000000;"> I1.total());
</span><span style="color: #0000ff;">double</span> psnr = <span style="color: #800080;">10.0</span>*log10((<span style="color: #800080;">255</span>*<span style="color: #800080;">255</span>)/<span style="color: #000000;">mse);
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> psnr;
}
}
SNR 不太好搜。
http://cg2010studio.com/2014/12/10/opencv-snr-%E8%88%87-psnr/
http://blog.csdn.net/lien0906/article/details/30059747
SNR (Signal to Noise Ratio):訊號雜訊比,簡稱訊雜比。
PSNR (Peak Signal to Noise Ratio):也是訊雜比,只是訊號部分的值通通改用該訊號度量的最大值。以訊號度量範圍為 0 到 255 當作例子來計算 PSNR 時,訊號部分均當成是其能夠度量的最大值,也就是 255,而不是原來的訊號。
代码实现(参考:http://cg2010studio.com/2014/12/10/opencv-snr-%E8%88%87-psnr/)
/**
Theme: SNR (Signal to Noise Ratio) & PSNR (Peak Signal to Noise Ratio)
compiler: Dev C++ 4.9.9.2
Library: OpenCV 2.0
Date: 103/12/10
Author: HappyMan
Blog: https://cg2010studio.wordpress.com/
*/
#include <cv.h>
#include <highgui.h>
#include<iostream>
using namespace std;
int main(){
IplImage *src1= cvLoadImage(“moon_o.BMP”);
IplImage *src2= cvLoadImage(“moon_m.BMP”);
</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> sigma = <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> squre = <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">double</span> MSE = <span style="color: #800080;">0.0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">double</span> SNR = <span style="color: #800080;">0.0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">double</span> PSNR = <span style="color: #800080;">0.0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> frameSize = src1->height*src1->width*<span style="color: #800080;">3</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> blue1=<span style="color: #800080;">0</span>, blue2=<span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> green1=<span style="color: #800080;">0</span>, green2=<span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> red1=<span style="color: #800080;">0</span>, red2=<span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> width x height -> [height][width]</span>
<span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;i<src1->height;i++<span style="color: #000000;">){
</span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> j=<span style="color: #800080;">0</span>;j<src1->widthStep;j=j+<span style="color: #800080;">3</span><span style="color: #000000;">){
blue1</span>=(<span style="color: #0000ff;">int</span>)(uchar)src1->imageData[i*src1->widthStep+j];<span style="color: #008000;">//</span><span style="color: #008000;">Blue</span>
green1=(<span style="color: #0000ff;">int</span>)(uchar)src1->imageData[i*src1->widthStep+j+<span style="color: #800080;">1</span>];<span style="color: #008000;">//</span><span style="color: #008000;">Green</span>
red1=(<span style="color: #0000ff;">int</span>)(uchar)src1->imageData[i*src1->widthStep+j+<span style="color: #800080;">2</span>];<span style="color: #008000;">//</span><span style="color: #008000;">Red</span>
blue2=(<span style="color: #0000ff;">int</span>)(uchar)src2->imageData[i*src2->widthStep+j];<span style="color: #008000;">//</span><span style="color: #008000;">Blue</span>
green2=(<span style="color: #0000ff;">int</span>)(uchar)src2->imageData[i*src2->widthStep+j+<span style="color: #800080;">1</span>];<span style="color: #008000;">//</span><span style="color: #008000;">Green</span>
red2=(<span style="color: #0000ff;">int</span>)(uchar)src2->imageData[i*src2->widthStep+j+<span style="color: #800080;">2</span>];<span style="color: #008000;">//</span><span style="color: #008000;">Red</span>
sigma+=(blue1-blue2)*(blue1-blue2)+<span style="color: #000000;">
(green1</span>-green2)*(green1-green2)+<span style="color: #000000;">
(red1</span>-red2)*(red1-<span style="color: #000000;">red2);
squre </span>+= blue1*blue1 + green1*green1 + red1*<span style="color: #000000;">red1;
}
}
MSE</span>=sigma/(<span style="color: #0000ff;">double</span><span style="color: #000000;">)frameSize;
PSNR</span>=<span style="color: #800080;">10</span>*log10(<span style="color: #800080;">255</span>*<span style="color: #800080;">255</span>/<span style="color: #000000;">MSE);
SNR </span>= <span style="color: #800080;">10</span>*log10(squre/<span style="color: #000000;">sigma);
cout</span><<<span style="color: #800000;">"</span><span style="color: #800000;">sigma: </span><span style="color: #800000;">"</span><<sigma<<<span style="color: #000000;">endl;;
cout</span><<<span style="color: #800000;">"</span><span style="color: #800000;">MSE: </span><span style="color: #800000;">"</span><<MSE<<<span style="color: #000000;">endl;;
cout</span><<<span style="color: #800000;">"</span><span style="color: #800000;">PSNR: </span><span style="color: #800000;">"</span><<PSNR<<<span style="color: #000000;">endl;;
cout</span><<<span style="color: #800000;">"</span><span style="color: #800000;">SNR: </span><span style="color: #800000;">"</span><<SNR<<<span style="color: #000000;">endl;;
system(</span><span style="color: #800000;">"</span><span style="color: #800000;">pause</span><span style="color: #800000;">"</span><span style="color: #000000;">);
cvWaitKey(</span><span style="color: #800080;">0</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> EXIT_SUCCESS;
}
</div>
<div class="postDesc">posted @ <span id="post-date">2016-03-31 10:39</span> <a href="https://www.cnblogs.com/qrlozte/">rldts</a> 阅读(<span id="post_view_count">14848</span>) 评论(<span id="post_comment_count">0</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=5340216" rel="nofollow">编辑</a> <a href="#" "AddToWz(5340216);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=144440,cb_entryId=5340216,cb_blogApp=currentBlogApp,cb_blogUserGuid='daffd0f4-978c-e211-9010-c3d7423e3b07',cb_entryCreatedDate='2016/3/31 10:39:00';loadViewCount(cb_entryId);var cb_postType=1;var isMarkdown=false;</script>
</div><!--end: forFlow -->
</div><!--end: mainContent 主体内容容器-->
<div id="sideBar">
<div id="sideBarMain">
公告
<div id="calendar"><div id="blog-calendar" style=""><table id="blogCalendar" class="Cal" title="Calendar" cellspacing="0" cellpadding="0">
<tbody><tr><td colspan="7"><table class="CalTitle" cellspacing="0">
<tbody><tr><td class="CalNextPrev"><a href="javascript:void(0);" "loadBlogCalendar('2019/03/01');return false;"><</a></td><td align="center">2019年4月</td><td class="CalNextPrev" align="right"><a href="javascript:void(0);" "loadBlogCalendar('2019/05/01');return false;">></a></td></tr>
</tbody></table></td></tr><tr><th class="CalDayHeader" abbr="日" scope="col" align="center">日</th><th class="CalDayHeader" abbr="一" scope="col" align="center">一</th><th class="CalDayHeader" abbr="二" scope="col" align="center">二</th><th class="CalDayHeader" abbr="三" scope="col" align="center">三</th><th class="CalDayHeader" abbr="四" scope="col" align="center">四</th><th class="CalDayHeader" abbr="五" scope="col" align="center">五</th><th class="CalDayHeader" abbr="六" scope="col" align="center">六</th></tr><tr><td class="CalOtherMonthDay" align="center">31</td><td align="center">1</td><td align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">5</td><td class="CalWeekendDay" align="center">6</td></tr><tr><td class="CalWeekendDay" align="center">7</td><td align="center">8</td><td align="center">9</td><td class="CalTodayDay" align="center">10</td><td align="center">11</td><td align="center">12</td><td class="CalWeekendDay" align="center">13</td></tr><tr><td class="CalWeekendDay" align="center">14</td><td align="center">15</td><td align="center">16</td><td align="center">17</td><td align="center">18</td><td align="center">19</td><td class="CalWeekendDay" align="center">20</td></tr><tr><td class="CalWeekendDay" align="center">21</td><td align="center">22</td><td align="center">23</td><td align="center">24</td><td align="center">25</td><td align="center">26</td><td class="CalWeekendDay" align="center">27</td></tr><tr><td class="CalWeekendDay" align="center">28</td><td align="center">29</td><td align="center">30</td><td class="CalOtherMonthDay" align="center">1</td><td class="CalOtherMonthDay" align="center">2</td><td class="CalOtherMonthDay" align="center">3</td><td class="CalOtherMonthDay" align="center">4</td></tr><tr><td class="CalOtherMonthDay" align="center">5</td><td class="CalOtherMonthDay" align="center">6</td><td class="CalOtherMonthDay" align="center">7</td><td class="CalOtherMonthDay" align="center">8</td><td class="CalOtherMonthDay" align="center">9</td><td class="CalOtherMonthDay" align="center">10</td><td class="CalOtherMonthDay" align="center">11</td></tr>
<div id="leftcontentcontainer">
<div id="blog-sidecolumn"><div id="sidebar_search" class="sidebar-block">