【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'))
相关推荐
甄心爱学习12 小时前
【项目实训(个人4)】
前端·vue.js·python
西兰先森12 小时前
使用Antv G6渲染neo4j知识图谱数据
python·知识图谱·neo4j
weixin_3077791312 小时前
SparkPySetup:基于Python的Windows 11 PySpark环境自动化搭建工具
大数据·开发语言·python·spark
m0_7381207212 小时前
渗透基础知识ctfshow——Web应用安全与防护(完结:第八章)
前端·python·sql·安全·web安全·网络安全
雷帝木木13 小时前
Python 并发编程高级技巧详解:从原理到实践
人工智能·python·深度学习·机器学习
devnullcoffee13 小时前
亚马逊 Movers and Shakers 数据采集实战:用 Python + Scrape API 构建实时榜单监控系统
python·亚马逊数据采集·scrape api·亚马逊数据 api·pangolinfo api·amazon 爬虫工具·实时榜单监控
一个天蝎座 白勺 程序猿13 小时前
AI入门踩坑实录:我换了3种语言才敢说,Python真的是入门唯一选择吗?
开发语言·人工智能·python·ai
Hui_AI72013 小时前
保险条款NLP解析与知识图谱搭建:让AI准确理解保险产品的技术方案
开发语言·人工智能·python·算法·自然语言处理·开源·开源软件
雷帝木木13 小时前
Python Web 框架对比与实战:Django vs Flask vs FastAPI
人工智能·python·深度学习·机器学习
万粉变现经纪人13 小时前
如何解决 pip install jaxlib[cuda] 报错 CUDA 版本与轮子标签不匹配 问题
人工智能·python·深度学习·tensorflow·pandas·scikit-learn·pip