《学习OpenCV》练习题第五章第二题abc
代码:
#include <stdio.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv_libs.h> /*
*《学习OpenCV》第五章第二题
* 完成时间:21:43 10/13 星期日 2013
* 作者:qdsclove@163.com
*/ /* Image Size */
#define IMG_SIZE 100 /*
* Window Title
*/
#define WNDTITLE_IMAGE "source image"
#define WNDTITLE_FIVE "5*5 Gaussian"
#define WNDTITLE_NINE "9*9 Gaussian"
#define WNDTITLE_FIVE_TEICE "5*5 Gaussian Twice" /*
* 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()
{
IplImage* image = cvCreateImage( cvSize(IMG_SIZE, IMG_SIZE), IPL_DEPTH_8U, );
IplImage* dst_five_gaussian = cvCreateImage( cvGetSize(image), image->depth, image->nChannels );
IplImage* dst_nine_gaussian = cvCreateImage( cvGetSize(image), image->depth, image->nChannels );
IplImage* dst_twice_five_gaussian = cvCreateImage( cvGetSize(image), image->depth, image->nChannels ); // 全部像素置零
cvZero(image);
// 设置中心像素为255
cvSet2D(image, IMG_SIZE/, IMG_SIZE/, cvScalarAll()); // 5*5 高斯滤波
cvSmooth(image, dst_five_gaussian, CV_GAUSSIAN, , );
// 9*9 高斯滤波
cvSmooth(image, dst_nine_gaussian, CV_GAUSSIAN, , );
// 5*5高斯滤波 第二次
cvSmooth(dst_five_gaussian, dst_twice_five_gaussian, , ); cvNamedWindow(WNDTITLE_IMAGE, CV_WINDOW_NORMAL);
cvNamedWindow(WNDTITLE_FIVE, CV_WINDOW_NORMAL);
cvNamedWindow(WNDTITLE_NINE, CV_WINDOW_NORMAL);
cvNamedWindow(WNDTITLE_FIVE_TEICE, CV_WINDOW_NORMAL); cvShowImage(WNDTITLE_IMAGE, image);
cvShowImage(WNDTITLE_FIVE, dst_five_gaussian);
cvShowImage(WNDTITLE_NINE, dst_nine_gaussian);
cvShowImage(WNDTITLE_FIVE_TEICE, dst_twice_five_gaussian); cvSaveImage("source.bmp", image);
cvSaveImage("5_5_gaussian.bmp", dst_five_gaussian);
cvSaveImage("9_9_gaussian.bmp", dst_nine_gaussian);
cvSaveImage("5_5_gaussian_twice.bmp", dst_twice_five_gaussian); // c part
double dMSE = , dPSNR = ;
calculateGrayImgsPSNR(dst_nine_gaussian, dst_twice_five_gaussian, dMSE, dPSNR);
printf("9*9 GAUSSIAN & 5*5 GAUSSIAN Twice: MSE: %f\tPSNR: %f\n", dMSE, dPSNR); cvWaitKey(); cvReleaseImage(&image);
cvReleaseImage(&dst_five_gaussian);
cvReleaseImage(&dst_nine_gaussian);
cvDestroyAllWindows(); return ;
}
结果分析,这里的截图都是结果图像放大之后的结果:
原图 & 5*5高斯滤波后的图像:
原图 & 5*5高斯滤波后的图像 & 9*9高斯滤波后的图像:
c部分:
原图 & 5*5高斯滤波后的图像 & 9*9高斯滤波后的图像 & 两次5*5高斯滤波后的图像:
9*9平滑一次与5*5平滑两次的MSE与PSNR:
从上一篇博文(http://www.cnblogs.com/qdsclove/p/3366907.html)可知这两幅图像的相似度很高。
《学习OpenCV》练习题第五章第二题abc的更多相关文章
- 《学习OpenCV》练习题第五章第一题ab
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...
- 《学习OpenCV》练习题第四章第二题
#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》练习题第四章第一题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 ...
- 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 练习题 第五章
1. #include <stdio.h> #define MINU 60 int main() { int minute, hour, m; printf("请输入分钟:&qu ...
- C++ Primer Plus学习:第十五章
第十五章 友元.异常和其他 友元 友元类 表 0-1 class Tv { public: friend class Remote; } Remote类可以使用Tv的数据成员,Remote类在Tv类后 ...
随机推荐
- linux硬件驱动层
1.make menuconfig scripts/kconfig/lxdialog/menubox.o: In function `print_buttons':menubox.c:(.text+0 ...
- 12.allegro环境设置[原创]
一.菜单简介 --- 分割电源,分割平面 ------- ------- ------- ----- --------- ---- --------------- ----------------- ...
- [NYIST15]括号匹配(二)(区间dp)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...
- 【转载】两军问题与Paxos算法 & 动画讲解Paxos算法
http://harry.me/blog/2014/12/27/neat-algorithms-paxos/ 这篇文章里面有用JS写的Paxos过程,有助理解.但是没怎么仔细看,没时间. 这篇文章用两 ...
- R语言 rwordseg包的下载
在CRAN中没有,如果通过R下载经常会出错,使用以下地址下载后加载本地包 http://R-Forge.R-project.org/bin/windows/contrib/3.0/Rwordseg_0 ...
- 【笨嘴拙舌WINDOWS】剪切板
Windows剪贴板是一种比较简单同时也是开销比较小的IPC(InterProcess Communication,进程间通讯)机制.Windows系统支持剪贴板IPC的基本机制是由系统预留的一块全局 ...
- jsp之jsp基础
1. Jsp生命周期 客户端第一次请求->web容器把jsp文件转译为servlet源文件(java)->编译为class文件->载入class文件生成servlet对象 2. Js ...
- LA 3971 (二分) Assemble
题意: 你有b块钱想要组装一台电脑.给出n个配件的种类,品质和价格,要求每个种类的配件各买一个总价格不超过b且“品质最差配件”的品质因子应尽量大. 这种情况下STL的map的确很好用,学习学习 这种最 ...
- Python [Leetcode 344]Reverse String
题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s ...
- UVa10603 Fill
解题思路:这是神奇的一题,一定要好好体会.见代码: #include<cstdio> #include<cstring> #include<algorithm> # ...