.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/MeshGeneration/demo_image-based.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_MeshGeneration_demo_image-based.py: Image-based Meshing =================== This example generates different types of image-based meshes using the `Stanford bunny `_ as an example. .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python import mymesh from mymesh import image, visualize import matplotlib.pyplot as plt threshold = 100 bunny_img = mymesh.demo_image('bunny') voxelsize = (0.337891, 0.337891, 0.5) # mm .. GENERATED FROM PYTHON SOURCE LINES 19-24 Voxel meshes represent every voxel (three dimensional pixel) with a box, and the voxel intensity value is stored as element data (:code:`ElemData['Image Data']`) in the mesh. The full image can be converted to a voxel mesh, with an element for every voxel, or a threshold can be applied to extract an object from the image. The image can also be coarsened (or refined) with the :code:`scalefactor` input to create a coarser mesh. For images other than the demo image, files (or directories of files, such as DICOMs) can be read with :func:mymesh.image.read, or 3D numpy arrays can be used directly (see also: :ref:`guide_image`) .. GENERATED FROM PYTHON SOURCE LINES 24-29 .. code-block:: Python bunny_voxel = image.VoxelMesh(bunny_img, voxelsize, scalefactor=0.5) half_voxel = bunny_voxel.Clip(normal=[0,1,0]) bunny_voxel2 = image.VoxelMesh(bunny_img, voxelsize, threshold, scalefactor=0.2) .. rst-class:: sphx-glr-script-out .. code-block:: none Calculating element centroids...Done .. GENERATED FROM PYTHON SOURCE LINES 30-31 Surface meshes or tetrahedral volume meshes can also be extracted from the image by applying a threshold and interpolating to create a more 'smooth' surface (see also :ref:`theory_contour`). .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python bunny_surf = image.SurfaceMesh(bunny_img, voxelsize, threshold, scalefactor=0.5) bunny_tet = image.TetMesh(bunny_img, voxelsize, threshold, scalefactor=0.5, interpolation='linear') half_tet = bunny_tet.Clip(pt=[0,88,0], normal=[0,1,0]) .. rst-class:: sphx-glr-script-out .. code-block:: none Calculating element centroids...Done .. GENERATED FROM PYTHON SOURCE LINES 36-52 .. code-block:: Python fig, axes = plt.subplots(2, 2) titles = ['Full Voxel', 'Thresholded Voxel', 'Surface Mesh', 'Tetrahedral Mesh'] meshes = [half_voxel, bunny_voxel2, bunny_surf, half_tet] for m, ax, title in zip(meshes, axes.ravel(), titles): m.verbose=False if title == 'Full Voxel': subfig, subax = m.plot(scalars='Image Data', color='Greys_r', show=False,return_fig=True, view='-x-z') elif title == 'Thresholded Voxel': subfig, subax = m.plot(show_edges=True,show=False,return_fig=True, view='-x-z') else: subfig, subax = m.plot(show=False,return_fig=True, view='-x-z') ax.imshow(subax.get_images()[0].get_array()) ax.set_title(title) ax.set_axis_off() plt.close(subfig) .. image-sg:: /examples/MeshGeneration/images/sphx_glr_demo_image-based_001.png :alt: Full Voxel, Thresholded Voxel, Surface Mesh, Tetrahedral Mesh :srcset: /examples/MeshGeneration/images/sphx_glr_demo_image-based_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() RFBOutputContext() RFBOutputContext() RFBOutputContext() .. rst-class:: sphx-glr-timing **Total running time of the script:** (2 minutes 50.752 seconds) .. _sphx_glr_download_examples_MeshGeneration_demo_image-based.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_image-based.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_image-based.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_image-based.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_