当前位置:   article > 正文

分层存储的图片的3d显示

分层存储的图片的3d显示

分层存储的图片叠层成为3d,并显示。

文件夹D:\mask内的分层存储的图像文件mask_1.PNG至mask_12.PNG:

1、显示为3d点云:

  1. import open3d as o3d
  2. import numpy as np
  3. from PIL import Image
  4. def images2point_cloud(paths, layer_height):
  5. points = []
  6. for i, image_path in enumerate(paths): # 分层读取
  7. img = Image.open(image_path)
  8. img_array = np.array(img)
  9. img_height, img_width = img_array.shape
  10. for y in range(img_height):
  11. for x in range(img_width):
  12. if img_array[y][x] > 0: # 假设非黑色点为感兴趣的点
  13. points.append([x, y, i * layer_height]) # 将 2D 像素点转换为 3D 点,高度使用索引值
  14. return points
  15. # 替换为你实际存储图像的路径
  16. image_paths = ['D:\\mask\\mask_1.PNG', 'D:\\mask\\mask_2.PNG', 'D:\\mask\\mask_3.PNG', 'D:\\mask\\mask_4.PNG', 'D:\\mask\\mask_5.PNG', 'D:\\mask\\mask_6.PNG', 'D:\\mask\\mask_7.PNG', 'D:\\mask\\mask_8.PNG', 'D:\\mask\\mask_9.PNG', 'D:\\mask\\mask_10.PNG', 'D:\\mask\\mask_11.PNG', 'D:\\mask\\mask_12.PNG']
  17. # 定义层间距离
  18. height = 5.0
  19. points = images2point_cloud(image_paths, height)
  20. point_cloud = o3d.geometry.PointCloud()
  21. point_cloud.points = o3d.utility.Vector3dVector(points)
  22. o3d.visualization.draw_geometries([point_cloud])

 输出:

 

进一步,可以自定义云点的颜色:

  1. import open3d as o3d
  2. import numpy as np
  3. from PIL import Image
  4. def images2point_cloud(paths, layer_height):
  5. points = []
  6. colors = []
  7. for i, image_path in enumerate(paths): # 分层读取
  8. img = Image.open(image_path)
  9. img_array = np.array(img)
  10. img_height, img_width = img_array.shape
  11. for y in range(img_height):
  12. for x in range(img_width):
  13. if img_array[y][x] > 0: # 假设非黑色点为感兴趣的点
  14. points.append([x, y, i * layer_height]) # 将 2D 像素点转换为 3D 点,高度使用索引值
  15. colors.append([1, 0, 0]) # 设置颜色为红色
  16. return points, colors
  17. # 替换为你实际存储图像的路径
  18. image_paths = ['D:\\mask\\mask_1.PNG', 'D:\\mask\\mask_2.PNG', 'D:\\mask\\mask_3.PNG', 'D:\\mask\\mask_4.PNG', 'D:\\mask\\mask_5.PNG', 'D:\\mask\\mask_6.PNG', 'D:\\mask\\mask_7.PNG', 'D:\\mask\\mask_8.PNG', 'D:\\mask\\mask_9.PNG', 'D:\\mask\\mask_10.PNG', 'D:\\mask\\mask_11.PNG', 'D:\\mask\\mask_12.PNG']
  19. # 定义层间距离
  20. height = 5.0
  21. points, colors = images2point_cloud(image_paths, height)
  22. point_cloud = o3d.geometry.PointCloud()
  23. point_cloud.points = o3d.utility.Vector3dVector(points)
  24. point_cloud.colors = o3d.utility.Vector3dVector(colors)
  25. # 显示渲染后的 3D 实体
  26. o3d.visualization.draw_geometries([point_cloud])

 

 2、将点云渲染为体素网格

  1. import open3d as o3d
  2. import numpy as np
  3. from PIL import Image
  4. def images2point_cloud(paths, layer_height):
  5. points = []
  6. for i, image_path in enumerate(paths): # 分层读取
  7. img = Image.open(image_path)
  8. img_array = np.array(img)
  9. img_height, img_width = img_array.shape
  10. for y in range(img_height):
  11. for x in range(img_width):
  12. if img_array[y][x] > 0: # 假设非黑色点为感兴趣的点
  13. points.append([x, y, i * layer_height]) # 将 2D 像素点转换为 3D 点,高度使用索引值
  14. return points
  15. # 替换为你实际存储图像的路径
  16. image_paths = ['D:\\mask\\mask_1.PNG', 'D:\\mask\\mask_2.PNG', 'D:\\mask\\mask_3.PNG', 'D:\\mask\\mask_4.PNG', 'D:\\mask\\mask_5.PNG', 'D:\\mask\\mask_6.PNG', 'D:\\mask\\mask_7.PNG', 'D:\\mask\\mask_8.PNG', 'D:\\mask\\mask_9.PNG', 'D:\\mask\\mask_10.PNG', 'D:\\mask\\mask_11.PNG', 'D:\\mask\\mask_12.PNG']
  17. # 定义层间距离
  18. height = 5.0
  19. points = images2point_cloud(image_paths, height)
  20. point_cloud = o3d.geometry.PointCloud()
  21. point_cloud.points = o3d.utility.Vector3dVector(points)
  22. # 创建点云体素网格
  23. voxel_size = 1
  24. voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(point_cloud, voxel_size=voxel_size)
  25. # 显示体素网格,指定渲染模式为网格
  26. o3d.visualization.draw_geometries([voxel_grid], mesh_show_wireframe=True)

 3、使用vtk库显示3d实体

