|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import json |
|
|
import os |
|
|
import open3d as o3d |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
mesh = o3d.io.read_triangle_mesh("./bottle.stl") |
|
|
pointcloud = mesh.sample_points_poisson_disk(50000) |
|
|
coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=50.0, origin=[0, 0, 0]) |
|
|
mesh.compute_vertex_normals() |
|
|
mesh_triangles = np.asarray(mesh.triangles) |
|
|
vertex_positions = np.asarray(mesh.vertices) |
|
|
triangle_normals = np.asarray(mesh.triangle_normals) |
|
|
|
|
|
centroid = mesh.get_center() |
|
|
|
|
|
|
|
|
|
|
|
folder = "./dataset" |
|
|
json_path = "ply_files.json" |
|
|
|
|
|
|
|
|
|
|
|
resolutions = { |
|
|
"100": 1.0, |
|
|
"75": 0.8, |
|
|
"50": 0.8, |
|
|
"25": 0.8, |
|
|
"0": 0.8 |
|
|
} |
|
|
|
|
|
|
|
|
try: |
|
|
with open(json_path, "r", encoding="utf-8") as f: |
|
|
categorized_files = json.load(f) |
|
|
except FileNotFoundError: |
|
|
print(f"오류: '{json_path}' 파일을 찾을 수 없습니다. 먼저 파일 분류 코드를 실행해 주세요.") |
|
|
exit() |
|
|
|
|
|
|
|
|
print("=== 데이터 처리 시작 ===") |
|
|
|
|
|
|
|
|
for category, resolution in resolutions.items(): |
|
|
|
|
|
print(f"\n--- [카테고리: {category}, 해상도: {resolution}] 처리 시작 ---") |
|
|
|
|
|
|
|
|
|
|
|
filenames_in_category = categorized_files.get(category, []) |
|
|
|
|
|
if not filenames_in_category: |
|
|
print("처리할 파일이 없습니다.") |
|
|
continue |
|
|
|
|
|
|
|
|
for filename in filenames_in_category: |
|
|
|
|
|
|
|
|
file_path = os.path.join(folder, f"{filename}.ply") |
|
|
|
|
|
print(f" - 파일 처리 중: {file_path} (해상도: {resolution})") |
|
|
|
|
|
|
|
|
filename = filename |
|
|
|
|
|
pcd = o3d.io.read_point_cloud(f"./dataset/{filename}.ply") |
|
|
|
|
|
GT = False |
|
|
if GT==True: |
|
|
mesh = o3d.io.read_triangle_mesh("./bottle2.stl") |
|
|
pointcloud = mesh.sample_points_poisson_disk(50000) |
|
|
coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=50.0, origin=[0, 0, 0]) |
|
|
|
|
|
mesh.compute_vertex_normals() |
|
|
mesh_triangles = np.asarray(mesh.triangles) |
|
|
vertex_positions = np.asarray(mesh.vertices) |
|
|
triangle_normals = np.asarray(mesh.triangle_normals) |
|
|
|
|
|
|
|
|
centroid = mesh.get_center() |
|
|
filtered_triangles = [] |
|
|
for i, triangle in enumerate(mesh_triangles): |
|
|
|
|
|
tri_center = vertex_positions[triangle].mean(axis=0) |
|
|
|
|
|
vec_to_center = tri_center - centroid |
|
|
|
|
|
dot_product = np.dot(triangle_normals[i], vec_to_center) |
|
|
|
|
|
if dot_product > 0: |
|
|
filtered_triangles.append(triangle) |
|
|
|
|
|
outer_mesh = o3d.geometry.TriangleMesh() |
|
|
outer_mesh.vertices = mesh.vertices |
|
|
outer_mesh.triangles = o3d.utility.Vector3iVector(np.array(filtered_triangles)) |
|
|
|
|
|
|
|
|
pcd = outer_mesh.sample_points_uniformly(number_of_points=50000) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pcd_array = np.asarray(pcd.points) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import open3d as o3d |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
if not GT: |
|
|
ply_path = f"./dataset/{filename}.ply" |
|
|
|
|
|
pcd = o3d.io.read_point_cloud(ply_path) |
|
|
print(ply_path) |
|
|
|
|
|
|
|
|
pcd_array = np.asarray(pcd.points) |
|
|
print(pcd_array.shape) |
|
|
|
|
|
coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=50.0, origin=[0, 0, 0]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if GT==False: |
|
|
|
|
|
new_pcd_array = np.unique(pcd_array, axis=0) |
|
|
|
|
|
|
|
|
new_pcd_array = new_pcd_array[new_pcd_array[:, 2] < 1000] |
|
|
|
|
|
|
|
|
new_pcd_array = new_pcd_array[new_pcd_array[:, 1] > -1000] |
|
|
new_pcd_array = new_pcd_array[new_pcd_array[:, 1] < 120] |
|
|
new_pcd_array = new_pcd_array[new_pcd_array[:, 0] > -1000] |
|
|
new_pcd_array = new_pcd_array[new_pcd_array[:, 0] < 1000] |
|
|
|
|
|
|
|
|
print(np.mean(new_pcd_array, axis=0)) |
|
|
|
|
|
new_pcd = o3d.geometry.PointCloud() |
|
|
new_pcd.points = o3d.utility.Vector3dVector(new_pcd_array) |
|
|
|
|
|
theta = np.radians(90) |
|
|
|
|
|
|
|
|
|
|
|
new_pcd_array = np.asarray(new_pcd.points) |
|
|
|
|
|
coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=50.0, origin=[0, 0, 0]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if GT==False: |
|
|
|
|
|
plane_model, inliers = new_pcd.segment_plane(distance_threshold=1, |
|
|
ransac_n=10, |
|
|
num_iterations=1000) |
|
|
[a, b, c, d] = plane_model |
|
|
print(f"Plane equation: {a:.2f}x + {b:.2f}y + {c:.2f}z + {d:.2f} = 0") |
|
|
|
|
|
|
|
|
|
|
|
inlier_cloud = new_pcd.select_by_index(inliers) |
|
|
inlier_cloud.paint_uniform_color([1.0, 0, 1.0]) |
|
|
outlier_cloud = new_pcd.select_by_index(inliers, invert=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new_pcd = outlier_cloud |
|
|
|
|
|
new_pcd_array = np.asarray(new_pcd.points) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHECK_PERTURB = GT |
|
|
|
|
|
def random_rotation_matrix(): |
|
|
""" |
|
|
Generate a random 3x3 rotation matrix (SO(3) matrix). |
|
|
|
|
|
Uses the method described by James Arvo in "Fast Random Rotation Matrices" (1992): |
|
|
1. Generate a random unit vector for rotation axis |
|
|
2. Generate a random angle |
|
|
3. Create rotation matrix using Rodriguez rotation formula |
|
|
|
|
|
Returns: |
|
|
numpy.ndarray: A 3x3 random rotation matrix |
|
|
""" |
|
|
|
|
|
|
|
|
theta = 0 |
|
|
|
|
|
|
|
|
|
|
|
axis = np.array([ |
|
|
1, |
|
|
0, |
|
|
0, |
|
|
]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
axis = axis / np.linalg.norm(axis) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
K = np.array([ |
|
|
[0, -axis[2], axis[1]], |
|
|
[axis[2], 0, -axis[0]], |
|
|
[-axis[1], axis[0], 0] |
|
|
]) |
|
|
|
|
|
|
|
|
R = (np.eye(3) + |
|
|
np.sin(theta) * K + |
|
|
(1 - np.cos(theta)) * np.dot(K, K)) |
|
|
|
|
|
return R |
|
|
|
|
|
if CHECK_PERTURB: |
|
|
R_pert = random_rotation_matrix() |
|
|
print(R_pert) |
|
|
t_pert = np.array([ |
|
|
0, |
|
|
0, |
|
|
0 |
|
|
]) |
|
|
|
|
|
|
|
|
perturbed_pcd_array = np.dot(R_pert, pcd_array.T).T + t_pert.T |
|
|
|
|
|
|
|
|
perturbed_pcd = o3d.geometry.PointCloud() |
|
|
perturbed_pcd.points = o3d.utility.Vector3dVector(perturbed_pcd_array) |
|
|
coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=50.0, origin=[0, 0, 0]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHECK_PERTURB = not GT |
|
|
|
|
|
|
|
|
if CHECK_PERTURB: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
perturbed_pcd_array = new_pcd_array |
|
|
perturbed_pcd = o3d.geometry.PointCloud() |
|
|
perturbed_pcd.points = o3d.utility.Vector3dVector(perturbed_pcd_array) |
|
|
|
|
|
|
|
|
now_centeroid = perturbed_pcd.get_center() |
|
|
perturbed_pcd.translate(centroid, relative=False) |
|
|
|
|
|
|
|
|
|
|
|
translation_vector = centroid - now_centeroid |
|
|
|
|
|
np.savetxt(f"./centroid/{filename}.txt",translation_vector) |
|
|
|
|
|
|
|
|
|
|
|
perturbed_pcd_array = np.asarray(perturbed_pcd.points) |
|
|
coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=50.0, origin=[0, 0, 0]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def write_ply(points, output_path): |
|
|
""" |
|
|
Write points and parameters to a PLY file |
|
|
|
|
|
Parameters: |
|
|
points: numpy array of shape (N, 3) containing point coordinates |
|
|
output_path: path to save the PLY file |
|
|
""" |
|
|
with open(output_path, 'w') as f: |
|
|
|
|
|
f.write("ply\n") |
|
|
f.write("format ascii 1.0\n") |
|
|
|
|
|
|
|
|
f.write(f"element vertex {len(points)}\n") |
|
|
f.write("property float x\n") |
|
|
f.write("property float y\n") |
|
|
f.write("property float z\n") |
|
|
|
|
|
|
|
|
f.write("element camera 1\n") |
|
|
f.write("property float view_px\n") |
|
|
f.write("property float view_py\n") |
|
|
f.write("property float view_pz\n") |
|
|
f.write("property float x_axisx\n") |
|
|
f.write("property float x_axisy\n") |
|
|
f.write("property float x_axisz\n") |
|
|
f.write("property float y_axisx\n") |
|
|
f.write("property float y_axisy\n") |
|
|
f.write("property float y_axisz\n") |
|
|
f.write("property float z_axisx\n") |
|
|
f.write("property float z_axisy\n") |
|
|
f.write("property float z_axisz\n") |
|
|
|
|
|
|
|
|
f.write("element phoxi_frame_params 1\n") |
|
|
f.write("property uint32 frame_width\n") |
|
|
f.write("property uint32 frame_height\n") |
|
|
f.write("property uint32 frame_index\n") |
|
|
f.write("property float frame_start_time\n") |
|
|
f.write("property float frame_duration\n") |
|
|
f.write("property float frame_computation_duration\n") |
|
|
f.write("property float frame_transfer_duration\n") |
|
|
f.write("property int32 total_scan_count\n") |
|
|
|
|
|
|
|
|
f.write("element camera_matrix 1\n") |
|
|
for i in range(9): |
|
|
f.write(f"property float cm{i}\n") |
|
|
|
|
|
|
|
|
f.write("element distortion_matrix 1\n") |
|
|
for i in range(14): |
|
|
f.write(f"property float dm{i}\n") |
|
|
|
|
|
|
|
|
f.write("element camera_resolution 1\n") |
|
|
f.write("property float width\n") |
|
|
f.write("property float height\n") |
|
|
|
|
|
|
|
|
f.write("element frame_binning 1\n") |
|
|
f.write("property float horizontal\n") |
|
|
f.write("property float vertical\n") |
|
|
|
|
|
|
|
|
f.write("end_header\n") |
|
|
|
|
|
|
|
|
for point in points: |
|
|
f.write(f"{point[0]} {point[1]} {point[2]}\n") |
|
|
|
|
|
print(True) |
|
|
|
|
|
if GT: write_ply(perturbed_pcd_array, f"gt_filtered.ply") |
|
|
else: |
|
|
write_ply(perturbed_pcd_array, f"./noisy_result/noisy_filtered_{filename}.ply") |
|
|
write_ply(new_pcd_array,f"./noisy_raw/noisy_filtered_{filename}.ply") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pcd = o3d.io.read_point_cloud("./gt_filtered.ply") |
|
|
|
|
|
|
|
|
o3d.io.write_point_cloud("./initialize_pcdfile/gt_filtered.pcd", pcd) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("PLY 파일이 PCD 파일로 성공적으로 변환되었습니다.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pcd = o3d.io.read_point_cloud(f"./noisy_result/noisy_filtered_{filename}.ply") |
|
|
|
|
|
|
|
|
o3d.io.write_point_cloud(f"./initialize_pcdfile/first_{filename}.pcd", pcd) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("PLY 파일이 PCD 파일로 성공적으로 변환되었습니다.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|