news 2026/2/10 6:13:23

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/8 2:25:25

Linly-Talker数字人系统:一键生成口型同步讲解视频

Linly-Talker数字人系统&#xff1a;一键生成口型同步讲解视频 在教育机构忙着录制网课、电商主播通宵直播、客服团队疲于应对重复咨询的今天&#xff0c;一个共通的痛点浮现出来&#xff1a;优质内容生产太慢&#xff0c;人力成本太高。有没有可能让“另一个我”替我讲话&…

作者头像 李华
网站建设 2026/2/9 9:22:34

Linly-Talker支持多线程推理,高并发场景从容应对

Linly-Talker&#xff1a;高并发数字人对话系统的多线程推理实践 在虚拟主播直播间里&#xff0c;成百上千的观众同时提问&#xff1b;企业客服系统中&#xff0c;数十名员工正通过AI助手处理客户咨询&#xff1b;在线教育平台上&#xff0c;数百个学生正在与个性化AI讲师互动……

作者头像 李华
网站建设 2026/2/7 7:30:12

Win xp激活

链接&#xff1a;https://pan.quark.cn/s/15877e4b435a器。

作者头像 李华
网站建设 2026/2/8 7:19:00

AI客服升级方案:传统IVR向Linly-Talker智能交互演进

AI客服升级方案&#xff1a;传统IVR向Linly-Talker智能交互演进 在银行热线中反复按键、听机械女声播报“请按1查询余额”&#xff0c;这种体验对今天的用户来说早已过时。当人们习惯了与Siri、小爱同学自然对话&#xff0c;再回到层层菜单的语音系统&#xff0c;就像从智能手机…

作者头像 李华
网站建设 2026/2/8 2:04:58

编程世界时间对象的最小公倍数(闲话Float-Time)

五花八门赖算力&#xff0c;数值直传操现代。 笔记模板由python脚本于2025-12-20 23:48:53创建&#xff0c;本篇笔记适合喜欢日期时间玩味的coder翻阅。 学习的细节是欢悦的历程 博客的核心价值&#xff1a;在于输出思考与经验&#xff0c;而不仅仅是知识的简单复述。 Python官…

作者头像 李华
网站建设 2026/2/7 21:29:18

医疗模型推理延迟高 后来补TensorRT优化才稳住实时预警

&#x1f4dd; 博客主页&#xff1a;jaxzheng的CSDN主页 目录 医疗数据科学&#xff1a;当医院遇到Excel 一、从“手写病历”到“数据洪流” 二、AI医生&#xff1a;从“算账”到“看病” 三、数据整合&#xff1a;比调情还难的艺术 四、隐私保护&#xff1a;比防小偷还难的难题…

作者头像 李华