当前位置: 首页 > news >正文

PolyDataContourToImageData 3D集合图像转换成等效3D二值图像

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①三维图形转换为3D体素数据


二:代码及注释

import math import vtkmodules.vtkRenderingOpenGL2 import vtkmodules.vtkInteractionStyle from vtkmodules.vtkFiltersSources import vtkSphereSource from vtkmodules.vtkFiltersCore import vtkCutter, vtkStripper from vtkmodules.vtkCommonDataModel import vtkImageData, vtkPlane from vtkmodules.vtkIOXML import vtkXMLPolyDataWriter from vtkmodules.vtkCommonCore import VTK_UNSIGNED_CHAR from vtkmodules.vtkFiltersModeling import vtkLinearExtrusionFilter from vtkmodules.vtkImagingStencil import vtkImageStencil, vtkPolyDataToImageStencil from vtkmodules.vtkIOImage import vtkMetaImageWriter, vtkPNGWriter def main(): # 创建一个3D图形 sphereSource = vtkSphereSource() sphereSource.SetPhiResolution(30) sphereSource.SetThetaResolution(30) sphereSource.SetCenter(40, 40, 40) sphereSource.SetRadius(20) # 切割数据集的"刀" # 首先先定义这个刀是什么,需要使用隐函数定义 cutPlane = vtkPlane() cutPlane.SetOrigin(sphereSource.GetCenter()) cutPlane.SetNormal(0, 0, 1) circleCutter = vtkCutter() circleCutter.SetInputConnection(sphereSource.GetOutputPort()) circleCutter.SetCutFunction(cutPlane) stripper = vtkStripper() stripper.SetInputConnection(circleCutter.GetOutputPort()) stripper.Update() circle = stripper.GetOutput() # 写入vtp文件 polyDataWriter = vtkXMLPolyDataWriter() polyDataWriter.SetInputData(circle) polyDataWriter.SetFileName("circle.vtp") polyDataWriter.SetCompressorTypeToNone() # 不适用任何压缩 polyDataWriter.SetDataModeToAscii() # 设置输出文件中的数据以ASCII(文本)格式存储 polyDataWriter.Write() # 准备一个体素数据 whiteImage = vtkImageData() bounds = [0] * 6 circle.GetBounds(bounds) spacing = [0.5, 0.5, 0.5] whiteImage.SetSpacing(spacing) # 计算这个体素数据的维度 dim = [0] * 3 for i in range(3): dim[i] = int(math.ceil((bounds[i * 2 + 1] - bounds[i * 2]) / spacing[i])) + 1 if dim[i] < 1: dim[i] = 1 whiteImage.SetDimensions(dim) # 算是设置了图像体素的数量 # SetExtent 设置了图像的索引范围 确保了生成的 3D 图像的体素网格是最小且精确的,既不浪费内存,又能完整地覆盖输入的几何体 whiteImage.SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1) origin_shift = 5 * 0.5 origin = [0] * 3 origin[0] = bounds[0] - origin_shift origin[1] = bounds[2] - origin_shift origin[2] = bounds[4] - origin_shift # 上述添加origin_shift是为了vtkImageData能够完全包裹住mesh whiteImage.SetOrigin(origin) # AllocateScalars # 为图像的体素数据分配内存。VTK_UNSIGNED_CHAR(无符号字符,范围 0-255),1 表示每个体素有一个分量(即灰度值) whiteImage.AllocateScalars(VTK_UNSIGNED_CHAR, 1) # 将 整个 3D 图像(体素网格)预先填充为前景值(即白色) # inval = 255 outval = 0 # count = whiteImage.GetNumberOfPoints() # for i in range(count): # whiteImage.GetPointData().GetScalars().SetTuple1(i, inval) # 上述for循环的简便替代方法 whiteImage.GetPointData().GetScalars().Fill(255) extrude = vtkLinearExtrusionFilter() extrude.SetInputData(circle) extrude.SetScaleFactor(1.0) # 设置挤压的比例因子 extrude.SetExtrusionTypeToVectorExtrusion() # 设置为向量挤压,而非几何体表面的法线定义 extrude.SetVector(0, 0, 1) # 设置挤压的向量 extrude.Update() pol2stenc = vtkPolyDataToImageStencil() pol2stenc.SetInputConnection(extrude.GetOutputPort()) pol2stenc.SetTolerance(0) # 帮助过滤垂直面 pol2stenc.SetOutputOrigin(origin) pol2stenc.SetOutputSpacing(spacing) pol2stenc.SetOutputWholeExtent(whiteImage.GetExtent()) pol2stenc.Update() imgstenc = vtkImageStencil() """ tkImageStencil 是另一个关键过滤器,它接收一个图像作为输入,并根据一个模板(Stencil)修改该图像的像素值。 """ imgstenc.SetInputData(whiteImage) imgstenc.SetStencilConnection(pol2stenc.GetOutputPort()) imgstenc.ReverseStencilOff() imgstenc.SetBackgroundValue(outval) imgstenc.Update() imageWriter = vtkMetaImageWriter() imageWriter.SetFileName('labelImage.mhd') imageWriter.SetInputConnection(imgstenc.GetOutputPort()) imageWriter.Write() imageWriter = vtkPNGWriter() imageWriter.SetFileName('labelImage.png') imageWriter.SetInputConnection(imgstenc.GetOutputPort()) imageWriter.Write() if __name__ == '__main__': main()
http://www.cnnetsun.cn/news/168173.html

