Skip to content

Commit 58b26f1

Browse files
committed
第一次提交
0 parents  commit 58b26f1

14 files changed

+527
-0
lines changed

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Semantic_rotation.iml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Pipeline/Run_Sematic_R.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
from Tran_and_scaling.Reput_txt import translate_and_scale_point_cloud
3+
from Vedio.Semantic_vedio import generate_rotating_point_cloud_video_from_txt
4+
5+
# 使用示例
6+
input_txt_dir = r"C:\Users\Administrator\Desktop\mp4\other_input\input"
7+
output_dir = r"C:\Users\Administrator\Desktop\mp4\other_input\combine_txt"
8+
output_mp4_dir = rf"C:\Users\Administrator\Desktop\mp4\other_output"
9+
10+
OVERWRITE = False
11+
12+
# 创建输出目录(如果不存在)
13+
os.makedirs(output_dir, exist_ok=True)
14+
os.makedirs(output_mp4_dir, exist_ok=True)
15+
16+
for filename in os.listdir(input_txt_dir):
17+
if filename.endswith(".txt"):
18+
# 构建输入文件路径
19+
input_file = os.path.join(input_txt_dir, filename)
20+
# 调用函数处理点云文件
21+
translate_and_scale_point_cloud(input_file, output_dir, OVERWRITE)
22+
# 构建处理后的txt文件路径
23+
processed_txt = os.path.join(output_dir, filename)
24+
# 调用函数生成旋转点云视频
25+
generate_rotating_point_cloud_video_from_txt(processed_txt, output_mp4_dir, OVERWRITE)
26+
27+

README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Semantic_Rotation
2+
3+
4+
5+
### ------------------------------------------使用**Semantic_rotation**可以展示语义点云------------------------------------------
6+
7+
8+
9+
对原始点云进行缩放重构,确保能够将点云放进open3d的视窗下
10+
11+
```
12+
input_txt_dir = r"C:\Users\Administrator\Desktop\mp4\other_input\input"
13+
output_dir = r"C:\Users\Administrator\Desktop\mp4\other_input\combine_txt"
14+
output_mp4_dir = rf"C:\Users\Administrator\Desktop\mp4\other_output"
15+
```
16+
17+
`input_txt_dir`为输入点云的目录;
18+
19+
`output_dir`为重构点云的输出目录;
20+
21+
`output_mp4_dir`为mp4视频的输出目录。
22+
23+
24+
25+
## Installation:
26+
27+
### ffmpeg:
28+
29+
​ 需要在命令行中能识别到ffmpeg即可。
30+
31+
32+
33+
### conda 安装
34+
35+
##### 新建conda环境
36+
37+
```
38+
conda activate -n semantic_rotation python==3.8 -y
39+
pip install open3d==0.17.0
40+
pip install opencv-python==4.10.0.82
41+
git clone
42+
cd semantic_rotation
43+
pip install . -e
44+
```
45+
46+
##### 从已有环境安装(需要注意open3d版本的关系,报错可以新建conda环境)
47+
48+
```
49+
conda activate your_env
50+
gir clone
51+
cd semantic_rotation
52+
pip install . -e
53+
```
54+
55+
56+
57+
58+
59+
## 输入点云要求:
60+
61+
1. txt格式;
62+
2. 间隔符为空格;
63+
3. 前七列是x y z r g b 和语义信息。
64+
65+
66+
67+
## 备注:
68+
69+
#输出的视频时长为14s,不支持自定义调节
70+
71+
#在第七秒进行过渡,过渡时长为0.5s
72+
73+
#设置y轴为点云旋转轴
74+
75+
76+

