news 2026/2/5 6:36:19

GenerateModelsFromLabels体积数据转表面模型并保存

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
GenerateModelsFromLabels体积数据转表面模型并保存

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①vtkImageAccumulate三维体素数据直方图统计器,②vtkMaskFields数据字段的过滤器


二:代码及注释

import vtkmodules.vtkInteractionStyle import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkIOImage import vtkMetaImageReader from vtkmodules.vtkImagingStatistics import vtkImageAccumulate from vtkmodules.vtkFiltersGeneral import vtkDiscreteFlyingEdges3D from vtkmodules.vtkFiltersCore import vtkWindowedSincPolyDataFilter, vtkThreshold, vtkMaskFields from vtkmodules.vtkFiltersGeometry import vtkGeometryFilter from vtkmodules.vtkCommonDataModel import ( vtkDataObject, vtkDataSetAttributes ) from vtkmodules.vtkIOXML import vtkXMLPolyDataWriter import os import sys def main(): file_name = "Data/Frog/frogtissue.mhd" start_label = 1 end_label = 29 reader = vtkMetaImageReader() reader.SetFileName(file_name) reader.Update() file_prefix = 'Label' smoothing_iterations = 15 pass_band = 0.001 feature_angle = 120.0 # 表面重建,最后一个表示要重建多少个表面 discrete_cubes = vtkDiscreteFlyingEdges3D() discrete_cubes.SetInputConnection(reader.GetOutputPort()) discrete_cubes.GenerateValues(end_label - start_label + 1, start_label, end_label) """ vtkImageAccumulate 当于一个 3D 直方图(histogram)计算器 用于计算图像(或体数据)中像素 / 体素的统计信息,包括直方图、平均值、最小值、最大值 计算所有离散标签的出现频率(即生成直方图) vtkImageAccumulate 的输出是一个特殊的 vtkImageData """ histogram = vtkImageAccumulate() histogram.SetInputConnection(reader.GetOutputPort()) """ SetComponentExtent 定义了标签值的范围 0到end_label表示直方图覆盖从标签0到最大标签纸所有的数值 后面的0是将Y和Z维度设置为单一切片,因为直方图本质上是一个一维的频率计数 """ histogram.SetComponentExtent(0, end_label, 0, 0, 0, 0) """ SetComponentOrigin设置直方图的起始值从 0 开始计数(即从标签 0 开始) """ histogram.SetComponentOrigin(0, 0, 0) """ SetComponentSpacing 设置直方图的“体素”间隔为 1。 由于处理的是离散整数标签,这个间隔为 1 确保了每个整数标签值都有一个对应的“箱子”(bin) """ histogram.SetComponentSpacing(1, 1, 1) histogram.Update() # mesh的平滑 smoother = vtkWindowedSincPolyDataFilter() smoother.SetInputConnection(discrete_cubes.GetOutputPort()) smoother.BoundarySmoothingOff() smoother.FeatureEdgeSmoothingOff() """ SetFeatureAngle 定义大于该角度(120.0 度)的边为“特征边”。由于 FeatureEdgeSmoothingOff,这些边在平滑时会得到保护 """ smoother.SetFeatureAngle(feature_angle) """ SetPassBand 它控制了哪些频率(即几何上的细节)会被保留或平滑。 pass_band = 0.001 是一个非常低的值,表示它会平滑掉大部分的高频细节(即锯齿和噪声),只保留低频(整体形状) """ smoother.SetPassBand(pass_band) smoother.SetNumberOfIterations(smoothing_iterations) smoother.NonManifoldSmoothingOn() """ NormalizeCoordinatesOn 启用在平滑处理前对坐标进行归一化。这有助于确保平滑效果独立于模型的绝对 """ smoother.NormalizeCoordinatesOn() smoother.Update() selector = vtkThreshold() selector.SetInputConnection(smoother.GetOutputPort()) selector.SetInputArrayToProcess(0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes.SCALARS) """ vtkMaskFields 是一个 数据字段过滤器 主要作用是:控制数据集中哪些属性(如标量、向量、法向量、纹理坐标等)被保留或删除 VTK 的数据集中,数据可能挂在两个层级上: PointData:每个点上存的属性(如温度、速度向量等) CellData:每个单元(网格单元)上的属性(如材料编号、区域 ID 等) CopyAttributeOff 表示丢弃某种属性数据 CopyAttributeOn 表示保留某种属性数据 """ scalars_off = vtkMaskFields() scalars_off.SetInputConnection(selector.GetOutputPort()) scalars_off.CopyAttributeOff(vtkMaskFields().POINT_DATA, vtkDataSetAttributes().SCALARS) scalars_off.CopyAttributeOff(vtkMaskFields().CELL_DATA, vtkDataSetAttributes().SCALARS) geometry = vtkGeometryFilter() geometry.SetInputConnection(scalars_off.GetOutputPort()) writer = vtkXMLPolyDataWriter() for i in range(start_label, end_label + 1): # see if the label exists, if not skip it frequency = histogram.GetOutput().GetPointData().GetScalars().GetTuple1(i) if frequency == 0.0: continue # select the cells for a given label selector.SetLowerThreshold(i) selector.SetUpperThreshold(i) writer.SetInputConnection(selector.GetOutputPort()) # output the polydata output_fn = '{:s}{:d}.vtp'.format(file_prefix, i) print('{:s} writing {:s}'.format(os.path.basename(sys.argv[0]), output_fn)) writer.SetFileName(output_fn) writer.Write() if __name__ == '__main__': main()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/31 17:36:09

