图像清晰度检测程序
在抓拍人臉時(shí),為丟掉抓拍質(zhì)量不高的數(shù)據(jù)。寫(xiě)了一個(gè)程序來(lái)測(cè)試三種清晰度檢測(cè)的算法。
程序的主要功能是,依次讀取圖片文件夾中的圖片,進(jìn)行清晰度檢測(cè),每張圖片會(huì)得到一個(gè)清晰度打分結(jié)果。為了提高程序執(zhí)行速度,將清晰度的閾值寫(xiě)在cpg文件里,讓程序去讀cpg文件里的閾值,與每張圖片的打分結(jié)果比較,低于此閾值的將放入這個(gè)路徑的子文件夾(命名為threshold1)中。
并且將每張圖片的打分結(jié)果,寫(xiě)入txt文件中,可視化打分結(jié)果。
?
》首先需注意的是,為了保證程序在不同閾值下正確運(yùn)行,需要在每次運(yùn)行時(shí)先清空掉threshold1文件夾,并將其中的圖片重新放回遍歷到的文件夾中。
這里采用rename方式,將圖片路徑名改變,使得threshold1文件夾內(nèi)的圖片移出。
》程序主體部分
用FindFirstFileA和FindNextFileA遍歷圖片文件夾內(nèi)的文件。
如果圖片文件夾中還有子文件夾,則遞歸操作。
獲得圖片路徑后再加上圖片名稱(chēng),就可以放入計(jì)算清晰度的函數(shù)中。
這里清晰度檢測(cè)有三種方法,sobel laplacian,和meanvalue
得到清晰度值后,進(jìn)入對(duì)清晰度值進(jìn)行判斷操作部分。
如果小于閾值,就調(diào)用rename函數(shù),將圖片移到threshold1文件夾中。
參考:
OpenCV 圖像清晰度評(píng)價(jià)(相機(jī)自動(dòng)對(duì)焦) - CSDN博客
#include <highgui/highgui.hpp> #include <imgproc/imgproc.hpp> #include <iostream> #include <opencv2/opencv.hpp> #include <string> #include <fstream> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <string> #include <shlwapi.h> #pragma comment(lib,"shlwapi.lib") #define INI_FILE _T("cpg.ini")using namespace cv; using namespace std;//int main(int argc, char *argv[])double calculate(string fileName) {string newfileName, grayFile;stringstream meanValueStream;string meanValueString;Mat imageSource = imread(fileName);Mat imageGrey;Mat meanValueImage, meanStdValueImage;cvtColor(imageSource, imageGrey, CV_RGB2GRAY);Mat imageSobel;Laplacian(imageGrey, imageSobel, CV_16U, 1, 1);//Sobel(imageGrey, imageSobel, CV_16U, 1, 1);//求灰度圖像的標(biāo)準(zhǔn)差/*meanStdDev(imageGrey, meanValueImage, meanStdValueImage);double meanValue = 0.0;meanValue = meanStdValueImage.at<double>(0, 0); *///圖像的平均灰度double meanValue = 0.0;meanValue = mean(imageSobel)[0];meanValueStream << meanValue;meanValueStream >> meanValueString;meanValueString = fileName + ": Articulation(Laplacian):" + meanValueString;ofstream outfile("路徑/ArticulationLaplacianresult.txt", ios::app);outfile << meanValueString;outfile << endl;outfile.close();return meanValue; } //double to string void opt(string fileName, string imgname, double meanValue) {char ThresholdValue[260];double fThresholdValue;string newfileName;WIN32_FIND_DATAA winFindData;GetPrivateProfileStringA("Threshold", "ThresholdValue", "", ThresholdValue, 260, "D:/AItensor/blurdetector/cpg.ini");fThresholdValue = atof(ThresholdValue);string str1 = winFindData.cFileName;newfileName = "路徑/" + imgname;if (meanValue <= fThresholdValue){rename(fileName.c_str(), newfileName.c_str());}}void judge(const string &strPath) {string fileName, filePath;double meanValue;string strRawPath = strPath;//傳地址strRawPath.append("\\");//加分隔符string strFindPath = strRawPath;//在新設(shè)一個(gè)strFindPath.append("*.*");//在最外面的目錄下查找文件WIN32_FIND_DATAA winFindData;HANDLE hTemp = FindFirstFileA(strFindPath.c_str(), &winFindData);//c.str是因?yàn)檫@個(gè)函數(shù)需要cha類(lèi)型,返回找到的數(shù)據(jù)//if (INVALID_HANDLE_VALUE == hTemp)// return 0;while (FindNextFileA(hTemp, &winFindData)){string strOldName = winFindData.cFileName;if ("." == strOldName || ".." == strOldName)continue;//如果是目錄,則遞歸繼續(xù)操作if (winFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){string strAgain = strPath;strAgain.append("\\");strAgain.append(winFindData.cFileName);judge(strAgain);continue;}//獲得絕對(duì)路徑strOldName = strRawPath;//strOldName.append(winFindData.cFileName);filePath = strRawPath;string fileName = strOldName + winFindData.cFileName;//string strNewName = strOldName;string imgname = winFindData.cFileName;meanValue = calculate(fileName);opt(fileName, imgname, meanValue);//return fileName, filePath, imgname;}FindClose(hTemp); }void rename(const string &strPath, const string &strPathroot) {string fileName, filePath;double meanValue;string strRawPath = strPath;//傳地址strRawPath.append("\\");//加分隔符string strFindPath = strRawPath;//在新設(shè)一個(gè)strFindPath.append("*.*");//在最外面的目錄下查找文件WIN32_FIND_DATAA winFindData;HANDLE hTemp = FindFirstFileA(strFindPath.c_str(), &winFindData);//c.str是因?yàn)檫@個(gè)函數(shù)需要cha類(lèi)型,返回找到的數(shù)據(jù)//if (INVALID_HANDLE_VALUE == hTemp)// return 0;while (FindNextFileA(hTemp, &winFindData)){string imgname = winFindData.cFileName;//newfilePath = strRawPath;string fileName = strRawPath + winFindData.cFileName;//string strNewName = strOldName;string newfileName = strPathroot + winFindData.cFileName;rename(fileName.c_str(), newfileName.c_str());//return fileName, filePath, imgname;}FindClose(hTemp); }int main(int argc, char *argv[]) {string thresholdName = "路徑/threshold1";string newthresholdName = "路徑/";rename(thresholdName, newthresholdName);string path = "路徑";judge(path); }?
總結(jié)
- 上一篇: Ubuntu 下yuma源码安装
- 下一篇: 逢二进一 、逢八进一、逢十六进一