Tran_and_scaling/Reput_txt.py

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import os
2+
import numpy as np
3+
4+
def translate_and_scale_point_cloud(input_file, output_dir, overwrite=False):
5+
# 获取输入文件名(不包括扩展名)
6+
input_filename = os.path.splitext(os.path.basename(input_file))[0]
7+
8+
# 构建输出文件路径
9+
output_file = os.path.join(output_dir, f"{input_filename}.txt")
10+
if overwrite == False:
11+
# 检查输出文件是否存在
12+
if os.path.exists(output_file):
13+
print(f"输出文件 {output_file} 已完成平移和旋转,无需重复执行")
14+
return
15+
# 读取点云文件的前7列数据
16+
point_cloud = np.genfromtxt(input_file, skip_header=1, usecols=range(7))
17+
18+
# 获取xyz坐标
19+
xyz = point_cloud[:, :3]
20+
21+
# 计算xz的平均值
22+
mean_x, _, mean_z = np.mean(xyz, axis=0)
23+
24+
# 平移点云
25+
xyz[:, 0] -= mean_x
26+
xyz[:, 2] -= mean_z
27+
28+
# 计算y的范围
29+
min_y, max_y = np.min(xyz[:, 1]), np.max(xyz[:, 1])
30+
y_range = max_y - min_y
31+
32+
# 如果y的范围大于0.75,则缩放点云
33+
if y_range > 1:
34+
scale_factor = 1 / y_range
35+
xyz *= scale_factor
36+
37+
# 计算y的平均值
38+
mean_y = np.mean(xyz[:, 1])
39+
40+
# 平移y坐标,使其均值落在-0.1
41+
xyz[:, 1] -= mean_y - (-0.1)
42+
43+
# 更新点云数据的前3列
44+
point_cloud[:, :3] = xyz
45+
46+
# 保存处理后的7列点云数据到输出文件
47+
np.savetxt(output_file, point_cloud, fmt='%.6f')
48+
49+
# 正方体边长
50+
length = 1
51+
52+
# 正方体中心点坐标
53+
center = np.array([0, 0, 0])
54+
55+
# 生成正方体顶点坐标
56+
vertices = np.array([
57+
[-length / 2, -length / 2, -length / 2],
58+
[length / 2, -length / 2, -length / 2],
59+
[length / 2, length / 2, -length / 2],
60+
[-length / 2, length / 2, -length / 2],
61+
[-length / 2, -length / 2, length / 2],
62+
[length / 2, -length / 2, length / 2],
63+
[length / 2, length / 2, length / 2],
64+
[-length / 2, length / 2, length / 2]
65+
]) + center
66+
67+
# 定义正方体边的顶点索引
68+
edges = [
69+
(0, 1), (1, 2), (2, 3), (3, 0), # 底面边
70+
(4, 5), (5, 6), (6, 7), (7, 4), # 顶面边
71+
(0, 4), (1, 5), (2, 6), (3, 7) # 侧面边
72+
]
73+
74+
# 创建正方体点云数据
75+
cube_point_cloud = []
76+
77+
for edge in edges:
78+
start, end = vertices[edge[0]], vertices[edge[1]]
79+
80+
# 计算边上的点的数量(这里设置为边长的10倍)
81+
num_points = int(length * 10)
82+
83+
# 生成边上的点坐标
84+
for i in range(num_points + 1):
85+
t = i / num_points
86+
x = start[0] + t * (end[0] - start[0])
87+
y = start[1] + t * (end[1] - start[1])
88+
z = start[2] + t * (end[2] - start[2])
89+
90+
# 设置为白色
91+
r = g = b = 255
92+
cube_point_cloud.append([x, y, z, r, g, b, 2])
93+
94+
# 将正方体点云数据转换为NumPy数组
95+
cube_point_cloud = np.array(cube_point_cloud)
96+
97+
# 组合原始点云和正方体点云
98+
combined_point_cloud = np.vstack((point_cloud, cube_point_cloud))
99+
100+
# 创建输出目录(如果不存在)
101+
os.makedirs(output_dir, exist_ok=True)
102+
103+
# 获取输入文件名(不包括扩展名)
104+
input_filename = os.path.splitext(os.path.basename(input_file))[0]
105+
106+
# 构建输出文件路径
107+
output_file = os.path.join(output_dir, f"{input_filename}.txt")
108+
109+
# 保存结果到新的txt文件
110+
header = 'x y z r g b object_id'
111+
fmt = ['%.6f'] * 3 + ['%d'] * 4 # 设置保存格式
112+
np.savetxt(output_file, combined_point_cloud, fmt=fmt, delimiter=' ', header=header, comments='')
113+

Tran_and_scaling/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)