opencv+视频亮度检测

// liangducheck.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"




#include "cv.h"  
#include "highgui.h"  
#include "iostream"  
#include "math.h"  


using namespace cv;


/************************************************************************************
*函数描述:  light      计算图像的亮度
*函数参数:  image   需要计算的图片
*                     cast      计算出的偏差值,小于1表示比较正常,大于1表示存在亮度异常;
当cast异常时,da大于0表示过亮,da小于0表示过暗
**************************************************************************************/


void light(IplImage * image)
{
IplImage * gray = cvCreateImage(cvGetSize(image), image->depth, 1);
cvCvtColor(image, gray, CV_BGR2GRAY);
double sum = 0;
double avg = 0;
CvScalar scalar;
int ls[256];
for (int i = 0; i<256; i++)
ls[i] = 0;
for (int i = 0; iheight; i++)
{
for (int j = 0; jwidth; j++)
{
scalar = cvGet2D(gray, i, j);
sum += (scalar.val[0] - 128);
int x = (int)scalar.val[0];
ls[x]++;
}
}
avg = sum / (gray->height * gray->width);
double total = 0;
double mean = 0;
for (int i = 0; i<256; i++)
{
total += abs(i - 128 - avg)* ls[i];
}
mean = total / (gray->height * gray->width);
double cast = abs(avg / mean);
printf("light: %f\n", cast);
if (cast>3)
{
if (avg>0)
printf("light\n");
else printf("dark\n");
}
else printf("normal\n");
}




int main()
{
CvCapture * capture = cvCreateFileCapture("d:\\video\\doudong.avi");
cvNamedWindow("video", CV_WINDOW_AUTOSIZE);
IplImage * frame;
int count = 0;
while (true)
{
count++;
frame = cvQueryFrame(capture);
if (!frame) break;
cvShowImage("video", frame);
if (!(count % 10))
{
printf("frame : %d\n", count);
light(frame);
/*light2(frame);
light3(frame);*/
}
if (cvWaitKey(33) == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("video");
return 0;
}

你可能感兴趣的:(OPENCV)