相机的自动对焦要求相机根据拍摄环境和场景的变化,通过相机内部的微型驱动马达,自动调节相机镜头和CCD之间的距离,保证像平面正好投影到CCD的成像表面上。这时候物体的成像比较清晰,图像细节信息丰富。

相机自动对焦的过程,其实就是对成像清晰度评价的过程,对焦不准确,拍摄出来的图像清晰度低,视觉效果模糊,如果是在工业检测测量领域,对焦不准导致的后果可能是致命的;对焦准确的图像清晰度较高,层次鲜明,对比度高。

图像清晰度评价算法有很多种,在空域中,主要思路是考察图像的领域对比度,即相邻像素间的灰度特征的梯度差;在频域中,主要思路是考察图像的频率分量,对焦清晰的图像高频分量较多,对焦模糊的图像低频分量较多。

这里实现3种清晰度评价方法,分别是Tenengrad梯度方法、Laplacian梯度方法和方差方法。

Tenengrad梯度方法

Tenengrad梯度方法利用Sobel算子分别计算水平和垂直方向的梯度,同一场景下梯度值越高,图像越清晰。以下是具体实现,这里衡量的指标是经过Sobel算子处理后的图像的平均灰度值,值越大,代表图像越清晰。

 #include <highgui/highgui.hpp>
#include <imgproc/imgproc.hpp> using namespace std;
using namespace cv; int main()
{
Mat imageSource = imread("2.jpg");
Mat imageGrey; cvtColor(imageSource, imageGrey, CV_RGB2GRAY);
Mat imageSobel;
Sobel(imageGrey, imageSobel, CV_16U, , ); //图像的平均灰度
double meanValue = 0.0;
meanValue = mean(imageSobel)[]; //double to string
stringstream meanValueStream;
string meanValueString;
meanValueStream << meanValue;
meanValueStream >> meanValueString;
meanValueString = "Articulation(Sobel Method): " + meanValueString;
putText(imageSource, meanValueString, Point(, ), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(, , ), );
imshow("Articulation", imageSource);
waitKey();
}

使用三张测试图片模拟不同对焦。第一张最清晰,得分最高,第二三张越来越模糊,得分依次降低。

Laplacian梯度方法:

Laplacian梯度是另一种求图像梯度的方法,在上例的OpenCV代码中直接替换Sobel算子即可。

 #include <highgui/highgui.hpp>
#include <imgproc/imgproc.hpp> using namespace std;
using namespace cv; int main()
{
Mat imageSource = imread("1.jpg");
Mat imageGrey; cvtColor(imageSource, imageGrey, CV_RGB2GRAY);
Mat imageSobel; Laplacian(imageGrey, imageSobel, CV_16U);
//Sobel(imageGrey, imageSobel, CV_16U, 1, 1); //图像的平均灰度
double meanValue = 0.0;
meanValue = mean(imageSobel)[]; //double to string
stringstream meanValueStream;
string meanValueString;
meanValueStream << meanValue;
meanValueStream >> meanValueString;
meanValueString = "Articulation(Laplacian Method): " + meanValueString;
putText(imageSource, meanValueString, Point(, ), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(, , ), );
imshow("Articulation", imageSource);
waitKey();
}

用同样的三张测试图片测试,结果一致,随着对焦模糊得分降低:

方差方法:

方差是概率论中用来考察一组离散数据和其期望(即数据的均值)之间的离散(偏离)成都的度量方法。方差较大,表示这一组数据之间的偏差就较大,组内的数据有的较大,有的较小,分布不均衡;方差较小,表示这一组数据之间的偏差较小,组内的数据之间分布平均,大小相近。

对焦清晰的图像相比对焦模糊的图像,它的数据之间的灰度差异应该更大,即它的方差应该较大,可以通过图像灰度数据的方差来衡量图像的清晰度,方差越大,表示清晰度越好。

 #include <highgui/highgui.hpp>
#include <imgproc/imgproc.hpp> using namespace std;
using namespace cv; int main()
{
Mat imageSource = imread("2.jpg");
Mat imageGrey; cvtColor(imageSource, imageGrey, CV_RGB2GRAY);
Mat meanValueImage;
Mat meanStdValueImage; //求灰度图像的标准差
meanStdDev(imageGrey, meanValueImage, meanStdValueImage);
double meanValue = 0.0;
meanValue = meanStdValueImage.at<double>(, ); //double to string
stringstream meanValueStream;
string meanValueString;
meanValueStream << meanValue*meanValue;
meanValueStream >> meanValueString;
meanValueString = "Articulation(Variance Method): " + meanValueString; putText(imageSource, meanValueString, Point(, ), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(, , ), );
imshow("Articulation", imageSource);
waitKey();
}

方差数值随着清晰度的降低逐渐降低:

在工业应用中,最清晰的对焦拍摄出来的图像不一定是最好的,有可能出现摩尔纹(水波纹)现象,一般需要在最清晰对焦位置附件做一个微调。

最新文章

  1. BAT“搅局”B2B市场,CIO们准备好了吗?
  2. Oracle SQL性能优化
  3. SQL Server数据库设计规范
  4. [HTML]输入框被限制输入某些类型数据
  5. 关于ajax的同步和异步
  6. UITextView 文本垂直居中
  7. vector 去重复
  8. nyoj 103 A + B problem II
  9. Delphi For Android 开发笔记-附:如何Delphi中同时实现Windows、Android版的GetModuleFileName函数
  10. android sdk manager 无法更新
  11. [反汇编练习] 160个CrackMe之015
  12. Upload/download/UrlConnection/URL
  13. Python-字符串开头或结尾匹配
  14. 设置windows窗口半透明(使用SetLayeredWindowAttributes API函数)
  15. Java 文件句柄泄露问题解决小记
  16. JVM进程占用CPU过高问题排查
  17. 【C#复习总结】析构函数
  18. Prism框架研究(二)
  19. 在SVNX中实现$Ids的自动替换
  20. JS基础内容小结(基础)(一)

热门文章

  1. Codeforces 1290A/1291C - Mind Control
  2. POJ 1663:Number Steps
  3. CTF密码学常见加密解密总结
  4. 小白需要了解的Ajax和websocket的区别以及使用场景!
  5. D语言-变量、输入、输出、注释
  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 删除数据库
  7. SQL基础教程(第2版)第1章 数据库和SQL
  8. Python创建命令行应用的工具 tools for command line application in python
  9. 洛谷 P1032 字串变换(map)
  10. Anaconda 添加清华源与恢复默认源