【wrl2stl】WRL文件转STL文件-Python

之前有一篇博客写了Avizo自动化批量导出wrl文件:【Avizo&Python】离散颗粒的分割、网格化与单颗粒批量自动保存wrl文件_avizo python-CSDN博客

还有一篇写了wrl转为xyz格式文件:

Wrl文件转XYZ文件-Python_python 打开wrl三维模型-CSDN博客

在这篇博客中,程序仅读取了顶点信息,面片信息直接被丢弃了,这样会丢失一手的网格信息,并不利于后续的数据分析和结构表征。

前几天在用Meshlab的时候,发现可以读取Wrl文件后直接导出stl/obj格式文件:

那就想有没有像Avizo之类的内置python,结果找到了PyMeshLab的官方库:cnr-isti-vclab/PyMeshLab: The open source mesh processing python library

官方手册:PyMeshLab --- PyMeshLab documentation

安装方式(Python >= 3.7 (64 bit)):

bash 复制代码
pip3 install pymeshlab

然后就直接:

python 复制代码
# wrl2stl_0913.py

import pymeshlab
import os
from tqdm import tqdm

wrls = []
gel = r'C:\DataSet_DOOR\dataset_3Dbubble\1008-gel-reexport'


for root, dirs, files in os.walk(gel):
    for file in tqdm(files):
        if file.endswith(".wrl"):
            wrls = os.path.join(root, file)

            ms = pymeshlab.MeshSet()
            ms.load_new_mesh(wrls)
            wrls = wrls.replace('0802gel', 'mesh')
            wrls = wrls.replace('wrl', 'stl')
            # stl_path = os.path.join(gel, '0802gel1-stl', file.replace('wrl', 'stl'))
            ms.save_current_mesh(wrls)

STL还不可以直接用,存在问题如下:

  1. STL的坐标还是Avizo中的坐标,分布在世界各地,需要进行中心化,将中心坐标移动到原点;
  2. 在一个非常不起眼的角落,存在着一个圆球,每个文件中都存在,尺寸固定。这导致对点坐标计算平均值求的点云中心位置的时候出现很大的偏差,导致中心化失败。这是wrl文件中存在的问题,不一定会遇到,但一定要检查。

罪魁祸首(长得像个病毒😡):

参考以下代码,其中还包括了点云超采样,如果不需要可以删掉。

python 复制代码
import os
import cv2
import numpy as np
import pyvista as pv
import matplotlib.pyplot as plt
from tqdm import tqdm

def upsample_point_cloud(points, num_clusters):
    # 点云上采样,返回mesh的点云数量大于num_clusters
    cloud = pv.PolyData(points)
    sample_spacing = 5
    while(1):
        mesh = cloud.reconstruct_surface(nbr_sz = 10, sample_spacing = sample_spacing)
        # 提取顶点作为新的点云
        new_points = np.asarray(mesh.points)
        if new_points.shape[0] < num_clusters * 1.1:
            sample_spacing *= 0.9
        else:
            break
    return mesh

stl_folder = r"C:\DataSet_DOOR\dataset_3Dbubble\1008-gel-reexport\mesh1-filter"
# stl_folder = r"C:\DataSet_DOOR\dataset_3Dbubble\1008-gel-reexport\mesh2-filter"

# 保存点到文件
progress1 = tqdm(os.listdir(stl_folder), dynamic_ncols=True, desc="Read STL", leave=True)
for stl_file in progress1:
    if stl_file.endswith(".stl"):
        stl_path = os.path.join(stl_folder, stl_file)

        mesh = pv.read(stl_path)
        points = np.array(mesh.points, dtype=float)

        distance_to_origin = np.linalg.norm(points, axis=1)
        # 筛出异常球
        points = points[distance_to_origin >= 100] / 1000000
        points -= np.mean(points, axis=0)
        
        # plotter = pv.Plotter()
        # actor = plotter.add_points(points, color = '#c8c8ff')
        # actor = plotter.add_points(np.array([0, 0, 0]), color = '#f14c36')
        # actor = plotter.show_bounds(grid='back',
        #             location='outer',
        #             ticks='both',
        #             n_xlabels=2,
        #             n_ylabels=2,
        #             n_zlabels=2,
        #             xtitle='X',
        #             ytitle='Y',
        #             ztitle='Z',
        #         )
        # plotter.show()
        
        # mesh = upsample_point_cloud(points, 5000)
        
        # 仅进行一次点云上采样
        cloud = pv.PolyData(points)
        mesh = cloud.reconstruct_surface(nbr_sz = 10, sample_spacing = 0.8)
        mesh.smooth_taubin(n_iter=10, pass_band=5, inplace = True)
        mesh = mesh.fill_holes(100)
        mesh.save(stl_path.replace('filter', 'center'))
相关推荐
不爱吃山楂罐头5 分钟前
第三十三天打卡复习
python·深度学习
weixin_3077791336 分钟前
Neo4j 数据可视化与洞察获取:原理、技术与实践指南
信息可视化·架构·数据分析·neo4j·etl
Dxy123931021639 分钟前
DrissionPage 性能优化实战指南:让网页自动化效率飞升
运维·爬虫·python·性能优化·自动化
蹦蹦跳跳真可爱5891 小时前
Python----目标检测(《SSD: Single Shot MultiBox Detector》论文和SSD的原理与网络结构)
人工智能·python·深度学习·神经网络·目标检测·计算机视觉
蚂蚁数据AntData1 小时前
⼤模型驱动的DeepInsight Copilot在蚂蚁的技术实践
大数据·人工智能·数据分析·copilot·数据库架构
LeonDL1681 小时前
HALCON 深度学习训练 3D 图像的几种方式优缺点
人工智能·python·深度学习·3d·halcon·halcon训练3d图像·深度学习训练3d图像
慧都小妮子2 小时前
跨平台浏览器集成库JxBrowser 支持 Chrome 扩展程序,高效赋能 Java 桌面应用
开发语言·python·api·jxbrowser·chrome 扩展程序
tanyyinyu3 小时前
Python函数参数详解:从位置参数到灵活调用的艺术
运维·开发语言·python
qq_214782613 小时前
mac下通过anaconda安装Python
python·macos·jupyter
junyuz5 小时前
Dify docker内网部署常见问题记录
python·docker