opencv 学习之 图像色偏、清晰度、亮度检测

转自: 秦城书生  http://blog.sina.com.cn/s/blog_4f084f550102uwl8.html

配置: VS2008 MFC  & opencv 2.4.4

步骤:1、新建 win32 控制台应用,空项目。

2、添加 main.cpp 文件。

3、添加 *.lib 库<软件配置里有说明>

说明:各类参数根据实际摄像头获取。我的是随意设置的。试验状态。

付源码:

#include  
#include  
#include

#include "highgui.h"
#include "cv.h"

using namespace std; 
using namespace cv;


double DefRto(Mat frame) 

    Matgray; 
   cvtColor(frame,gray,CV_BGR2GRAY); 
    IplImage*img = &(IplImage(gray)); 
    double temp= 0; 
    double DR =0; 
    inti,j;//循环变量 
    intheight=img->height; 
    intwidth=img->width; 
    intstep=img->widthStep/sizeof(uchar); 
    uchar*data=(uchar*)img->imageData; 
    double num =width*height; 
 
   for(i=0;i
   { 
       for(j=0;j
       { 
           temp +=sqrt((pow((double)(data[(i+1)*step+j]-data[i*step+j]),2)

                             +  pow((double)(data[i*step+j+1]-data[i*step+j]),2))); 
           temp +=abs(data[(i+1)*step+j]-data[i*step+j])+abs(data[i*step+j+1]-data[i*step+j]); 
       } 
   } 
    DR =temp/num; 
    returnDR; 
}


 
void colorException(Mat InputImg,float& cast,float&da,float& db) 

    MatLABimg; 
   cvtColor(InputImg,LABimg,CV_BGR2Lab);

    //由于OpenCV定义的格式是uint8,这里输出的LABimg从标准的0~100,-127~127,

               -127~127,被映射到了0~255,0~255,0~255空间 
    floata=0,b=0; 
    intHistA[256],HistB[256]; 
    for(inti=0;i<256;i++) 
   { 
       HistA[i]=0; 
       HistB[i]=0; 
   } 
    for(inti=0;i
   { 
       for(int j=0;j
       { 
           a+=float(LABimg.at(i,j)[1]-128);//在计算过程中,要考虑将CIEL*a*b*空间还原后同 
           b+=float(LABimg.at(i,j)[2]-128); 
           int x=LABimg.at(i,j)[1]; 
           int y=LABimg.at(i,j)[2]; 
           HistA[x]++; 
           HistB[y]++; 
       } 
   } 
   da=a/float(LABimg.rows*LABimg.cols); 
   db=b/float(LABimg.rows*LABimg.cols); 
    float D=sqrt(da*da+db*db); 
    floatMa=0,Mb=0; 
    for(inti=0;i<256;i++) 
   { 
       Ma+=abs(i-128-da)*HistA[i];//计算范围-128~127 
       Mb+=abs(i-128-db)*HistB[i]; 
   } 
   Ma/=float((LABimg.rows*LABimg.cols)); 
   Mb/=float((LABimg.rows*LABimg.cols)); 
    floatM=sqrt(Ma*Ma+Mb*Mb); 
    floatK=D/M; 
    cast =K;
 printf("色偏指数: %f\n",cast);
 if(cast>1)
  printf("存在色偏\n");
 else
  printf("不存在色偏\n");
   return; 
}


void brightnessException (Mat InputImg,float& cast,float&da) 

    MatGRAYimg; 
   cvtColor(InputImg,GRAYimg,CV_BGR2GRAY); 
    floata=0; 
    intHist[256]; 
    for(inti=0;i<256;i++) 
   Hist[i]=0; 
    for(inti=0;i
   { 
       for(int j=0;j
       { 
           a+=float(GRAYimg.at(i,j)-128);//在计算过程中,考虑128为亮度均值点 
           int x=GRAYimg.at(i,j); 
           Hist[x]++; 
       } 
   } 
   da=a/float(GRAYimg.rows*InputImg.cols); 
    float D=abs(da); 
    floatMa=0; 
    for(inti=0;i<256;i++) 
   { 
       Ma+=abs(i-128-da)*Hist[i]; 
   } 
   Ma/=float((GRAYimg.rows*GRAYimg.cols)); 
    floatM=abs(Ma); 
    floatK=D/M; 
    cast =K;

printf("亮度指数: %f\n",cast);
 if(cast>1)
 {
  printf("亮度:");
  if(da>0)
   printf("过亮\n");
  else
   printf("过暗\n");
 }
 else
  printf("亮度:正常\n");
   return; 
}


int main() 

    Mat image =imread("d:\\picture\\lena.jpg"); 
   
 imshow("源图像",image); 
 
 printf("\n图片质量检测\n\n");
 
 float x = 1.0, y = 0.0, z = 0.0;
 
 double t = DefRto(image);       //清晰度
 printf("清晰度: %f\n",t);
 if(t>10)
  printf("清晰:正常\n");
 else
  printf("模糊\n\n");

printf("\n");
 colorException(image, x, y,z);     //色偏

printf("\n");
 brightnessException (image, x,y);    //明亮度

cvWaitKey(0);

return 0;
}

opencv <wbr>学习之 <wbr>图像色偏、清晰度、亮度检测



opencv <wbr>学习之 <wbr>图像色偏、清晰度、亮度检测

猜你喜欢

转载自blog.csdn.net/longbinliu/article/details/79742112