opencv实现视频帧相对亮度的数值表达
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
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>1)
{
if(avg>0)
printf("light\n");
else printf("dark\n");
}
}
int main(){
CvCapture * capture =cvCreateCameraCapture(0);
IplImage * frame=cvQueryFrame(capture);;
int count = 0;
while(true)
{
count++;
frame = cvQueryFrame(capture);
if(!frame) break;
if(!(count%10))
{
printf("frame : %d\n", count);
light(frame);
}
if(cvWaitKey(33)==27) break;
}
cvReleaseCapture(&capture);
return 0;
}