示例#1
0
    def _run_interface(self, runtime):
        import os.path as op
        import nibabel.gifti as ng
        import numpy as np
        import skimage.measure as sm
        import nilearn.image as nimg

        import slam.io as sio
        import slam.differential_geometry as sdg

        from nipype.utils.filemanip import split_filename

        # Generate output mesh filename from the input image name
        _, fname, _ = split_filename(self.inputs.image_file)
        gii_file = op.abspath(op.join(runtime.cwd, fname + ".gii"))

        # Load the largest connected component of the input image
        img = nimg.largest_connected_component_img(self.inputs.image_file)

        # TODO: check if the input image is correct (binary)

        # Run the marching cube algorithm
        verts, faces, normals, values = sm.marching_cubes_lewiner(
            img.get_data(), self.inputs.level)

        # Convert vertices coordinates to image space
        # TODO: check that is correct by plotting the mesh on the image
        x, y, z = nimg.coord_transform(verts[:, 0], verts[:, 1], verts[:, 2],
                                       img.affine)
        mm_verts = np.array([x, y, z]).T

        # Save the mesh as Gifti
        # FIXME: FreeView can not open the mesh (but anatomist do)
        gii = ng.GiftiImage(darrays=[
            ng.GiftiDataArray(mm_verts, intent='NIFTI_INTENT_POINTSET'),
            ng.GiftiDataArray(faces, intent='NIFTI_INTENT_TRIANGLE')
        ])
        gii.meta = ng.GiftiMetaData().from_dict({
            "volume_file":
            self.inputs.image_file,
            "marching_cube_level":
            str(self.inputs.level),
            "smoothing_iterations":
            str(self.inputs.smoothing_iter),
            "smoothing_dt":
            str(self.inputs.smoothing_dt)
        })
        ng.write(gii, gii_file)

        # Optional: Smooth the marching cube output with SLAM
        if self.inputs.smoothing_iter > 0:
            mesh = sdg.laplacian_mesh_smoothing(
                sio.load_mesh(gii_file),
                nb_iter=self.inputs.smoothing_iter,
                dt=self.inputs.smoothing_dt)
            sio.write_mesh(mesh, gii_file)

        return runtime
示例#2
0
# License: BSD (3-clause)
# sphinx_gallery_thumbnail_number = 2

###############################################################################
# Importation of slam modules
import slam.distortion as sdst
import slam.differential_geometry as sdg
import slam.plot as splt
import slam.io as sio
import numpy as np

###############################################################################
# Loading an example mesh and a smoothed copy of it

mesh = sio.load_mesh('data/example_mesh.gii')
mesh_s = sdg.laplacian_mesh_smoothing(mesh, nb_iter=50, dt=0.1)

################################################################################
# Visualization of the original mesh
visb_sc = splt.visbrain_plot(mesh=mesh, caption='original mesh')
visb_sc.preview()

###############################################################################
# Visualization of the smoothed mesh
visb_sc = splt.visbrain_plot(mesh=mesh_s,
                             caption='smoothed mesh',
                             visb_sc=visb_sc)
visb_sc.preview()

###############################################################################
# Computation of the angle difference between each faces of mesh and mesh_s
if __name__ == '__main__':
    # load example data
    mesh = sio.load_mesh('data/example_mesh.gii')
    tex = sio.load_texture('data/example_texture.gii')

    # compute various types of Laplacian of the mesh
    lap, lap_b = sdg.compute_mesh_laplacian(mesh, lap_type='fem')
    print(mesh.vertices.shape)
    print(lap.shape)
    lap, lap_b = sdg.compute_mesh_laplacian(mesh, lap_type='conformal')
    lap, lap_b = sdg.compute_mesh_laplacian(mesh, lap_type='meanvalue')
    lap, lap_b = sdg.compute_mesh_laplacian(mesh, lap_type='authalic')

    # smooth the mesh using Laplacian
    s_mesh = sdg.laplacian_mesh_smoothing(mesh, nb_iter=100, dt=0.1)

    # compute the gradient of texture tex
    triangle_grad = sdg.triangle_gradient(mesh, tex.darray[0])
    print(triangle_grad)
    grad = sdg.gradient(mesh, tex.darray[0])
    print(grad.values)
    norm_grad = sdg.norm_gradient(mesh, tex.darray[0])
    print(norm_grad)

    # compute the depth potential function
    dpf = sdg.depth_potential_function(mesh, tex.darray[0], [0.3])

    # plot all that to show the results
    visb_sc = splt.visbrain_plot(mesh=mesh,
                                 tex=tex.darray[0],
示例#4
0
            "marching_cube_level": self.input_spec.level,
            "smoothing_iterations": self.input_spec.smoothing_iter,
            "smoothing_dt": self.input_spec.smoothing_dt
=======
            "volume_file": self.inputs.image_file,
            "marching_cube_level": str(self.inputs.level),
            "smoothing_iterations": str(self.inputs.smoothing_iter),
            "smoothing_dt": str(self.inputs.smoothing_dt)
>>>>>>> 02924efd7fd9ff93e10f0e0030a43b878812e288
        })
        ng.write(gii, gii_file)

        # Optional: Smooth the marching cube output with SLAM
<<<<<<< HEAD
        if self.input_spec.smoothing_iter > 0:
            mesh = sdg.laplacian_mesh_smoothing(
                sio.load_mesh(gii_file), 
                nb_iter=self.input_spec.smoothing_iter,
                dt=self.input_spec.smoothing_dt)
            sio.write_mesh(mesh, gii_file)
=======
        if self.inputs.smoothing_iter > 0:
            mesh = sdg.laplacian_mesh_smoothing(
                sio.load_mesh(gii_file),
                nb_iter=self.inputs.smoothing_iter,
                dt=self.inputs.smoothing_dt)
            sio.write_mesh(mesh, gii_file)

        return runtime
>>>>>>> 02924efd7fd9ff93e10f0e0030a43b878812e288