Halcon 自动对焦算法

1、介绍
图像清晰度是衡量图像质量的一个重要指标,对于相机来说,其一般工作在无参考图像的模式下,所以在拍照时需要进行对焦的控制。对焦不准确,图像就会变得比较模糊不清晰。相机对焦时通过一些清晰度评判指标,控制镜头与CCD的距离,使图像成像清晰。一般对焦时有一个调整的过程,图像从模糊到清晰,再到模糊,确定清晰度峰值,再最终到达最清晰的位置。

常见的图像清晰度评价一般都是基于梯度的方法,本文将介绍五种简单的评价指标,分别是Brenner梯度法、Tenegrad梯度法、laplace梯度法、方差法、能量梯度法。

2、Halcon代码

①、Tenegrad函数

1 //Tenegrad梯度法
2 sobel_amp(Image, EdgeAmplitude, 'sum_sqrt', 3)
3 min_max_gray(EdgeAmplitude, EdgeAmplitude, 0, Min, Max, Range)
4 threshold(EdgeAmplitude, Region1, 20, 255)
5 region_to_bin(Region1, BinImage, 1, 0, Width, Height)
6 mult_image(EdgeAmplitude, BinImage, ImageResult4, 1, 0)
7 mult_image(ImageResult4, ImageResult4, ImageResult, 1, 0)
8 intensity(ImageResult, ImageResult, Value, Deviation)

②、方差函数

1 //方差法
2 region_to_mean(ImageReduced, Image, ImageMean)
3 convert_image_type(ImageMean, ImageMean, 'real')
4 convert_image_type(Image, Image, 'real')
5 sub_image(Image, ImageMean, ImageSub, 1, 0)
6 mult_image(ImageSub, ImageSub, ImageResult, 1, 0)
7 intensity(ImageResult, ImageResult, Value, Deviation)

③、能量函数

 1 //能量梯度函数
 2 crop_part(Image, ImagePart00, 0, 0, Width-1, Height-1)
 3 crop_part(Image, ImagePart01, 0, 1, Width-1, Height-1)
 4 crop_part(Image, ImagePart10, 1, 0, Width-1, Height-1)
 5 convert_image_type(ImagePart00, ImagePart00, 'real')
 6 convert_image_type(ImagePart10, ImagePart10, 'real')
 7 convert_image_type(ImagePart01, ImagePart01, 'real')
 8 sub_image(ImagePart10, ImagePart00, ImageSub1, 1, 0)
 9 mult_image(ImageSub1, ImageSub1, ImageResult1, 1, 0)
10 sub_image(ImagePart01, ImagePart00, ImageSub2, 1, 0)
11 mult_image(ImageSub2, ImageSub2, ImageResult2, 1, 0)
12 add_image(ImageResult1, ImageResult2, ImageResult, 1, 0)
13 intensity(ImageResult, ImageResult, Value, Deviation)

④、Brenner函数

1 //Brenner梯度法
2 crop_part(Image, ImagePart00, 0, 0, Width, Height-2)
3 convert_image_type(ImagePart00, ImagePart00, 'real')
4 crop_part(Image, ImagePart20, 2, 0, Width, Height-2)
5 convert_image_type(ImagePart20, ImagePart20, 'real')
6 sub_image(ImagePart20, ImagePart00, ImageSub, 1, 0)
7 mult_image(ImageSub, ImageSub, ImageResult, 1, 0)
8 intensity(ImageResult, ImageResult, Value, Deviation)

⑤、Laplace函数

1 //拉普拉斯梯度函数
2 laplace(Image, ImageLaplace4, 'signed', 3, 'n_4')
3 laplace(Image, ImageLaplace8, 'signed', 3, 'n_8')
4 add_image(ImageLaplace4, ImageLaplace4, ImageResult1, 1, 0)
5 add_image(ImageLaplace4, ImageResult1, ImageResult1, 1, 0)
6 add_image(ImageLaplace8, ImageResult1, ImageResult1, 1, 0)
7 mult_image(ImageResult1, ImageResult1, ImageResult, 1, 0)
8 intensity(ImageResult, ImageResult, Value, Deviation)
原文地址:https://www.cnblogs.com/ybqjymy/p/15166088.html