opencv 学习之 亮度检测

照例,先看代码

[cpp]  view plain  copy
 print ?
  1. /****************************************************************************** 
  2. 说明: 
  3.         检测亮度,常用方法是计算图片在灰度图上的均值和方差,当存在亮度 
  4.         异常时,均值会偏离均值点(可以假设为128),方差也会偏小;通过计 
  5.         算灰度图的均值和方差,就可评估图像是否存在过曝光或曝光不足。 
  6. ******************************************************************************/  
  7.   
  8. #include "cv.h"  
  9. #include "highgui.h"  
  10. #include "iostream"  
  11. #include "math.h"  
  12.   
  13. using namespace cv;  
  14.   
  15. /************************************************************************************ 
  16. *函数描述:  light      计算图像的亮度   
  17. *函数参数:  image   需要计算的图片 
  18. *                     cast      计算出的偏差值,小于1表示比较正常,大于1表示存在亮度异常; 
  19.                       当cast异常时,da大于0表示过亮,da小于0表示过暗  
  20. **************************************************************************************/  

[cpp]  view plain  copy
 print ?
  1. void light(IplImage * image)  
  2. {  
  3.     IplImage * gray = cvCreateImage(cvGetSize(image), image->depth, 1);  
  4.     cvCvtColor(image, gray, CV_BGR2GRAY);    
  5.     double sum = 0;    
  6.     double avg = 0;  
  7.     CvScalar scalar;  
  8.     int ls[256];  
  9.     for(int i=0; i<256; i++)  
  10.         ls[i]=0;  
  11.     for(int i=0;i<gray->height;i++)    
  12.     {    
  13.         for(int j=0;j<gray->width;j++)    
  14.         {    
  15.             scalar = cvGet2D(gray, i, j);  
  16.             sum += (scalar.val[0] - 128);  
  17.             int x= (int)scalar.val[0];    
  18.              ls[x]++;    
  19.         }    
  20.     }    
  21.     avg = sum/(gray->height * gray->width);    
  22.     double total = 0;  
  23.     double mean = 0;  
  24.     for(int i=0;i<256;i++)    
  25.     {    
  26.         total += abs(i-128-avg)* ls[i];    
  27.     }    
  28.     mean = total/(gray->height * gray->width);  
  29.     double cast = abs(avg/mean);    
  30.     printf("light: %f\n", cast);  
  31.     if(cast>1)  
  32.     {  
  33.         if(avg>0)  
  34.             printf("light\n");  
  35.         else printf("dark\n");  
  36.     }  
  37. }  

[cpp]  view plain  copy
 print ?
  1. int main()  
  2. {  
  3.     CvCapture * capture = cvCreateFileCapture("d:\\picture\\video.avi");  
  4.     cvNamedWindow("video", CV_WINDOW_AUTOSIZE);  
  5.     IplImage * frame;  
  6.     int count = 0;  
  7.     while(true)  
  8.     {  
  9.         count++;  
  10.         frame = cvQueryFrame(capture);  
  11.         if(!frame) break;  
  12.         cvShowImage("video", frame);  
  13.         if(!(count%10))  
  14.         {  
  15.             printf("frame : %d\n", count);  
  16.             light1(frame);  
  17.             light2(frame);  
  18.             light3(frame);  
  19.         }  
  20.         if(cvWaitKey(33)==27) break;  
  21.     }  
  22.     cvReleaseCapture(&capture);  
  23.     cvDestroyWindow("video");  
  24.     return 0;  
  25. }  

这个不难理解吧

转自:http://blog.csdn.net/u010477528/article/details/39374817

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值