news 2026/8/16 0:36:19

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/8/16 18:21:06

数字人品牌代言:虚拟偶像商业化的技术基石

数字人品牌代言&#xff1a;虚拟偶像商业化的技术基石 在品牌营销的战场上&#xff0c;一个新趋势正悄然重塑用户与企业的互动方式——虚拟代言人。从洛天依到AYAYI&#xff0c;从天猫精灵3D客服到某手机品牌的“数字代言人”直播带货&#xff0c;越来越多企业开始用一张AI生成…

作者头像 李华
网站建设 2026/8/16 19:04:19

AI导游上线:景区小程序集成Linly-Talker实战记录

AI导游上线&#xff1a;景区小程序集成Linly-Talker实战记录 在杭州西湖边的某个清晨&#xff0c;一位游客掏出手机打开景区小程序&#xff0c;轻点“问我”按钮&#xff0c;对着麦克风问道&#xff1a;“雷峰塔为什么晚上会亮灯&#xff1f;”不到两秒&#xff0c;屏幕中一位面…

作者头像 李华
网站建设 2026/8/16 12:32:48

数字人客服质检:自动评估服务态度与话术规范性

数字人客服质检&#xff1a;自动评估服务态度与话术规范性 在银行客服热线中&#xff0c;一个声音温和、回应精准的“客服专员”耐心解答着用户关于账单的疑问&#xff1b;在电商App里&#xff0c;一位面带微笑的虚拟导购员正根据你的浏览记录推荐商品——这些角色没有工牌&…

作者头像 李华
网站建设 2026/8/15 21:25:12

Linly-Talker支持Prometheus监控,纳入统一运维体系

Linly-Talker 支持 Prometheus 监控&#xff0c;纳入统一运维体系 在当前 AI 驱动的数字人应用快速落地的背景下&#xff0c;越来越多企业开始部署虚拟主播、智能客服和数字员工。这类系统虽然功能强大&#xff0c;但其内部由多个深度学习模型协同工作——从语音识别到语言生成…

作者头像 李华
网站建设 2026/8/16 8:14:46

用Linly-Talker制作节日祝福视频?个性化礼品新创意

用Linly-Talker制作节日祝福视频&#xff1f;个性化礼品新创意 在母亲节的清晨&#xff0c;一条由“妈妈本人”出镜说出“孩子&#xff0c;妈妈永远爱你”的短视频&#xff0c;悄然出现在家庭群聊中——而实际上&#xff0c;这位母亲从未录制过这段话。画面里是她熟悉的面容&am…

作者头像 李华
网站建设 2026/8/16 10:34:33

复星与比亚迪达成全球战略合作,引领“出行+度假“新生态

、美通社消息&#xff1a;12月18日&#xff0c;复星与比亚迪在太仓阿尔卑斯国际度假区达成全球长期战略合作。双方将基于各自在绿色出行、智能制造、旅游度假及家庭消费生态领域的核心优势&#xff0c;开展全品牌矩阵深度协同&#xff0c;共同构建"出行度假"融合生态…

作者头像 李华