计算CAF FV值的demo code

1 CAF FV基本介绍

自动对焦AF有很多算法,用的最多的是CAF和PDAF。
完成AF的第一步就是计算出FV,本文首先提供了一份计算CAF FV值的demo code,以方便有需要的人使用,毕竟现在CSDN上好多AF的code都无法使用。
然后本文将demo code合入到平台的HAL层,并做了几点优化。
CAF是Continuous Auto Focus(持续自动对焦)的缩写。
FV是Focus Value(对焦值)的缩写。
ROI是Region Of Interest(感兴趣区域)的缩写。
简单来讲,FV是由一张图中ROI区域的灰度值计算出来的。原理请看参考资料中的pdf文档"反差式相机对焦系统原理"。

2 focus_test_code

2.1 编译

直接运行编译脚本build.sh即可

2.2 运行

直接运行./test即可
demo code的输入是bmp图片,取10张由模糊到清晰再到模糊的bmp图片(1920x1080),选固定的对焦区域,查看FV值

可以看到FV值由低到高再到低,表示图片由模糊到清晰再到模糊的过程。
demo code放在github上:
https://github.com/hanyuhang-hz/focus-value

3 合入到平台HAL层及优化

3.1 合入到平台HAL层

demo code的输入是bmp,得到FV的转化逻辑:
bmp->bgr->rgb->grey->FV
而HAL层的输入是nv21(yuv420sp),最早的转化逻辑是:
nv21->bmp,bmp->bgr->rgb->grey->FV
nv21->bmp参考:
https://blog.csdn.net/qq_42955378/article/details/117477173
即nv21->bgr+header->bmp,即转化逻辑变为:
nv21->bgr+header->bmp,bmp->bgr->rgb->grey->FV
过于繁琐,不如优化掉直接:
nv21->rgb->grey->FV

3.2 log看耗时

从实际预览看计算FV会导致严重耗时,"卡顿"出现
log看主要耗时:

02-09 10:25:40.834  3289  5875 I MIPISensor:   400 captureNV21 - captureNV21, hyh contrast focus>>>
02-09 10:25:40.834  3289  5875 I MIPISensor:  1741 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>
02-09 10:25:40.834  3289  5875 I MIPISensor:  1767 ImageLoad - ImageLoad, hyh contrast ImageLoad rgbdata malloc>>>
02-09 10:25:40.834  3289  5875 I MIPISensor:  1774 ImageLoad - ImageLoad, hyh contrast ImageLoad rgbdata malloc<<<
02-09 10:25:40.834  3289  5875 I MIPISensor:  1822 NV212RGBorBGR - NV212RGBorBGR, hyh contrast NV212RGBorBGR>>>
02-09 10:25:41.204  3289  5875 I MIPISensor:  1853 NV212RGBorBGR - NV212RGBorBGR, hyh contrast NV212RGBorBGR<<<
02-09 10:25:41.204  3289  5875 I MIPISensor:  1790 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
02-09 10:25:41.204  3289  5875 I MIPISensor:  1797 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
02-09 10:25:41.219  3289  5875 I MIPISensor:  1816 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<
02-09 10:25:41.219  3289  5875 I MIPISensor:   417 captureNV21 - hyh contrast:===============================================>1828
02-09 10:25:41.220  3289  5875 I MIPISensor:   427 captureNV21 - captureNV21, hyh contrast focus<<<

共耗时386ms,可以看到时间主要耗时在NV21->RGB的过程中,这个过程共耗时370ms。

3.3 优化

再看转化逻辑:
nv21->rgb->grey->FV
实际分析可以得出结论,FV只与灰度图有关,而yuv的y不就是灰度么?!
yuv2grey:

将nv21->rgb->grey->FV 改为 nv21->grey->FV:

//dump grey
02-09 15:13:50.517  3292  5670 I MIPISensor:   399 captureNV21 - captureNV21, hyh contrast focus>>>
02-09 15:13:50.517  3292  5670 I MIPISensor:  1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>
02-09 15:13:50.517  3292  5670 I MIPISensor:  1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
02-09 15:13:50.517  3292  5670 I MIPISensor:  1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
02-09 15:13:50.599  3292  5670 I MIPISensor:  1812 ImageLoad - hyh contrast filename_grey:/data/misc/cameraserver/2970_1920x1080.grey
02-09 15:13:50.610  3292  5670 I MIPISensor:  1822 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<
02-09 15:13:50.610  3292  5670 I MIPISensor:   418 captureNV21 - hyh contrast:===============================================>1935
02-09 15:13:50.610  3292  5670 I MIPISensor:   428 captureNV21 - captureNV21, hyh contrast focus<<<

//not dump grey
02-09 15:23:04.302  3288  5822 I MIPISensor:   399 captureNV21 - captureNV21, hyh contrast focus>>>
02-09 15:23:04.302  3288  5822 I MIPISensor:  1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>
02-09 15:23:04.302  3288  5822 I MIPISensor:  1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
02-09 15:23:04.302  3288  5822 I MIPISensor:  1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
02-09 15:23:04.389  3288  5822 I MIPISensor:  1822 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<<
02-09 15:23:04.389  3288  5822 I MIPISensor:   418 captureNV21 - hyh contrast:===============================================>1810
02-09 15:23:04.389  3288  5822 I MIPISensor:   428 captureNV21 - captureNV21, hyh contrast focus<<<

优化后,计算FV的时间由380ms->87ms。
继续优化,将nv21->grey由的code,由for循环改为memcpy后:

//dump grey
02-09 15:36:31.468  3288  5648 I MIPISensor:   399 captureNV21 - captureNV21, hyh contrast focus>>>
02-09 15:36:31.468  3288  5648 I MIPISensor:  1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>>
02-09 15:36:31.468  3288  5648 I MIPISensor:  1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
02-09 15:36:31.468  3288  5648 I MIPISensor:  1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
02-09 15:36:31.485  3288  5648 I MIPISensor:  1813 ImageLoad - hyh contrast filename_grey:/data/misc/cameraserver/1740_1920x1080.grey
02-09 15:36:31.516  3288  5648 I MIPISensor:  1823 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<<
02-09 15:36:31.516  3288  5648 I MIPISensor:   418 captureNV21 - hyh contrast:===============================================>2462
02-09 15:36:31.516  3288  5648 I MIPISensor:   428 captureNV21 - captureNV21, hyh contrast focus<<<

//not dump grey
02-09 15:46:02.363  3290  5873 I MIPISensor:   399 captureNV21 - captureNV21, hyh contrast focus>>>
02-09 15:46:02.363  3290  5873 I MIPISensor:  1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>>
02-09 15:46:02.363  3290  5873 I MIPISensor:  1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
02-09 15:46:02.363  3290  5873 I MIPISensor:  1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<<
02-09 15:46:02.380  3290  5873 I MIPISensor:  1823 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<<
02-09 15:46:02.381  3290  5873 I MIPISensor:   418 captureNV21 - hyh contrast:===============================================>1712
02-09 15:46:02.384  3290  5873 I MIPISensor:   428 captureNV21 - captureNV21, hyh contrast focus<<<

优化后,计算FV的时间由87ms->21ms。


3.4 log看耗时

手动调节sensor焦距,FV值会按照预期变化且如图每帧计算的FV值时间分别为17ms,14ms,18ms和21ms。

4 参考资料

https://github.com/Windaway/Autofocus

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值