参照博文:【Python VTK】读取二维序列医学图像分割结果并进行三维重建_vtk三维重建-CSDN博客

 

  1. import vtk
  2. # 创建渲染器、窗口和交互器
  3. renderer = vtk.vtkRenderer()
  4. window = vtk.vtkRenderWindow()
  5. window.AddRenderer(renderer)
  6. interactor = vtk.vtkRenderWindowInteractor()
  7. interactor.SetRenderWindow(window)
  8. # 图像读取
  9. reader = vtk.vtkPNGReader()
  10. reader.SetDataExtent(0, 300, 0, 300, 0, 12) # 定义图像大小和层数
  11. name_prefix = 'D:/mask/mask_' # 设置图像前缀名字
  12. reader.SetFilePrefix(name_prefix)
  13. reader.SetFilePattern("%s%d.PNG")
  14. reader.Update()
  15. reader.SetDataByteOrderToLittleEndian()
  16. spacing = [1.0, 1.0, 5.0] # x, y 方向上的间距为 1 像素,层间距距为 5 像素
  17. reader.GetOutput().SetSpacing(spacing)
  18. # 高斯平滑
  19. gauss = vtk.vtkImageGaussianSmooth()
  20. gauss.SetInputConnection(reader.GetOutputPort())
  21. gauss.SetStandardDeviations(1.0, 1.0, 1.0)
  22. gauss.SetRadiusFactors(1.0, 1.0, 1.0)
  23. gauss.Update()
  24. # 计算轮廓
  25. contour = vtk.vtkMarchingCubes()
  26. gauss.GetOutput().SetSpacing(spacing)
  27. contour.SetInputConnection(gauss.GetOutputPort())
  28. contour.ComputeNormalsOn()
  29. contour.SetValue(0, 100)
  30. # 映射器和演员
  31. mapper = vtk.vtkPolyDataMapper()
  32. mapper.SetInputConnection(contour.GetOutputPort())
  33. mapper.ScalarVisibilityOff()
  34. actor = vtk.vtkActor()
  35. actor.SetMapper(mapper)
  36. # 设置渲染器背景颜色
  37. renderer.SetBackground([0.0, 0.0, 0.0])
  38. renderer.AddActor(actor)
  39. # 开始显示
  40. if __name__ == '__main__':
  41. window.Render()
  42. interactor.Initialize()
  43. interactor.Start()

 显示效果:

​​​​​​​

 持续寻找新的方法中。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/692111
推荐阅读
相关标签
  

闽ICP备14008679号