一、背景

深度估计是计算机视觉领域的一项重要任务,旨在从单张RGB图像中预测每个像素到相机的距离信息,生成对应的深度图。这项技术在自动驾驶、机器人导航、增强现实、3D重建等众多应用中发挥着关键作用。

Depth-Anything-V2 是 Depth-Anything 项目的第二代版本,由腾讯ARC Lab开发。相比前代,V2版本在模型架构、训练策略和性能上都有显著提升。它采用了更强大的视觉Transformer(ViT)作为编码器,支持多种模型尺寸(ViT-S、ViT-B、ViT-L、ViT-G),能够适应不同计算资源和精度需求。

本文将通过实际示例,详细介绍如何使用 Depth-Anything-V2 进行深度估计,包括环境配置、模型选择、参数调整等关键步骤。


图1 原图

图2 深度估计图

二、代码

代码主页:https://github.com/DepthAnything/Depth-Anything-V2

2.1 下载代码与安装运行环境

git clone https://github.com/DepthAnything/Depth-Anything-V2
cd Depth-Anything-V2
pip install -r requirements.txt

2.2 运行脚本

python run.py \
  --encoder 模型名称 \
  --img-path 图片路径 \
  --outdir 输出目录 \
  --input-size 输入尺寸 \
  [--pred-only] \
  [--grayscale]

示例1:使用 ViT-S 模型

python run.py \
  --encoder vits \
  --img-path ./test.jpg \
  --outdir ./output

示例2:使用 ViT-B 模型,指定输入尺寸

python run.py \
  --encoder vitb \
  --img-path ./test.jpg \
  --outdir ./output \
  --input-size 518

示例3:只输出深度预测结果

python run.py \
  --encoder vitl \
  --img-path ./test.jpg \
  --outdir ./output \
  --pred-only

这里的 --pred-only 是一个开关参数,不需要填写值。

示例4:输出灰度深度图

python run.py \
  --encoder vitg \
  --img-path ./test.jpg \
  --outdir ./output \
  --grayscale

示例5:同时使用多个选项

python run.py \
  --encoder vitl \
  --img-path ./images/test.jpg \
  --outdir ./depth_results \
  --input-size 518 \
  --pred-only \
  --grayscale

三、备注

在这里插入图片描述

vits | vitb | vitl | vitg 四个模型能力依次增强,vitb 一般就挺好用了。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