《学习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类后 ...
随机推荐
- base64加密和解密
http://snailwarrior.blog.51cto.com/680306/142472/ 2.从标准输入读取文件内容,base64编码并打印到标准输出 [root@localhost tes ...
- 4630 no pain no game 树状数组
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你N个数,然后给你M个询问,每个询问包含一个l 一个r,问你lr 这个区间中任意两个数最 ...
- Codeforces 447 C DZY Loves Sequences【DP】
题意:给出一列数,在这个序列里面找到一个连续的严格上升的子串,现在可以任意修改序列里面的一个数,问得到的子串最长是多少 看的题解,自己没有想出来 假设修改的是a[i],那么有三种情况, 1.a[i]& ...
- UVa 1587 Box
题意:给出6个矩形的长和宽,问是否能够构成一个长方体 先假设一个例子 2 3 3 4 2 3 3 4 4 2 4 2 排序后 2 3 2 3 3 4 3 4 4 2 4 2 如果要构成一个长方体的话, ...
- 在VMware 虚拟机中配置 windows2003系统的NLB负载均衡;0x800706D5错误的解决方法;没有接口可用于安装新的群集
首先在VM里面 我装了3个2003的系统, 分别为 webservice01 ,webservice 02 , 以及 webview 这3台. 前面两台用于配置负载均衡,后面的webview就是 ...
- Python [Leetcode 121]Best Time to Buy and Sell Stock
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. If you ...
- [转载] ffmpeg超详细综合教程——摄像头直播
本文的示例将实现:读取PC摄像头视频数据并以RTMP协议发送为直播流.示例包含了 1.ffmpeg的libavdevice的使用 2.视频解码.编码.推流的基本流程 具有较强的综合性. 要使用liba ...
- [原创] Ubuntu Linux 安装Eclipse
一 安装JDK 1.下载 JDK 7从http://www.oracle.com/technetwork/java/javasebusiness/downloads/选择下载JDK的最新版本 JDK ...
- Heritrix源码分析(十一) Heritrix中的URL--CandidateURI和CrawlURI以及如何增加自己的属性(转)
本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/649889 本博客已迁移到本人独立博客: http://www.yun5u.com/ ...
- liux环境下配置jdk
大家都知道,现在JAVA的发展可谓是如日中天,它覆盖面非常广泛,小到个人PC,大到商业应用都能见到它的身影.以前它是由SUN公司来维护的,现在已经归属到甲骨文旗下了. 今天我们来学习一下Java JD ...