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

ops/2024/10/25 2:37:21/

第一,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/ops/128217.html

相关文章

Pandas分组与分箱

目录 分组 df.groupby分组函数返回分组对象 去除每组第一条或最后一条数据 获取分组后的每组名称 get_group()按组依据获取其中一组 分组聚合 分组后直接聚合 分组后指定单列或多列聚合 分组后使用多个聚合函数 分组后对多列分别使用不同的聚合函数 分组后使用自定义…

Ansible for Windows hosts(ansible.windows 模块介绍)

Ansible 具有许多专为 Windows 操作系统设计的模块,它使得自动化 Windows 任务变得简单。下面我将介绍一些常用的 Ansible Windows 模块,以及如何配置 Ansible 以管理 Windows 主机。 更详细的用法请参考:Using Ansible and Windows — Ansi…

使用预训练的BERT进行金融领域问答

获取更多完整项目代码数据集,点此加入免费社区群 : 首页-置顶必看 1. 项目简介 本项目旨在开发并优化一个基于预训练BERT模型的问答系统,专注于金融领域的应用。随着金融市场信息复杂性和规模的增加,传统的信息检索方法难以高效…

高效车辆管理:SpringBoot实现指南

1系统概述 1.1 研究背景 随着计算机技术的发展以及计算机网络的逐渐普及,互联网成为人们查找信息的重要场所,二十一世纪是信息的时代,所以信息的管理显得特别重要。因此,使用计算机来管理车辆管理系统的相关信息成为必然。开发合适…

【黑马Redis原理篇】Redis网络模型

来源视频 [16,27] 文章目录 1.用户空间和内核空间空间划分缓冲区 2.IO模型2.1 阻塞IO2.2 非阻塞IO2.3 IO多路复用2.3.1 阻塞和非阻塞的对比2.3.2 IO多路复用2.3.3 监听FD方式、通知的方式,有多种实现 2.4 信号驱动IO2.5 异步IO2.6 真正的同步和异步 3.Redis是单线程…

十六、行为型(责任链模式)

责任链模式(Chain of Responsibility Pattern) 概念 责任链模式是一种行为型设计模式,它使多个对象都有机会处理请求,从而避免请求的发送者与接收者之间的耦合。将这些对象连成一条链,并沿着这条链传递请求&#xff0…

docker 误删gitlab文件,另类的删库跑路,如何进行恢复?

缘起:由于看到linux服务器内存快满了,于是本着责任感,想着清理一下内存,结果在看到docker文件占了20多个G,于是想着,我们就三个容器,为啥这么大,肯定是有诈,于是就一个个…

freeswitch-esl动态控制录制音频(开始、停止)

场景描述:在控制freeswitch中使用ESL socket连接,其实类型连接TCP差不多。 当A和B在通话中,我想录制它们通话内容,录制格式为wav格式音频文件。代码如下#include <iostream> #include <string> #include <esl/esl.h><