机器视觉相机自动对焦算法

server/2024/10/25 3:22:27/

第一,Brenner梯度法、

第二,Tenegrad梯度法、

第三,laplace梯度法、

第四,方差法、

第五,能量梯度法。

此实例通过使用Halcon实现5种清晰度算法函数:
1. 方差算法函数;
2. 拉普拉斯能量函数;
3. 能量梯度函数;
4. Brenner函数;
5. Tenegrad函数;
测试效果如下图片;找到峰值对应的那张图,确实是最清晰的那张;使用直方图显示清晰度结果,如果有更好的方法,那就跟帖回复吧。
此实例有HalconBBS群友提供!
 
 
 
*evaluate_definition的使用例子
*使用halcon自带的图片
*实现了五种评价函数,
*选择算子的Method值,可以观察不同评价函数的效果。
read_image (Image, 'pcb_focus/pcb_focus_telecentric_106')
dev_update_off ()
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 752, 480, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_color ('lime green')
dev_set_line_width (3)
Ret:=[]
get_image_size(Image, Width, Height)
for Index := 1 to 121 by 1
    read_image (Image, 'pcb_focus/pcb_focus_telecentric_'+Index$'03d')
    
    evaluate_definition (Image, 'Tenegrad', Value)
    
    dev_display (Image)
    Ret:=[Ret,Value]
endfor
*使用直方图显示清晰度结果,如果有更好的方法,那就跟帖回复吧
VMax:=max(Ret)
VMin:=min(Ret)
GRet := 100*(Ret-VMin)/(VMax-VMin)
gen_region_histo(Region, Ret, 255, 255, 1)
*找到峰值对应的那张图,确实是最清晰的那张。
qxd:=find(Ret, max(Ret))
read_image (GoodImage, 'pcb_focus/pcb_focus_telecentric_'+qxd$'03d')
dev_display (GoodImage)
dev_display (Region)
evaluate_definition函数代码如下:

scale_image_max(Image, Image)
get_image_size(Image, Width, Height)if(Method = 'Deviation')
*方差法region_to_mean (Image, Image, ImageMean) convert_image_type (ImageMean, ImageMean, 'real')convert_image_type (Image, Image, 'real') sub_image(Image, ImageMean, ImageSub, 1, 0)mult_image(ImageSub, ImageSub, ImageResult, 1, 0)intensity(ImageResult, ImageResult, Value, Deviation) elseif(Method = 'laplace')
*拉普拉斯能量函数laplace (Image, ImageLaplace4, 'signed', 3, 'n_4')laplace (Image, ImageLaplace8, 'signed', 3, 'n_8')add_image(ImageLaplace4,ImageLaplace4,ImageResult1, 1, 0)add_image(ImageLaplace4,ImageResult1,ImageResult1, 1, 0)add_image(ImageLaplace8,ImageResult1,ImageResult1, 1, 0)mult_image(ImageResult1, ImageResult1, ImageResult, 1, 0)intensity(ImageResult, ImageResult, Value, Deviation)elseif(Method = 'energy')
*能量梯度函数crop_part(Image, ImagePart00, 0, 0, Width-1, Height-1)crop_part(Image, ImagePart01, 0, 1, Width-1, Height-1)crop_part(Image, ImagePart10, 1, 0, Width-1, Height-1)convert_image_type (ImagePart00, ImagePart00, 'real')convert_image_type (ImagePart10, ImagePart10, 'real')convert_image_type (ImagePart01, ImagePart01, 'real')sub_image(ImagePart10, ImagePart00, ImageSub1, 1, 0)mult_image(ImageSub1, ImageSub1, ImageResult1, 1, 0)sub_image(ImagePart01, ImagePart00, ImageSub2, 1, 0)mult_image(ImageSub2, ImageSub2, ImageResult2, 1, 0)add_image(ImageResult1, ImageResult2, ImageResult, 1, 0)    intensity(ImageResult, ImageResult, Value, Deviation)
elseif(Method = 'Brenner')
*Brenner函数法crop_part(Image, ImagePart00, 0, 0, Width, Height-2)convert_image_type (ImagePart00, ImagePart00, 'real')crop_part(Image, ImagePart20, 2, 0, Width, Height-2)convert_image_type (ImagePart20, ImagePart20, 'real')sub_image(ImagePart20, ImagePart00, ImageSub, 1, 0)mult_image(ImageSub, ImageSub, ImageResult, 1, 0)intensity(ImageResult, ImageResult, Value, Deviation)
elseif(Method = 'Tenegrad')
*Tenegrad函数法sobel_amp (Image, EdgeAmplitude, 'sum_sqrt', 3)min_max_gray(EdgeAmplitude, EdgeAmplitude, 0, Min, Max, Range)threshold(EdgeAmplitude, Region1, 11.8, 255)region_to_bin(Region1, BinImage, 1, 0, Width, Height)mult_image(EdgeAmplitude, BinImage, ImageResult4, 1, 0)mult_image(ImageResult4, ImageResult4, ImageResult, 1, 0)intensity(ImageResult, ImageResult, Value, Deviation)elseif(Method = '2')elseif(Method = '3')endifreturn ()

scale_image_max(Image, Image)
get_image_size(Image, Width, Height)

if(Method = 'Deviation')
*方差法
    region_to_mean (Image, Image, ImageMean) 
    convert_image_type (ImageMean, ImageMean, 'real')
    convert_image_type (Image, Image, 'real') 
    sub_image(Image, ImageMean, ImageSub, 1, 0)
    mult_image(ImageSub, ImageSub, ImageResult, 1, 0)
    intensity(ImageResult, ImageResult, Value, Deviation) 
    
