平滑 3d 坐标

3d平滑

python 复制代码
import torch
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

class SmoothOperator:
    def smooth(self, vertices):
        # 使用一维平均池化进行平滑
        vertices_smooth = F.avg_pool1d(
            vertices.permute(0, 2, 1),
            kernel_size=3,
            stride=1,
            padding=1
        ).permute(0, 2, 1)
        # 保持顶点的首尾不变,只修改中间部分
        vertices[:, 1:-1] = vertices_smooth[:, 1:-1]
        return vertices

# 创建一些示例数据
t = np.linspace(0, 2 * np.pi, 100)
x = np.sin(t) + np.random.normal(0, 0.1, t.shape)  # 添加一些噪声
y = np.cos(t) + np.random.normal(0, 0.1, t.shape)
z = t
vertices = torch.tensor(np.stack([x, y, z], axis=1), dtype=torch.float32).unsqueeze(0)

# 实例化平滑操作对象并应用平滑
smooth_operator = SmoothOperator()
vertices_smooth = smooth_operator.smooth(vertices.clone())

# 将PyTorch张量转换为NumPy数组以用于绘图
vertices_np = vertices.squeeze(0).numpy()
vertices_smooth_np = vertices_smooth.squeeze(0).numpy()

# 创建图形和3D轴
fig = plt.figure(figsize=(12, 6))

# 绘制原始数据
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot(vertices_np[:, 0], vertices_np[:, 1], vertices_np[:, 2], label='Original', color='b')
ax1.set_title("Original Data")
ax1.legend()

# 绘制平滑后的数据
ax2 = fig.add_subplot(122, projection='3d')
ax2.plot(vertices_smooth_np[:, 0], vertices_smooth_np[:, 1], vertices_smooth_np[:, 2], label='Smoothed', color='r')
ax2.set_title("Smoothed Data")
ax2.legend()

# 显示图形
plt.show()
相关推荐
statistican_ABin几秒前
WHO各国预期寿命影响因素分析与轻量回归预测
大数据·人工智能·python·数据分析·回归
薛定谔的猫-菜鸟程序员1 分钟前
一个 Skill 如何让大模型拥有专业 UI 能力?——ui-ux-pro-max 架构深度拆解与 Skill 设计方法论
人工智能·ui
牧子川3 分钟前
何时拒绝使用工具:Agent 不是万能钥匙
人工智能·大模型·agent·tools·functioncalling
小小测试开发4 分钟前
Promptfoo 实战:用自动化测试框架驯服 LLM 应用的“不确定性“
人工智能
8K超高清4 分钟前
博冠获中国电影电视技术学会科技进步奖
人工智能·科技·算法·安全·接口隔离原则·智能硬件
液态不合群7 分钟前
AI+低代码破局:零售全域数据壁垒的落地新范式
人工智能·低代码·自动化·零售
小码哥哥13 分钟前
企业AI知识库不是套个ChatGPT
人工智能·chatgpt
搞科研的小刘选手14 分钟前
【华南农业大学主办】2026 年人工智能与低空技术国际学术会议(AI-LAT 2026)
人工智能·学术会议·会议推荐·低空技术
承渊政道16 分钟前
从设备数据到AI洞察:时序数据的多模融合实践
数据库·人工智能·性能优化·金仓数据库·多模融合
刘小八21 分钟前
Spring AI Tool Calling 生产化:参数校验、权限控制与超时隔离
java·人工智能·spring