Base Line/python 기초 코드

open3d extract depth map from mesh

ylab 2022. 3. 14. 22:16

별의별것을 다해봤던거 같다 그나마 가장 깔끔하고 자동화 가능할 거 같은 open3d 라이브러리가 나은 거 같다.

이럴때면 누가 옆에서 알려줬으면... ㅋㅋ 

import open3d as o3d
import math
import os
import camera
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
#import open3d
import cv2 as cv
#import loader.loader as load
#from PIL import Image

smplpath="C:\\Users\\user\\Desktop\\normal\\0003.obj"

filepath,fullflname = os.path.split(smplpath)
fname,ext = os.path.splitext(fullflname)
outpath= "C:\\Users\\user\\Desktop\\normal\\depth\\"+fname+'.jpeg'
#outpath=os.path.dirname(filepath)+'\\depth\\'+fname+'.png'
print(outpath)

def custom_draw_geometry_with_key_callback(pcd):
    w=1024
    h=1024
    vis = o3d.visualization.Visualizer()
    vis=vis.add_geometry(pcd)
    def change_background_to_black(vis):
        opt = vis.get_render_option()
        opt.background_color = np.asarray([0, 0, 0])
        return False

    
    def capture_depth(vis):
    	#depth buffer
        depth = vis.capture_depth_float_buffer()
        plt.imshow(np.asarray(depth))
        plt.show()
        im = np.asarray(depth)
        plt.imsave('depth.jpeg',im)
        return False

    def capture_image(vis):
        image = vis.capture_screen_float_buffer()
        plt.imshow(np.asarray(image))
        plt.show()
        im = np.asarray(image)
        im.save('depth','jpeg')
        return False

    key_to_callback = {}
    key_to_callback[ord("K")] = change_background_to_black
    #key_to_callback[ord("R")] = load_render_option
    key_to_callback[ord(",")] = capture_depth
    key_to_callback[ord(".")] = capture_image
    o3d.visualization.draw_geometries_with_key_callbacks([pcd], key_to_callback,width=w,height=h,left=0,top=0)



mesh = o3d.io.read_triangle_mesh(smplpath)
#m = load.Obj()
#mesh=m
#mesh=dict_to_open3dmesh(mesh)
mesh.compute_vertex_normals()
#pcd = mesh.sample_points_poisson_disk(10000)
custom_draw_geometry_with_key_callback(mesh)