How to visualize meshes with overlay?#

[1]:
import pyvista as pv

from polpo.preprocessing import ListSqueeze, Map
from polpo.preprocessing.load.pregnancy import (
    PregnancyPilotRegisteredMeshesLoader,
    PregnancyPilotSegmentationsLoader,
)
from polpo.preprocessing.mesh.conversion import PvFromData
from polpo.preprocessing.mesh.io import PvReader
from polpo.preprocessing.mesh.transform import MeshCenterer, MeshScaler
from polpo.preprocessing.mri import (
    MeshExtractorFromSegmentedImage,
    MriImageLoader,
)
[KeOps] Warning : cuda was detected, but driver API could not be initialized. Switching to cpu only.
[2]:
STATIC_VIZ = True

if STATIC_VIZ:
    pv.set_jupyter_backend("static")

Following How to get a mesh from an MRI image?, we download a segmented MRI and get a mesh out of it.

[3]:
SESSION_ID = 2
[4]:
loader = PregnancyPilotSegmentationsLoader(subset=[SESSION_ID])

loading_pipe = loader + ListSqueeze() + MriImageLoader()
img2mesh_pipe = MeshExtractorFromSegmentedImage(encoding="ashs") + PvFromData()

pipe = loading_pipe + img2mesh_pipe

raw_mesh = pipe()
INFO: Data has already been downloaded... using cached file ('/home/luisfpereira/.herbrain/data/pregnancy/Segmentations/BB02').

We also load a registered version of the same mesh (NB: there’s more registrations at figshare).

[5]:
loader = (
    PregnancyPilotRegisteredMeshesLoader(
        subset=[SESSION_ID], method="elastic", version=1
    )
    + ListSqueeze()
    + PvReader()
)

registered_mesh = loader()
INFO: Data has already been downloaded... using cached file ('/home/luisfpereira/.herbrain/data/pregnancy/registration/elastic_1').

The registered mesh was preprocessed before registration. Let’s apply the same preprocessing in order to have an overlayable representation.

[6]:
processing_pipe = Map([MeshCenterer(), MeshScaler()], force_iter=True)

raw_mesh = processing_pipe(raw_mesh)

Visualization.

[7]:
pl = pv.Plotter(shape=(1, 2), border=False)

pl.subplot(0, 0)
pl.add_mesh(raw_mesh, show_edges=True)
pl.add_mesh(registered_mesh, opacity=0.25)
pl.remove_scalar_bar()

pl.subplot(0, 1)
pl.add_mesh(registered_mesh, show_edges=True)
pl.add_mesh(raw_mesh, opacity=0.25)
pl.remove_scalar_bar()

pl.link_views()

pl.show()
../../../_images/_notebooks_how_to_mesh_mesh_viz_compare_11_0.png

Notice the meshes fully overlap even though they have a very different combinatorial structure.

[8]:
(
    raw_mesh.faces.shape[0] // 3,
    registered_mesh.faces.shape[0] // 3,
)
[8]:
(27680, 6032)