使用工具:OpenCV 3.0 + VS2013
test所用的图像如下,1.jpg最清晰,4.jpg最模糊。
c++语言
#include <opencv2/opencv.hpp>
#include <iostream>
#include "opencv2/core/core.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <cv.h>
using namespace std;
using namespace cv;
int main()
{
string pattern = "D:/皮卡丘最可爱/*.jpg"; //test的图片放在D盘
vector<Mat> images;
vector<String> pic; // 必须cv的String
glob(pattern, pic, false);
size_t count = pic.size();
cout << count << endl; //显示一共有多少张图片
for (int i = 0; i < count; i++)
{
images.push_back(imread(pic[i]));
Mat imageSource = images[i];
Mat imageGrey;
cvtColor(imageSource, imageGrey, COLOR_RGB2GRAY);
Mat imageSobel;
Laplacian(imageGrey, imageSobel, CV_16U); //Laplacian梯度法,数值越大表示图像越清晰
double meanValue = 0.0;
meanValue = mean(imageSobel)[0]; //图片清晰度
//double to string
stringstream meanValueStream;
string meanValueString; //字符串
meanValueStream << meanValue;
meanValueStream >> meanValueString;
meanValueString = "Articulation(Laplacian Method): " + meanValueString;
putText(imageSource, meanValueString, Point(20, 20), FONT_HERSHEY_COMPLEX, 0.7, Scalar(255, 0, 0), 2);
imshow("Articulation", imageSource);
waitKey(1000);
cout << meanValue << endl; //显示图片的清晰度
//将清晰度数据存为xml文件
FileStorage mul_wr("D:\\pikaqiu.xml", FileStorage::APPEND);
mul_wr << "meanValue" << meanValue;
mul_wr.release();
}
return 1;
}
说明:FileStorage写入的操作模式有包括:FileStorage::WRITE 和FileStorage::APPEND。如果使用WRITE模式,则xml只会保存存最后一个数据。
运行结果: