Created
September 1, 2021 11:19
-
-
Save Totoro97/43664cfc28110a469d88a158af040014 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 as cv | |
import os | |
from glob import glob | |
from scipy.io import loadmat | |
import trimesh | |
def clean_points_by_mask(points, scan): | |
cameras = np.load('/home/aska/Projects/NeuS/public_data/dtu_scan{}/cameras_sphere.npz'.format(scan)) | |
mask_lis = sorted(glob('/home/aska/Projects/NeuS/public_data/dtu_scan{}/mask/*.png'.format(scan))) | |
n_images = 49 if scan < 83 else 64 | |
inside_mask = np.ones(len(points)) > 0.5 | |
for i in range(n_images): | |
P = cameras['world_mat_{}'.format(i)] | |
pts_image = np.matmul(P[None, :3, :3], points[:, :, None]).squeeze() + P[None, :3, 3] | |
pts_image = pts_image / pts_image[:, 2:] | |
pts_image = np.round(pts_image).astype(np.int32) + 1 | |
mask_image = cv.imread(mask_lis[i]) | |
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (101, 101)) | |
mask_image = cv.dilate(mask_image, kernel, iterations=1) | |
mask_image = (mask_image[:, :, 0] > 128) | |
mask_image = np.concatenate([np.ones([1, 1600]), mask_image, np.ones([1, 1600])], axis=0) | |
mask_image = np.concatenate([np.ones([1202, 1]), mask_image, np.ones([1202, 1])], axis=1) | |
curr_mask = mask_image[(pts_image[:, 1].clip(0, 1201), pts_image[:, 0].clip(0, 1601))] | |
inside_mask &= curr_mask.astype(bool) | |
return inside_mask | |
scans = [ 24, 37, 40, 55, 63, 65, 69, 83, 97, 105, 106, 110, 114, 118, 122 ] | |
old_dir = '/run/media/aska/Samsung_T5/Data/DTU/SampleSet/MVS_Data/Surfaces/womask_sphere/' | |
new_dir = '/run/media/aska/Samsung_T5/Data/DTU/SampleSet/MVS_Data/Surfaces/womask_sphere_clean/' | |
for scan in scans: | |
print(scan) | |
old_mesh = trimesh.load(os.path.join(old_dir, '{:0>3d}.ply'.format(scan))) | |
old_vertices = old_mesh.vertices[:] | |
old_faces = old_mesh.faces[:] | |
mask = clean_points_by_mask(old_vertices, scan) | |
indexes = np.ones(len(old_vertices)) * -1 | |
indexes = indexes.astype(np.long) | |
indexes[np.where(mask)] = np.arange(len(np.where(mask)[0])) | |
faces_mask = mask[old_faces[:, 0]] & mask[old_faces[:, 1]] & mask[old_faces[:, 2]] | |
new_faces = old_faces[np.where(faces_mask)] | |
new_faces[:, 0] = indexes[new_faces[:, 0]] | |
new_faces[:, 1] = indexes[new_faces[:, 1]] | |
new_faces[:, 2] = indexes[new_faces[:, 2]] | |
new_vertices = old_vertices[np.where(mask)] | |
new_mesh = trimesh.Trimesh(new_vertices, new_faces) | |
meshes = new_mesh.split(only_watertight=False) | |
new_mesh = meshes[np.argmax([len(mesh.faces) for mesh in meshes])] | |
new_mesh.export(os.path.join(new_dir, '{:0>3d}.ply'.format(scan))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment