• 计算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看主要耗时:

    1. 02-09 10:25:40.834 3289 5875 I MIPISensor: 400 captureNV21 - captureNV21, hyh contrast focus>>>
    2. 02-09 10:25:40.834 3289 5875 I MIPISensor: 1741 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>
    3. 02-09 10:25:40.834 3289 5875 I MIPISensor: 1767 ImageLoad - ImageLoad, hyh contrast ImageLoad rgbdata malloc>>>
    4. 02-09 10:25:40.834 3289 5875 I MIPISensor: 1774 ImageLoad - ImageLoad, hyh contrast ImageLoad rgbdata malloc<<<
    5. 02-09 10:25:40.834 3289 5875 I MIPISensor: 1822 NV212RGBorBGR - NV212RGBorBGR, hyh contrast NV212RGBorBGR>>>
    6. 02-09 10:25:41.204 3289 5875 I MIPISensor: 1853 NV212RGBorBGR - NV212RGBorBGR, hyh contrast NV212RGBorBGR<<<
    7. 02-09 10:25:41.204 3289 5875 I MIPISensor: 1790 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
    8. 02-09 10:25:41.204 3289 5875 I MIPISensor: 1797 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
    9. 02-09 10:25:41.219 3289 5875 I MIPISensor: 1816 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<
    10. 02-09 10:25:41.219 3289 5875 I MIPISensor: 417 captureNV21 - hyh contrast:===============================================>1828
    11. 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:

    1. //dump grey
    2. 02-09 15:13:50.517 3292 5670 I MIPISensor: 399 captureNV21 - captureNV21, hyh contrast focus>>>
    3. 02-09 15:13:50.517 3292 5670 I MIPISensor: 1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>
    4. 02-09 15:13:50.517 3292 5670 I MIPISensor: 1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
    5. 02-09 15:13:50.517 3292 5670 I MIPISensor: 1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
    6. 02-09 15:13:50.599 3292 5670 I MIPISensor: 1812 ImageLoad - hyh contrast filename_grey:/data/misc/cameraserver/2970_1920x1080.grey
    7. 02-09 15:13:50.610 3292 5670 I MIPISensor: 1822 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<
    8. 02-09 15:13:50.610 3292 5670 I MIPISensor: 418 captureNV21 - hyh contrast:===============================================>1935
    9. 02-09 15:13:50.610 3292 5670 I MIPISensor: 428 captureNV21 - captureNV21, hyh contrast focus<<<
    10. //not dump grey
    11. 02-09 15:23:04.302 3288 5822 I MIPISensor: 399 captureNV21 - captureNV21, hyh contrast focus>>>
    12. 02-09 15:23:04.302 3288 5822 I MIPISensor: 1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>
    13. 02-09 15:23:04.302 3288 5822 I MIPISensor: 1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
    14. 02-09 15:23:04.302 3288 5822 I MIPISensor: 1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
    15. 02-09 15:23:04.389 3288 5822 I MIPISensor: 1822 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<<
    16. 02-09 15:23:04.389 3288 5822 I MIPISensor: 418 captureNV21 - hyh contrast:===============================================>1810
    17. 02-09 15:23:04.389 3288 5822 I MIPISensor: 428 captureNV21 - captureNV21, hyh contrast focus<<<

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

    1. //dump grey
    2. 02-09 15:36:31.468 3288 5648 I MIPISensor: 399 captureNV21 - captureNV21, hyh contrast focus>>>
    3. 02-09 15:36:31.468 3288 5648 I MIPISensor: 1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>>
    4. 02-09 15:36:31.468 3288 5648 I MIPISensor: 1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
    5. 02-09 15:36:31.468 3288 5648 I MIPISensor: 1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<
    6. 02-09 15:36:31.485 3288 5648 I MIPISensor: 1813 ImageLoad - hyh contrast filename_grey:/data/misc/cameraserver/1740_1920x1080.grey
    7. 02-09 15:36:31.516 3288 5648 I MIPISensor: 1823 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<<
    8. 02-09 15:36:31.516 3288 5648 I MIPISensor: 418 captureNV21 - hyh contrast:===============================================>2462
    9. 02-09 15:36:31.516 3288 5648 I MIPISensor: 428 captureNV21 - captureNV21, hyh contrast focus<<<
    10. //not dump grey
    11. 02-09 15:46:02.363 3290 5873 I MIPISensor: 399 captureNV21 - captureNV21, hyh contrast focus>>>
    12. 02-09 15:46:02.363 3290 5873 I MIPISensor: 1742 ImageLoad - ImageLoad, hyh contrast ImageLoad>>>>
    13. 02-09 15:46:02.363 3290 5873 I MIPISensor: 1793 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc>>>
    14. 02-09 15:46:02.363 3290 5873 I MIPISensor: 1800 ImageLoad - ImageLoad, hyh contrast ImageLoad greydata malloc<<<<
    15. 02-09 15:46:02.380 3290 5873 I MIPISensor: 1823 ImageLoad - ImageLoad, hyh contrast ImageLoad<<<<
    16. 02-09 15:46:02.381 3290 5873 I MIPISensor: 418 captureNV21 - hyh contrast:===============================================>1712
    17. 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

  • 相关阅读:
    k8s学习-CKA真题-集群故障排查kubelet
    图像分割算法
    mac: nvm is already installed in /Users/**/.nvm, trying to update using git
    Windows 11 上使用安卓应用及安装谷歌 Google Play和亚马逊 应用商店
    MATLAB——概率神经网络分类问题程序
    Android 内存优化&内存泄漏处理
    安卓玩机----解锁system分区 可读写系统分区 magisk面具模块
    Fiber Golang:Golang中的强大Web框架
    10道不得不会的Docker面试题
    计算机前沿(从三次数学危机到量子计算)
  • 原文地址:https://blog.csdn.net/u012906122/article/details/126563270