相关文章:

  • Linly-Talker支持模型性能 profiling,精准定位瓶颈
  • Linly-Talker如何处理中英文混读?语音识别适配策略
  • LLM 的思考方式
  • 【虚拟同步机控制建模】三相虚拟同步发电机双环控制(Simulink仿真实现)
  • 万字长文!关于AI绘图,一篇超详细的总结发布
  • 数字人会议主持:Linly-Talker在远程会议中的创新应用
  • 【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)
  • 用Linly-Talker做企业宣传片?品牌传播的AI新路径
  • Electerm(桌面终端模拟软件)
  • Thinkphp和Laravel基于Vue的黄山旅游景区门票预订网站的设计与实现_3h38caai
  • Thinkphp和Laravel基于大数据架构的大学生求职招聘就业岗位推荐系统的设计与实现_67911t4j
  • AI工具实战测评技术
  • 创意AI应用开发大赛技术
  • 全球股市估值与海洋微生物能源技术的关系
  • 基于python的同城宠物照看数据可视化分析系统的设计与实现_34cl0po8--论文
  • 【路径规划】基于RRT快速探索随机树的图像地图路径规划实现3附matlab代码
  • Quartz 工作模式,是“堵塞排队”还是“并发狂奔”?
  • 【FFNN负荷预测】基于人工神经网络的空压机负荷预测(Matlab代码实现)
  • 【C2000系列DSP的反向灌电流】为什么热插拔的时候I2C总线电平会被拉低?
  • Gemini Inc靶场练习(包含suid提权,文件包含漏洞,ssh免密登录)
  • 软件解耦与扩展:插件式开发方式(基于 C++ 与 C# 的实现)
  • 免费降AI率的工具红黑榜:认准这2个免费降AI率工具,亲测有效!
  • 霍华德·马克斯的市场周期定位技巧
  • 1500字免费降AIGC率的额度,2026年毕业论文查重必备!
  • 1500字免费降AIGC率的额度,2026年毕业论文查重必备!(附每天5次aigc查重)
  • 别再焦虑了!6款实测有效的降ai工具推荐,学姐手把手教你降低ai率!
  • 国外软件,安装即时专业版!
  • 防控近视你需要知道的这些科普常识!
  • 抽奖机随机号码生成:3 种算法实现 + 测试全解析(附完整代码)
  • LLM入门指南:预训练、SFT和强化学习三步构建ChatGPT式大模型