图像的 SNR 和 PSNR 的计算1

</div><!--end: blogTitle 博客的标题和副标题 -->
<div id="navigator">
	<div class="blogStats">
		
		<div id="blog_stats">

随笔-466 
文章-9 
评论-7 

	</div><!--end: blogStats -->
</div><!--end: navigator 博客导航栏 -->
    <div id="post_detail">

图像的 SNR 和 PSNR 的计算

PSNR 的公式很容易搜到。

http://www.360doc.com/content/12/0605/21/4129998_216244993.shtml
http://blog.sina.com.cn/s/blog_455c7a600101ytgo.html

 

峰值信噪比经常用作图像压缩等领域中信号重建质量的测量方法,它常简单地通过均方差(MSE)进行定义。两个m×n单色图像IK,如果一个为另外一个的噪声近似,那么它们的的均方差定义为:

峰值信噪比定义为:

 

代码实现(参考: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 &lt;= 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-&gt;height*src1-&gt;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 -&gt; [height][width]</span>
<span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;i&lt;src1-&gt;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&lt;src1-&gt;widthStep;j=j+<span style="color: #800080;">3</span><span style="color: #000000;">){
        blue1</span>=(<span style="color: #0000ff;">int</span>)(uchar)src1-&gt;imageData[i*src1-&gt;widthStep+j];<span style="color: #008000;">//</span><span style="color: #008000;">Blue</span>
        green1=(<span style="color: #0000ff;">int</span>)(uchar)src1-&gt;imageData[i*src1-&gt;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-&gt;imageData[i*src1-&gt;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-&gt;imageData[i*src2-&gt;widthStep+j];<span style="color: #008000;">//</span><span style="color: #008000;">Blue</span>
        green2=(<span style="color: #0000ff;">int</span>)(uchar)src2-&gt;imageData[i*src2-&gt;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-&gt;imageData[i*src2-&gt;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>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">sigma: </span><span style="color: #800000;">"</span>&lt;&lt;sigma&lt;&lt;<span style="color: #000000;">endl;;
cout</span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">MSE: </span><span style="color: #800000;">"</span>&lt;&lt;MSE&lt;&lt;<span style="color: #000000;">endl;;
cout</span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">PSNR: </span><span style="color: #800000;">"</span>&lt;&lt;PSNR&lt;&lt;<span style="color: #000000;">endl;;
cout</span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">SNR: </span><span style="color: #800000;">"</span>&lt;&lt;SNR&lt;&lt;<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;

}

复制代码

 

分类: OpenCV
0
0
« 上一篇: swprintf %s %ws %S 的区别
» 下一篇: VirtualBox Win7 虚拟机 共享文件夹设置
	</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">

公告

昵称: rldts
园龄: 6年
粉丝: 13
关注: 0
+加关注
		<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;">&lt;</a></td><td align="center">2019年4月</td><td class="CalNextPrev" align="right"><a href="javascript:void(0);" "loadBlogCalendar('2019/05/01');return false;">&gt;</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">
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值