Skip to main content

깊이 추정 — 2D에서 3D 거리 이해

깊이 추정(Depth Estimation)은 단일 이미지에서 각 픽셀까지의 거리(깊이)를 예측하는 태스크입니다. 3D 재구성, AR/VR, 로봇 내비게이션, 자율주행에 활용됩니다.

핵심 아이디어

인간은 양안 시차(Stereo Vision)로 깊이를 인식하지만, 단안 깊이 추정(Monocular Depth Estimation)은 단일 카메라 이미지만으로 깊이를 추정합니다. 원근법, 텍스처 밀도, 오클루전 등의 시각적 단서를 학습합니다.

동작 방식

유형출력활용
상대 깊이(Relative)순서 관계만 (가까움/멀음)시각 효과, 배경 분리
절대 깊이(Metric)실제 거리(미터)로봇, 자율주행

구현

Depth Anything V2 (Transformers)

from transformers import pipeline
from PIL import Image

# 깊이 추정 파이프라인
pipe = pipeline("depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf")

image = Image.open("image.jpg")
result = pipe(image)

depth_map = result["depth"]  # PIL 이미지
depth_map.save("depth_output.png")

MiDaS (PyTorch Hub)

import torch
import cv2

# MiDaS 모델 로드
model = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
transform = midas_transforms.dpt_transform

model.eval()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

# 추론
image = cv2.imread("image.jpg")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
input_tensor = transform(image_rgb).to(device)

with torch.no_grad():
    prediction = model(input_tensor)
    prediction = torch.nn.functional.interpolate(
        prediction.unsqueeze(1),
        size=image.shape[:2],
        mode="bicubic",
    ).squeeze()

depth_map = prediction.cpu().numpy()

관련 기술 비교

모델방식속도정확도특징
MiDaSDPT (ViT + Dense Prediction)보통높음범용 상대 깊이
Depth Anything V2DINOv2 기반빠름매우 높음최신, 범용 추천
ZoeDepthMiDaS + Metric Head보통높음절대 깊이 가능
UniDepth범용 Metric Depth보통높음카메라 내재 파라미터 불필요
상대 깊이는 “A가 B보다 가깝다”는 순서만 알려줍니다. 절대 깊이는 “A까지 3.2m, B까지 5.7m”처럼 실제 거리를 제공합니다. 대부분의 범용 모델은 상대 깊이를 출력하며, 절대 깊이가 필요하면 Metric Depth 모델을 사용해야 합니다.

참고 논문

논문학회/연도링크
MiDaS: Towards Robust Monocular DepthIEEE T-PAMI 2022arXiv:1907.01341
Depth AnythingCVPR 2024arXiv:2401.10891
Depth Anything V2NeurIPS 2024arXiv:2406.09414