自动对焦算法

自动对焦算法

算法

图像清晰度参考 https://blog.csdn.net/dcrmg/article/details/53543341

在实际应用中,清晰度随着对焦点的移动,呈以下图像的趋势。说白了就是有一个波峰,我们进行对焦的时候会经过这个波峰,那怎么知道这个波峰在哪呢?
在这里插入图片描述

步骤

1、首先我们一开始是不知道图像是否在对焦点的,因为清晰度这玩意儿,变化非常大,分辨率,亮度等都会影响,所以是没办法定一个死的峰值来判断,这就需要我们随机往一个方向进行对焦(这里我用的是垂直电机对焦)。
2、第二张图你就会知道,清晰度是增加了,还是减少了,就可以知道我们应该往哪个方向进行对焦。当然这一步也有可能因为步幅太大,直接越过波峰和同等清晰度的点,但是实际应用中这种情况比较少见,因为我们可以手动修改步幅,尽可能避免这种情况。
3、知道方向之后我们一直运动清晰度是一直增加的,跑到越过波峰的时候,我们的清晰度会比前一次的低。这时候我们可以判断我们越过了波峰。
4‘、这时候我们可以往反方向进行小步幅进行精对焦,知道第二次越过波峰(这里我们可以进行第三次更小步幅对焦,但是清晰度这东西很玄学,这么小的步幅很有可能会出现清晰度理应增加的方向,清晰度降低了,自行判断即可,实际应用中两轮就可以了,如果是合适的步幅运动一轮也已经可以达到不错的效果)。

注意:不能用全图进行对焦,所以ROI很重要。

代码

    public void AutoFocus()
    {
    label:
        List<double> focusL = new List<double>();
        int Round=2;
        int T=1;
        while (true)
        {
            //获取当前清晰度
            Bitmap currentBmp=GetImage();   //这里是获取相机的图片,每个人都有自己的方法,这里不详说,懂意思就行
            Rectangle Roi=new Rectangle();     //需要对焦的区域
            double currentArticulation = GetArticulation(currentBmp,roi);  //这里参考上面的连接,

            focusL.Add(currentArticulation);
            if (focusL.Count == 1) //这里是第一次运动
            {
                RunPulse(pulse); //对焦的运动方法,自己换成自己的
                continue;
            }
            //前两张确认调焦方向
            if (focusL.Count == 2)
            {
                if (currentArticulation > focusL[focusL.Count - 2]) //判断是否跑对方向
                {
                    pulse *= 1; //对的方向
                    RunPulse(pulse);  
                }
                else
                {
                    pulse *= -1; //不对的方向我们相反就可以了
                    RunPulse(pulse*1.5);   //这里不是跑回原来的位置,而是更过去一些,节约时间
                }
                
                continue;
            }
            //清晰度大于前一张图
            if (currentArticulation > focusL[focusL.Count - 2])
            {
                RunPulse(pulse);
            }
            else
            {
                返回峰值,跳出循环
                
	               if(T==Round)
	               {
	               		break; //两轮结束了,跳出循环
	               }
                goto label;
                
            }
        }
    }

后记

这里只提供思路。用什么语言都无所谓的。我这用的是C井

  • 8
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1) Basic concepts of Auto Focus Algorithm 1.1) AF Detection methods There are two focus detection (AF) methods. 1) Distance detection method. 2) Contrast detection method. 1.1.1) Distance detection method The distance detection method is that the camera use AF module for detecting distance for main objects. If the camera is used this AF method, it will not need to use CCD to detect object’s contrast. There are three types of AF modules. 1. Active AF module 2. Passive AF module 3. TTL AF module 1.1.1.1) Active AF module Active AF systems measure distance to the subject independently of the optical system, and subsequently adjust the optical system for correct focus. There are various ways to measure distance, including ultrasonic sound waves and infrared light. In the first case, sound waves are emitted from the camera, and by measuring the delay in their reflection, distance to the subject is calculated. Polaroid cameras were known for successfully applying this system. In the latter case, infrared light is usually used to triangulate the distance to the subject. An exception to the two-step approach is the mechanical autofocus provided in some enlargers, which adjust the lens directly. 1.1.1.2) Passive AF module Passive AF systems determine correct focus by performing passive analysis of the image that is entering the optical system. They generally do not direct any energy, such as ultrasonic sound or infrared light waves, toward the subject. (However, an autofocus assist beam of usually infrared light is required when there is not enough light to take passive measurements.) Passive autofocusing can be achieved by phase detection or contrast measurement.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值