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; i<gray->height; i++)
{
for (int j = 0; j<gray->width; 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;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值