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

时间:2022-05-24 02:00:43

代码:

 #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高斯滤波后的图像:

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

原图 & 5*5高斯滤波后的图像 & 9*9高斯滤波后的图像:

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

c部分:

原图 & 5*5高斯滤波后的图像 & 9*9高斯滤波后的图像 & 两次5*5高斯滤波后的图像:

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

9*9平滑一次与5*5平滑两次的MSE与PSNR:

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

从上一篇博文(http://www.cnblogs.com/qdsclove/p/3366907.html)可知这两幅图像的相似度很高。