【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'))
相关推荐
电子_咸鱼19 分钟前
动态规划经典题解:单词拆分(LeetCode 139)
java·数据结构·python·算法·leetcode·线性回归·动态规划
风轻扬7778 小时前
SQLAlchemy2.0使用
python·sqlalchemy·orm框架
潘帕斯的雄鹰8 小时前
直观理解注意力机制
python·transformer·注意力机制·自注意力机制
打酱油程序员8 小时前
Python数据挖掘详细学习指南
python
天天进步201510 小时前
Python全栈项目:结合Puppeteer和AI模型操作浏览器
开发语言·人工智能·python
闲人编程10 小时前
用Python识别图片中的文字(Tesseract OCR)
开发语言·python·ocr·识图·codecapsule
盘古开天166611 小时前
从零开始:如何搭建你的第一个简单的Flask网站
后端·python·flask
点云SLAM11 小时前
方差的迭代计算公式
大数据·深度学习·数据分析·概率论·数学原理·概论率
二进制星轨11 小时前
Transofrmer架构详解与PyTorch实现(附代码讲解)
人工智能·pytorch·python
生而为虫12 小时前
02.第一个Python程序
开发语言·python