-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdecimate_labels.py
More file actions
73 lines (56 loc) · 2.85 KB
/
decimate_labels.py
File metadata and controls
73 lines (56 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Match the labels (body parts) of each original mesh to its equivalent in decimated mesh
Use 1-NN to match each face of the mesh
By inverting dirLabels and dirLabelsDecimated, it should be possible to do the inverse
mapping
Just indicate the parameters and target directories.
Use python 3 (but should be working with python 2)
"""
import os, sys
import numpy as np
import utils
# Set directories
root = os.getcwd()
dirMesh = root + '/../MeshsegBenchmark-1.0/data/off/'
dirMeshDecimated = root + '/../MeshsegBenchmark-1.0/data/off_decimated/' # Should contain the sames number and filename as above
dirLabels = root + '/../MeshsegBenchmark-1.0/data/seg/Bench/'
dirLabelsDecimated = root + '/../MeshsegBenchmark-1.0/data/seg/Bench_decimated/'
def main():
# Global check
assert(os.path.exists(dirMesh))
assert(os.path.exists(dirMeshDecimated))
assert(os.path.exists(dirLabels))
assert(os.path.exists(dirLabelsDecimated))
# For each mesh
filesList = utils.sortFiles(os.listdir(dirMesh))
for filename in filesList:
if filename.endswith('.off'): # Candidate
print('Try matching ', filename)
idMesh = filename.split('.')[0]
# Loading the original and reduced meshes and compute the center of each face
vertices, faces = utils.extractMesh(dirMesh + filename)
verticesDecimated, facesDecimated = utils.extractMesh(dirMeshDecimated + filename)
pointCloud = utils.meshToPointCloud(vertices, faces)
pointCloudDecimated = utils.meshToPointCloud(verticesDecimated, facesDecimated)
# Extract the label list
labelList = utils.loadLabelList(dirLabels + idMesh + '.seg')
labelListDecimated = []
# Use K-NN on each face of the decimated mesh
for pointDecimated in pointCloudDecimated:
# Search the closest point index in the original point cloud
minIndex = 0
minDist = -1 # Initialize
for i, point in enumerate(pointCloud):
distance = np.linalg.norm(pointDecimated - point)
if distance < minDist or minDist == -1:
minDist = distance
minIndex = i
labelListDecimated.append(labelList[minIndex])
if len(labelListDecimated) % 100 == 0:
print(len(labelListDecimated) / len(pointCloudDecimated) * 100, '%')
# Save the values of the labels of the closest match indexes
saveName = dirLabelsDecimated + idMesh + '.seg'
print('Saving ', saveName)
utils.saveLabelList(labelListDecimated, saveName)
if __name__ == "__main__":
main()