opencv清晰度,色偏等评价函数
生活随笔
收集整理的這篇文章主要介紹了
opencv清晰度,色偏等评价函数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
配置: VS2008 MFC ?& opencv 2.4.4
步驟:1、新建 win32 控制臺(tái)應(yīng)用,空項(xiàng)目。
2、添加 main.cpp 文件。
3、添加 *.lib 庫(kù)<軟件配置里有說(shuō)明>
?
說(shuō)明:各類參數(shù)根據(jù)實(shí)際攝像頭獲取。我的是隨意設(shè)置的。試驗(yàn)狀態(tài)。
付源碼:
double DefRto(Mat frame)?
{?
? ? Mat gray;?
? ? cvtColor(frame,gray,CV_BGR2GRAY);?
? ? IplImage *img = &(IplImage(gray));?
? ? double temp = 0;?
? ? double DR = 0;?
? ? int i,j;//循環(huán)變量?
? ? 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從標(biāo)準(zhǔn)的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);//在計(jì)算過(guò)程中,要考慮將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];//計(jì)算范圍-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("色偏指數(shù): %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);//在計(jì)算過(guò)程中,考慮128為亮度均值點(diǎn)?
? ? ? ? ? ? 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("亮度指數(shù): %f\n",cast);
?if(cast>1)
?{
? printf("亮度:");
? if(da>0)
? ?printf("過(guò)亮\n");
? else
? ?printf("過(guò)暗\n");
?}
?else
? printf("亮度:正常\n");
? ? return;?
}
int main()?
{?
? ? Mat image = imread("d:\\picture\\lena.jpg");?
? ?
?imshow("源圖像",image);?
?
?printf("\n圖片質(zhì)量檢測(cè)\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;
步驟:1、新建 win32 控制臺(tái)應(yīng)用,空項(xiàng)目。
2、添加 main.cpp 文件。
3、添加 *.lib 庫(kù)<軟件配置里有說(shuō)明>
?
說(shuō)明:各類參數(shù)根據(jù)實(shí)際攝像頭獲取。我的是隨意設(shè)置的。試驗(yàn)狀態(tài)。
付源碼:
double DefRto(Mat frame)?
{?
? ? Mat gray;?
? ? cvtColor(frame,gray,CV_BGR2GRAY);?
? ? IplImage *img = &(IplImage(gray));?
? ? double temp = 0;?
? ? double DR = 0;?
? ? int i,j;//循環(huán)變量?
? ? 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從標(biāo)準(zhǔn)的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);//在計(jì)算過(guò)程中,要考慮將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];//計(jì)算范圍-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("色偏指數(shù): %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);//在計(jì)算過(guò)程中,考慮128為亮度均值點(diǎn)?
? ? ? ? ? ? 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("亮度指數(shù): %f\n",cast);
?if(cast>1)
?{
? printf("亮度:");
? if(da>0)
? ?printf("過(guò)亮\n");
? else
? ?printf("過(guò)暗\n");
?}
?else
? printf("亮度:正常\n");
? ? return;?
}
int main()?
{?
? ? Mat image = imread("d:\\picture\\lena.jpg");?
? ?
?imshow("源圖像",image);?
?
?printf("\n圖片質(zhì)量檢測(cè)\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;
}
總結(jié)
以上是生活随笔為你收集整理的opencv清晰度,色偏等评价函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: DICOM医学图像处理:DICOM存储操
- 下一篇: 图像清晰度的评价及分析