Charticulator免费图表设计工具:从零开始构建专业数据可视化

Charticulator免费图表设计工具:从零开始构建专业数据可视化 【免费下载链接】charticulator Interactive Layout-Aware Construction of Bespoke Charts 项目地址: https://gitcode.com/gh_mirrors/ch/charticulator 想要快速创建个性化图表却苦于不会编程&…

作者头像 李华
网站建设 2026/2/3 22:05:04

宏智树AI如何将课程论文锻造成你的“第一份研究作品”

深夜的图书馆,咖啡已凉,屏幕上的课程论文仅有三行标题。这曾是大三学生李明的常态,直到他第一次使用宏智树AI,三十分钟后,一个结构完整、文献扎实、逻辑自洽的论文大纲清晰地呈现在眼前。作为教育博主,我们…

作者头像 李华
网站建设 2026/1/29 18:35:42

Dify平台内建日志监控系统,便于运维排查

Dify平台内建日志监控系统,便于运维排查 在AI应用加速落地的今天,企业对智能客服、知识问答、自动化内容生成等系统的稳定性要求越来越高。然而,当一个RAG流程突然返回“我不知道”,或者Agent在执行到第三步时卡住无响应&#xff…

作者头像 李华
网站建设 2026/2/1 23:51:21

RX-Explorer终极指南:重新定义Windows文件管理体验

RX-Explorer终极指南:重新定义Windows文件管理体验 【免费下载链接】RX-Explorer 一款优雅的UWP文件管理器 | An elegant UWP Explorer 项目地址: https://gitcode.com/gh_mirrors/rx/RX-Explorer 你是否曾经在几十个窗口中迷失,只为找到一个重要…

作者头像 李华
网站建设 2026/2/4 10:05:44

Charticulator数据可视化工具完全攻略:无需编程的智能图表设计

Charticulator数据可视化工具完全攻略:无需编程的智能图表设计 【免费下载链接】charticulator Interactive Layout-Aware Construction of Bespoke Charts 项目地址: https://gitcode.com/gh_mirrors/ch/charticulator 还在为制作专业图表而头疼吗&#xff…

作者头像 李华
网站建设 2026/1/30 13:10:12

TeslaMate自建监控平台:从数据采集到智能决策的完整指南

TeslaMate自建监控平台:从数据采集到智能决策的完整指南 【免费下载链接】teslamate 项目地址: https://gitcode.com/gh_mirrors/tes/teslamate 在当今数据驱动的时代,特斯拉车主们是否真正了解自己爱车的性能表现?TeslaMate作为开源…

作者头像 李华