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

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;

}

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值