opencv清晰度,色偏等评价函数

参考文档:https://blog.csdn.net/qq_26499769/article/details/52057013

配置: VS2008 MFC  & opencv 2.4.4


步骤:1、新建 win32 控制台应用,空项目。
2、添加 main.cpp 文件。
3、添加 *.lib 库<软件配置里有说明>
 
说明:各类参数根据实际摄像头获取。我的是随意设置的。试验状态。


付源码:
double DefRto(Mat frame) 

    Mat gray; 
    cvtColor(frame,gray,CV_BGR2GRAY); 
    IplImage *img = &(IplImage(gray)); 
    double temp = 0; 
    double DR = 0; 
    int i,j;//循环变量 
    int height=img->height; 
    int width=img->width; 
    int step=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; 
    return DR; 
}
 




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

    Mat LABimg; 
    cvtColor(InputImg,LABimg,CV_BGR2Lab);
     //由于OpenCV定义的格式是uint8,这里输出的LABimg从标准的0~100,-127~127,
                -127~127,被映射到了0~255,0~255,0~255空间 
    float a=0,b=0; 
    int HistA[256],HistB[256]; 
    for(int i=0;i<256;i++) 
    { 
        HistA[i]=0; 
        HistB[i]=0; 
    } 
    for(int i=0;i
    { 
        for(int j=0;j
        { 
            a+=float(LABimg.at(i,j)[1]-128);//在计算过程中,要考虑将CIE L*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); 
    float Ma=0,Mb=0; 
    for(int i=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)); 
    float M=sqrt(Ma*Ma+Mb*Mb); 
    float K=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) 

    Mat GRAYimg; 
    cvtColor(InputImg,GRAYimg,CV_BGR2GRAY); 
    float a=0; 
    int Hist[256]; 
    for(int i=0;i<256;i++) 
    Hist[i]=0; 
    for(int i=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); 
    float Ma=0; 
    for(int i=0;i<256;i++) 
    { 
        Ma+=abs(i-128-da)*Hist[i]; 
    } 
    Ma/=float((GRAYimg.rows*GRAYimg.cols)); 
    float M=abs(Ma); 
    float K=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;

}

猜你喜欢

转载自blog.csdn.net/weixin_41770169/article/details/82351040
今日推荐