视频帧相对亮度的数值表达

来源:互联网 发布:人工智能创业大赛 编辑:程序博客网 时间:2024/05/20 09:07

opencv实现视频帧相对亮度的数值表达

//  main.cpp//  testOpencv////  Created by 李江涛 on 2017/11/17.//  Copyright © 2017年 lee. All rights reserved.#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 processiamge(Mat &frame)//{//    circle(frame, Point(cvRound(frame.cols / 2), cvRound(frame.rows / 2)), 150, Scalar(0, 0, 255), 2, 8);//}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);//    cvNamedWindow("video", CV_WINDOW_AUTOSIZE);//    cvNamedWindow("video", CV_WINDOW_AUTOSIZE);    IplImage * frame=cvQueryFrame(capture);;    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);        }        if(cvWaitKey(33)==27) break;    }    cvReleaseCapture(&capture);//    cvDestroyWindow("video");    return 0;}

运行结果

原创粉丝点击