3D 데이터 처리에서 voxel 기반 segmentation 결과를 mesh geometry에 매핑해야 하는 경우가 자주 발생합니다. 예를 들어, medical imaging이나 3D reconstruction에서는 voxel grid 상에 얻어진 segmentation 결과를 원래의 mesh surface에 옮겨야 할 수 있습니다.
이 글에서는 voxel segmentation label이 주어졌을 때, mesh vertex에 해당 label을 trilinear interpolation을 이용해 보간하는 방법을 설명합니다.
주어짐:
D x H x W
), 각 voxel vertex 또는 cell에는 segmentation label 정보가 있음 (예: class index, one-hot label, 또는 softmax 확률).N x 3
)목표:
Trilinear interpolation은 3D 공간에서 정규격자(voxel grid) 위에 존재하는 데이터 값을 이용해, 임의 위치에서의 값을 추정하는 방법입니다.
이를 mesh vertex에 적용하면 다음과 같은 절차로 segmentation label을 보간할 수 있습니다.
입력 voxel label은 shape (C, D, H, W)
형태로 준비합니다.
C
는 class 수입니다 (예: 4개의 클래스라면 C=4).mesh_vertex
는 보통 world coordinate에 있으므로, voxel grid 범위로 정규화(normalization) 합니다.
x_normalized = (x - min) / (max - min) * 2 - 1
[-1, 1]
범위로 변환 (PyTorch의 grid_sample
기준)grid_sample
을 이용해 interpolation을 수행할 수 있습니다.argmax
를 취해 최종 class index를 부여할 수 있습니다.import torch
import torch.nn.functional as F
# voxel_labels: (1, C, D, H, W)
# mesh_vertices: (N, 3)
# 1. Normalize coordinates to [-1, 1]
coords = (mesh_vertices - min_coord) / (max_coord - min_coord) * 2 - 1
coords = coords.view(1, 1, 1, -1, 3) # (1, D=1, H=1, W=N, 3)
# 2. Perform trilinear interpolation
sampled = F.grid_sample(voxel_labels, coords, mode='bilinear', align_corners=True)
# 3. Get class index
vertex_labels = sampled.squeeze().argmax(dim=0) # shape: (N,)
Trilinear interpolation은 voxel → mesh vertex로 label 정보를 매핑할 때 매우 유용한 기법입니다.
특히 voxel-based 모델의 출력을 mesh로 시각화하거나 후처리할 때 널리 활용됩니다.