opencv+清晰度检测



/******************************************************************************
说明:
衡量画面模糊程度的主要方法就是梯度的的统计特征,通常梯度值越高,
画面边缘信息越丰富,画面越清晰。然而需要注意,对于一些文理比较
少的画面,即使不失焦,梯度值也很小。
******************************************************************************/
#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;
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值