elseif(Method = 'laplace')
*拉普拉斯能量函数
    laplace (Image, ImageLaplace4, 'signed', 3, 'n_4')
    laplace (Image, ImageLaplace8, 'signed', 3, 'n_8')
    add_image(ImageLaplace4,ImageLaplace4,ImageResult1, 1, 0)
    add_image(ImageLaplace4,ImageResult1,ImageResult1, 1, 0)
    add_image(ImageLaplace8,ImageResult1,ImageResult1, 1, 0)
    mult_image(ImageResult1, ImageResult1, ImageResult, 1, 0)
    intensity(ImageResult, ImageResult, Value, Deviation)

elseif(Method = 'energy')
*能量梯度函数
    crop_part(Image, ImagePart00, 0, 0, Width-1, Height-1)
    crop_part(Image, ImagePart01, 0, 1, Width-1, Height-1)
    crop_part(Image, ImagePart10, 1, 0, Width-1, Height-1)
    convert_image_type (ImagePart00, ImagePart00, 'real')
    convert_image_type (ImagePart10, ImagePart10, 'real')
    convert_image_type (ImagePart01, ImagePart01, 'real')
    sub_image(ImagePart10, ImagePart00, ImageSub1, 1, 0)
    mult_image(ImageSub1, ImageSub1, ImageResult1, 1, 0)
    sub_image(ImagePart01, ImagePart00, ImageSub2, 1, 0)
    mult_image(ImageSub2, ImageSub2, ImageResult2, 1, 0)
    add_image(ImageResult1, ImageResult2, ImageResult, 1, 0)    
    intensity(ImageResult, ImageResult, Value, Deviation)
elseif(Method = 'Brenner')
*Brenner函数法
    crop_part(Image, ImagePart00, 0, 0, Width, Height-2)
    convert_image_type (ImagePart00, ImagePart00, 'real')
    crop_part(Image, ImagePart20, 2, 0, Width, Height-2)
    convert_image_type (ImagePart20, ImagePart20, 'real')
    sub_image(ImagePart20, ImagePart00, ImageSub, 1, 0)
    mult_image(ImageSub, ImageSub, ImageResult, 1, 0)
    intensity(ImageResult, ImageResult, Value, Deviation)
elseif(Method = 'Tenegrad')
*Tenegrad函数法
    sobel_amp (Image, EdgeAmplitude, 'sum_sqrt', 3)
    min_max_gray(EdgeAmplitude, EdgeAmplitude, 0, Min, Max, Range)
    threshold(EdgeAmplitude, Region1, 11.8, 255)
    region_to_bin(Region1, BinImage, 1, 0, Width, Height)
    mult_image(EdgeAmplitude, BinImage, ImageResult4, 1, 0)
    mult_image(ImageResult4, ImageResult4, ImageResult, 1, 0)
    intensity(ImageResult, ImageResult, Value, Deviation)
   
elseif(Method = '2')

elseif(Method = '3')
    
endif
    
return ()


http://www.ppmy.cn/server/134591.html

相关文章

置分辨率设置多显示器的时候提示, 某些设置由系统管理员进行管理

遇到的问题 设置分辨率设置多显示器的时候提示(如下图所示): 某些设置由系统管理员进行管理 解决方法 先试试这个方法: https://answers.microsoft.com/zh-hans/windows/forum/all/%E6%9B%B4%E6%94%B9%E5%88%86%E8%BE%A8%E7%8…

得物iOS函数调用栈及符号化调优实践|得物技术

一、背景 随着《个人信息保护法》等法律法规的逐步发布实施,个人隐私保护受到越来越多人的关注。在这个整体的大背景下,得物持续完善App的各项合规属性,而在这个过程中,绕不开法务、安全、产品、设计、研发、测试几个重要环节&am…

[LeetCode] 207. 课程表

题目描述: 你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。 在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] [ai, bi] ,表示如果要学习课程 ai 则 必须 先…

CZX前端秘籍1

盒模型 1 标准盒模型 box-sizing: content-box; margin border padding content 2 怪异(IE)盒模型 box-sizing: border-box; margin content(border padding content) 3 区别 计算宽高的时候,怪异盒模型会把边框的内边距计算进…

Spark 基础概念

Apache Spark 是一个快速、分布式的计算系统,用于大规模数据处理和分析。它提供了一个高级 API,用于编写并行处理的任务,可以在大规模集群上运行。 Spark 的基本概念包括以下几个方面: Resilient Distributed Datasets (RDDs)&a…

5. Node.js Http模块

2.4 Http模块 2.4.1创建Http服务端 //1.导入http模块 let httprequire(http)//2.创建服务对象 let serverhttp.createServer((request,response)>{console.log(request.method) //获取请求方式console.log(request.url) //获取请求url(路径和参数部份)co…

全面击破工程级复杂缓存难题

目录 一、走进业务中的缓存 (一)本地缓存 (二)分布式缓存 二、缓存更新模式分析 (一)Cache Aside Pattern(旁路缓存模式) 读操作流程 写操作流程 流程问题思考 问题1&#…

如何利用 OCR 和文档处理,快速提高供应商管理效率 ?

在当今瞬息万变的商业环境中,有效的供应商管理通常需要处理大量实物文档,这带来了巨大的挑战。手动提取供应商名称、编号和其他关键信息等关键细节非常耗时、容易出错,并且会降低整体效率。 为了应对这些挑战,组织正在逐步采用自…