这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同。

先说下我的做法,a部分我将每个不同的窗口大小模糊化后的图像生成后,还计算了每个模糊化后的图像与原始图像间的MSE值与PSNR值。(参见:http://zh.wikipedia.org/wiki/%E5%B3%B0%E5%80%BC%E4%BF%A1%E5%99%AA%E6%AF%94

b部分我计算了两次5*5窗口大小的高斯模糊后的图像与一次11*11窗口大小的高斯模糊图像之间的MSE与PSNR。

代码:

 #include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv_libs.h>
#include <math.h> /*
*《学习OpenCV》第五章第一题
* 完成时间:18:37 10/13 星期日 2013
* 作者:qdsclove@163.com
*/ /*
* function: calculate MSE & PSNR of two GrayScale(8-bit depth & one channel) images.
* param: img1 -- the first image.
* param: img2 -- the second image.
* param: dMSE -- the MSE of two images(output)
* param: dPSNR -- the PSNR of two images(output)
* return: 0 -- success; others -- failed.
*/
int calculateGrayImgsPSNR(IplImage* img1, IplImage* img2, double& dMSE, double& dPSNR)
{
if( !img1 || !img2 ||
img1->nChannels != ||
img2->nChannels != ||
img1->depth != img2->depth ||
img1->width != img2->width ||
img1->height != img2->height )
{
return -;
}
int width = img1->width;
int height = img1->height; // calculate MSE of the two images
double dSumOfSquares = ;
for(int i = ; i < height; i++)
{
char* pdata1 = img1->imageData + i * img1->widthStep;
char* pdata2 = img2->imageData + i *img2->widthStep;
for(int j = ; j < width; j++ )
{
uchar value1 = *(pdata1 + j);
uchar value2 = *(pdata2 + j); double square = pow( (double)(value1 - value2), );
dSumOfSquares += square;
}
} dMSE = dSumOfSquares / (width * height); // this is means the two images are strictly same.
if(dMSE == )
{
dPSNR = -;
return ;
}
int iDepth = img1->depth;
int iMAX = pow( ., iDepth) - ; dPSNR = * log10(iMAX / (sqrt(dMSE))); return ;
} int main()
{
const char * FILE_PATH = "Fig0333(a)(test_pattern_blurring_orig).tif"; IplImage* src = cvLoadImage(FILE_PATH, CV_LOAD_IMAGE_UNCHANGED); if(!src)
{
printf("Load image error.\n");
return -;
} // Get the source image's size
CvSize srcSize = cvGetSize(src); // 3 * 3
IplImage* dst_three_gaussian = cvCreateImage(srcSize, src->depth, src->nChannels);
// 5 * 5
IplImage* dst_five_gaussian = cvCreateImage(srcSize, src->depth, src->nChannels);
// 9 * 9
IplImage* dst_nine_gaussian = cvCreateImage(srcSize, src->depth, src->nChannels);
// 11 * 11
IplImage* dst_eleven_gaussian = cvCreateImage(srcSize, src->depth, src->nChannels);
// twice 5 * 5
IplImage* dst_twice_five_gaussian = cvCreateImage( srcSize, src->depth, src->nChannels ); if( !dst_three_gaussian || !dst_five_gaussian ||
!dst_nine_gaussian || !dst_eleven_gaussian ||
!dst_twice_five_gaussian )
{
printf("Create image error.\n");
return -;
} cvSmooth(src, dst_three_gaussian, CV_GAUSSIAN, , );
cvSmooth(src, dst_five_gaussian, CV_GAUSSIAN, , );
cvSmooth(src, dst_nine_gaussian, CV_GAUSSIAN, , );
cvSmooth(src, dst_eleven_gaussian, CV_GAUSSIAN, , );
cvSmooth( dst_five_gaussian, dst_twice_five_gaussian, CV_GAUSSIAN, , ); cvShowImage("src", src);
cvShowImage("src - GAUSSIAN 3*3", dst_three_gaussian);
cvShowImage("src - GAUSSIAN 5*5", dst_five_gaussian);
cvShowImage("src - GAUSSIAN 9*9", dst_nine_gaussian);
cvShowImage("src - GAUSSIAN 11*11", dst_eleven_gaussian);
cvShowImage("src - GAUSSIAN 5*5 Twice", dst_twice_five_gaussian ); // calculate the MSE and PSNR of the two images.
double dMSE, dPSNR;
// part a:
calculateGrayImgsPSNR(src, dst_three_gaussian, dMSE, dPSNR);
printf("source image & 3*3 GAUSSIAN: MSE: %f\tPSNR: %f\n", dMSE, dPSNR);
calculateGrayImgsPSNR(src, dst_five_gaussian, dMSE, dPSNR);
printf("source image & 5*5 GAUSSIAN: MSE: %f\tPSNR: %f\n", dMSE, dPSNR);
calculateGrayImgsPSNR(src, dst_nine_gaussian, dMSE, dPSNR);
printf("source image & 9*9 GAUSSIAN: MSE: %f\tPSNR: %f\n", dMSE, dPSNR);
calculateGrayImgsPSNR(src, dst_eleven_gaussian, dMSE, dPSNR);
printf("source image & 11*11 GAUSSIAN: MSE: %f\tPSNR: %f\n", dMSE, dPSNR); // part b
puts("---------------------------\n");
calculateGrayImgsPSNR(src, dst_eleven_gaussian, dMSE, dPSNR);
printf("source image & eleven: MSE: %f\tPSNR: %f\n", dMSE, dPSNR);
calculateGrayImgsPSNR(src, dst_twice_five_gaussian, dMSE, dPSNR);
printf("source image & twice five: MSE: %f\tPSNR: %f\n", dMSE, dPSNR);
calculateGrayImgsPSNR(dst_eleven_gaussian, dst_twice_five_gaussian, dMSE, dPSNR);
printf("eleven & twice five: MSE: %f\tPSNR: %f\n", dMSE, dPSNR); cvWaitKey();
cvReleaseImage(&src);
cvReleaseImage(&dst_three_gaussian);
cvReleaseImage(&dst_five_gaussian);
cvReleaseImage(&dst_nine_gaussian);
cvReleaseImage(&dst_eleven_gaussian);
cvReleaseImage(&dst_twice_five_gaussian);
cvDestroyAllWindows(); return ;
}

运行结果:

a部分:

3*3:

5*5:

9*9:

11*11:

同时各个不同的窗口大小模糊化后的图像与原始图像之间的MSE与PSNR:

从图中可以看出,当窗口大小越大时,MSE增大,PSNR减小。

b部分:

两幅图像的PSNR与MSE:

众所周知的是在图像压缩中典型的PSNR比值在30-40dB之间,而我们这两幅平滑之后的图像PSNR为31.976534,所以这两幅图像是比较接近的。

《学习OpenCV》练习题第五章第一题ab的更多相关文章

  1. 《学习OpenCV》练习题第五章第二题abc

    代码: #include <stdio.h> #include <opencv/highgui.h> #include <opencv/cv.h> #include ...

  2. 《学习OpenCV》练习题第四章第一题b&c

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  3. 《学习OpenCV》练习题第四章第一题a

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  4. 《学习Opencv》第五章 习题6

    这是第五章 习题5.6的结合版,其中实现了摄像头抓拍功能,能够成功运行. #include "stdafx.h" #include "cv.h" #includ ...

  5. 《学习OpenCV》练习题第四章第二题

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  6. 学习opencv中文版教程——第二章

    学习opencv中文版教程——第二章 所有案例,跑起来~~~然而并没有都跑起来...我只把我能跑的都尽量跑了,毕竟看书还是很生硬,能运行能出结果,才比较好. 越着急,心越慌,越是着急,越要慢,越是陌生 ...

  7. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第五章:渲染流水线

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第五章:渲染流水线 学习目标 了解几个用以表达真实场景的标志和2D图像 ...

  8. JavaScript DOM编程艺术-学习笔记(第五章、第六章)

    第五章: 1.题外话:首先大声疾呼,"js无罪",有罪的是滥用js的那些人.js的father 布兰登-艾克,当初为了应付工作,10天就赶出了这个js,事后还说人家js是c语言和s ...

  9. C++ Primer Plus学习:第十五章

    第十五章 友元.异常和其他 友元 友元类 表 0-1 class Tv { public: friend class Remote; } Remote类可以使用Tv的数据成员,Remote类在Tv类后 ...

随机推荐

  1. sysfs实例

    转自:http://blog.chinaunix.net/u1/51562/showart_1076295.html 一:前言 在设备模型中,sysfs文件系统用来表示设备的结构.将设备的层次结构形象 ...

  2. C# 时间戳和时间的相互转换

    时间戳定义为从格林威治时间 1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. C#格式时间转时间戳Timestamp private in ...

  3. debian系统安装Thinkpad T410s的无线网卡驱动:centrino Advanced-N 6200 2x2 AGN

    前几天搞到手一台小黑:T410s.自带系统是win7.由于想学习debian,所以就搞成了双系统,安装了一套debian 6.0. 可是让我困惑的是在debian下,无法使用T410s的网卡,因为默认 ...

  4. mysql安装图解 mysql图文安装教程(详细说明)

    MySQL5.0版本的安装图解教程是给新手学习的,当前mysql5.0.96是最新的稳定版本. mysql 下载地址 http://www.jb51.net/softs/2193.html 下面的是M ...

  5. tc srm 632 500 (规律)

    We have a sequence of N positive integers: a[0] through a[N-1]. You do not know these integers. All ...

  6. Chrome 快捷键使用

    窗口和标签页快捷方式 Ctrl+N 打开新窗口 按住 Ctrl‎ 键,然后点击链接 在新标签页中打开链接 按住 Shift 键,然后点击链接 在新窗口中打开链接 Alt+F4 关闭当前窗口 Ctrl+ ...

  7. HDU 4513 吉哥系列故事——完美队形II

    变形的Manacher算法,在扩展的时候要加入限制条件,满足题目中说的从左到中间身高不减. 其他地方倒是没有什么改动.. //#define LOCAL #include <iostream&g ...

  8. 漫游Kafka设计篇之主从同步

    Kafka允许topic的分区拥有若干副本,这个数量是可以配置的,你可以为每个topci配置副本的数量.Kafka会自动在每个个副本上备份数据,所以当一个节点down掉时数据依然是可用的. Kafka ...

  9. svn备份脚 本

    一直用这套脚本备份,脚本主体虽不是原创,但是从网上得到后因为不能运行也进行了些修改,前两天看到有人问关于SVN备份的问题,今天又把脚本整理了一下,解决了不能循环备份多个配置库的问题.希望对大家有所帮助 ...

  10. win32窗口机制之CreateWindow

    CreateWindow     函数功能:该函数创建一个重叠式窗口.弹出式窗口或子窗口.它指定窗口类,窗口标题,窗口   风格,以及窗口的初始位置及大小(可选的).该函数也指定该窗口的父窗口或所属窗 ...