《学习OpenCV》练习题第五章第一题ab
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用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的更多相关文章
- 《学习OpenCV》练习题第五章第二题abc
代码: #include <stdio.h> #include <opencv/highgui.h> #include <opencv/cv.h> #include ...
- 《学习OpenCV》练习题第四章第一题b&c
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- 《学习OpenCV》练习题第四章第一题a
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- 《学习Opencv》第五章 习题6
这是第五章 习题5.6的结合版,其中实现了摄像头抓拍功能,能够成功运行. #include "stdafx.h" #include "cv.h" #includ ...
- 《学习OpenCV》练习题第四章第二题
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- 学习opencv中文版教程——第二章
学习opencv中文版教程——第二章 所有案例,跑起来~~~然而并没有都跑起来...我只把我能跑的都尽量跑了,毕竟看书还是很生硬,能运行能出结果,才比较好. 越着急,心越慌,越是着急,越要慢,越是陌生 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第五章:渲染流水线
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第五章:渲染流水线 学习目标 了解几个用以表达真实场景的标志和2D图像 ...
- JavaScript DOM编程艺术-学习笔记(第五章、第六章)
第五章: 1.题外话:首先大声疾呼,"js无罪",有罪的是滥用js的那些人.js的father 布兰登-艾克,当初为了应付工作,10天就赶出了这个js,事后还说人家js是c语言和s ...
- C++ Primer Plus学习:第十五章
第十五章 友元.异常和其他 友元 友元类 表 0-1 class Tv { public: friend class Remote; } Remote类可以使用Tv的数据成员,Remote类在Tv类后 ...
随机推荐
- Android Socket 聊天室示例
服务端: package com.test.chatServer; import java.io.IOException; import java.net.ServerSocket; import j ...
- Oracle Order Management DropShip Flow for R12
Oracle Order Management DropShip Flow for R12 Email ThisBlogThis!Share to TwitterShare to FacebookSh ...
- JS Scoping and Hoisting
参考了这篇文章 http://www.jb51.net/article/30719.htm var v='Hello World'; (function(){ console.log(v); })() ...
- 51nod1394 差和问题
我只会用线段树写...不喜欢树状数组..其实跑的也不算慢?然后各种*的时候忘了longlong一直WA...药丸! 而且我不怎么会用map离散化...那么就sort+unique #include&l ...
- 浅谈 Scala 中下划线的用途
Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...
- PopupWindow+ListView
1. 获取打到数据 for (int i = 0; i < iocOutMakeMaterialSubmit.data.size(); i++) { dataListPopupWindow.ad ...
- ios协议调起app
function openIos(url, callback) { if (!url) { return; } var node = document.createElement('iframe'); ...
- equal 和 ==
刚才看了一下别人的博客,想加深一下对 equal 和 == 的了解. 总结了几点: 1.equal 每个类都有必要覆盖一下,对于String 类,已经覆盖,比较的是String对象的字符序列是否相等. ...
- Android下Fragment的动画切换效果
效果图如下: 源码链接 : 请戳这里
- InnoDB 引擎独立表空间 innodb_file_per_table
使用过MySQL的同学,刚开始接触最多的莫过于MyISAM表引擎了,这种引擎的数据库会分别创建三个文件:表结构.表索引.表数据空间.我们可以将某个数据库目录直接迁移到其他数据库也可以正常工作.然而当你 ...