/******************************************************************************
说明:
衡量画面模糊程度的主要方法就是梯度的的统计特征,通常梯度值越高,
画面边缘信息越丰富,画面越清晰。然而需要注意,对于一些文理比较
少的画面,即使不失焦,梯度值也很小。
******************************************************************************/
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "iostream"
#include "math.h"
using namespace cv;
/********************************************************************************
相邻像素灰度方差法
*********************************************************************************/
void clear2(IplImage * image)
{
IplImage * gray = cvCreateImage(cvGetSize(image), image->depth, 1);
cvCvtColor(image, gray, CV_BGR2GRAY);
double sum = 0;
int height = gray->height;
int width = gray->width;
int step = gray->widthStep / sizeof(uchar);
uchar * data = (uchar *)gray->imageData;
for (int i = 0; i<gray->height - 1; i++)
{
for (int j = 0; j<gray->width - 1; j++)
{
sum += sqrt((pow((double)(data[(i + 1)*step + j] - data[i*step + j]), 2) + pow((double)(data[i*step + j + 1] - data[i*step + j]), 2)));
sum += abs(data[(i + 1)*step + j] - data[i*step + j]) + abs(data[i*step + j + 1] - data[i*step + j]);
}
}
double cast = sum / (gray->height * gray->width);
printf("cast2 : %f\n", cast);
}
//opebcv自带的公式读取像素值,效率比较低
void clear3(IplImage * image)
{
IplImage * gray = cvCreateImage(cvGetSize(image), image->depth, 1);
cvCvtColor(image, gray, CV_BGR2GRAY);
double sum = 0;
int height = gray->height;
int width = gray->width;
CvScalar s, x, y;
for (int i = 0; i<gray->height - 1; i++)
{
for (int j = 0; j<gray->width - 1; j++)
{
s = cvGet2D(gray, i, j);
double a = s.val[0];
x = cvGet2D(gray, i + 1, j);
double b = x.val[0];
y = cvGet2D(gray, i, j + 1);
double c = y.val[0];
sum += sqrt((double)pow((a - b), 2) + (double)pow((a - c), 2));
sum += abs(a - b) + abs(a - c);
}
}
double cast = sum / (gray->height * gray->width);
printf("cast3 : %f\n", cast);
}
int main()
{
CvCapture * capture = cvCreateFileCapture("d:\\video\\xuehua.avi");
IplImage * frame;
cvNamedWindow("video", CV_WINDOW_AUTOSIZE);
int count = 0;
while (true)
{
count++;
frame = cvQueryFrame(capture);
if (!frame) break;
cvShowImage("video", frame);
if (!(count % 50))
{
printf("f : %d\n", count);
clear2(frame);
//clear3(frame);
}
if (cvWaitKey(10) == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("video");
return 0;
}