Ejemplo n.º 1
0
    def create_head(self):
        path = mne.datasets.sample.data_path()
        surf = mne.read_bem_surfaces(path +
                                     '/subjects/sample/bem/sample-head.fif')[0]
        points, triangles = surf['rr'], surf['tris']
        # reduce to 30000 triangles:
        points_dec, triangles_dec = decimate_surface(points,
                                                     triangles,
                                                     n_triangles=30000)
        p, t = points_dec, triangles_dec
        mesh_data = gl.MeshData(p, t)
        mesh_item = gl.GLMeshItem(meshdata=mesh_data,
                                  computeNormals=True,
                                  shader='viewNormalColor',
                                  glOptions='translucent')
        mesh_item.translate(0, 0, -20)
        mesh_item.rotate(90, 1, 0, 0)
        mesh_item.scale(650, 650, 650)
        mesh_item.setColor([0, 0, 1, 0.4])

        self.view.addItem(mesh_item)
Ejemplo n.º 2
0
instead of e.g. EEG-cap positions.

"""
# Authors: Denis Engemann <*****@*****.**>
#          Alexandre Gramfort <*****@*****.**>
#
# License: BSD (3-clause)

import mne
from mne.surface import decimate_surface

print(__doc__)

path = mne.datasets.sample.data_path()
surf = mne.read_bem_surfaces(path + '/subjects/sample/bem/sample-head.fif')[0]

points, triangles = surf['rr'], surf['tris']

# reduce to 30000 meshes equaling ${SUBJECT}-head-medium.fif output from
# mne_make_scalp_surfaces.py and mne_make_scalp_surfaces
points_dec, triangles_dec = decimate_surface(points,
                                             triangles,
                                             n_triangles=30000)

from mayavi import mlab  # noqa

head_col = (0.95, 0.83, 0.83)  # light pink

p, t = points_dec, triangles_dec
mlab.triangular_mesh(p[:, 0], p[:, 1], p[:, 2], t, color=head_col)
"""
print(__doc__)

# Authors: Denis Engemann <*****@*****.**>
#          Alexandre Gramfort <*****@*****.**>
#
# License: BSD (3-clause)

import mne
from mne.surface import decimate_surface

path = mne.datasets.sample.data_path()
surf = mne.read_bem_surfaces(path + '/subjects/sample/bem/sample-head.fif')[0]

points, triangles = surf['rr'], surf['tris']

# reduce to 30000 meshes equaling ${SUBJECT}-head-medium.fif output from
# mne_make_scalp_surfaces.py and mne_make_scalp_surfaces
points_dec, triangles_dec = decimate_surface(points, triangles,
                                             n_triangles=30000)

try:
    from enthought.mayavi import mlab
except:
    from mayavi import mlab

head_col = (0.95, 0.83, 0.83)  # light pink

p, t = points_dec, triangles_dec
mlab.triangular_mesh(p[:, 0], p[:, 1], p[:, 2], t, color=head_col)
def process_bem(fs_subject_path, bem_path_):
    """
     Main function for BEM extraction.
    """
    subject_ = bem_path_.name.split("_")[0].replace("AVG", "ANTS")

    epi_img_ = nib.load(str(bem_path_))

    for file_name_, discard_inds in zip(
        ["outer_skin.surf", "outer_skull.surf", "inner_skull.surf"],
        [[1, 2, 3, 4], [1, 2, 3], [1, 2]]):
        epi_img_data_ = deepcopy(epi_img_.get_fdata())

        correct_line_artefact(epi_img_data_)

        cond = np.stack([(epi_img_data_ == i_) for i_ in discard_inds]).sum(0)
        epi_img_data_[np.where(cond)] = 1
        epi_img_data_[np.where(np.logical_not(cond))] = 0
        vertices, simplices = measure.marching_cubes_lewiner(
            epi_img_data_, spacing=(1, 1, 1), allow_degenerate=False)[:2]

        path_white = fs_subject_path / subject_ / "surf" / "lh.white"

        try:
            volume_info_ = read_geometry(path_white, read_metadata=True)[2]
        except:
            print("Skipping subject {}...".format(subject_))
            continue

        vertices = vertices @ epi_img_.affine[:3, :
                                              3] + epi_img_.affine[:3,
                                                                   3] - volume_info_[
                                                                       "cras"]

        mesh_ = trimesh.Trimesh(vertices=vertices, faces=simplices)
        trimesh.repair.fix_normals(mesh_, multibody=False)

        smooth_mesh = trimesh.smoothing.filter_laplacian(
            deepcopy(mesh_), lamb=0.8, iterations=15, volume_constraint=True)

        bem_output_path_ = fs_subject_path / subject_ / "bem"
        bem_output_path_.mkdir(parents=True, exist_ok=True)

        vertices, faces_ = smooth_mesh.vertices, smooth_mesh.faces

        # Defect corrections for the large meshes
        vertices, faces_ = fix_all_defects(vertices, faces_)

        # Writing a freesufer mesh file
        file_name_large = file_name_.split(".")[0] + "_large.surf"
        write_geometry(str(bem_output_path_ / file_name_large), vertices,
                       faces_)

        # Writing an obj mesh file
        with (bem_output_path_ /
              file_name_large).with_suffix(".obj").open('w') as file_obj:
            file_obj.write(
                trimesh.exchange.obj.export_obj(
                    trimesh.Trimesh(vertices, faces_)))

        # Decimating BEM surfaces
        vertices, faces_ = decimate_surface(vertices,
                                            faces_,
                                            n_triangles=5120,
                                            method='sphere')

        # Defect correction for decimated meshes...
        vertices, faces_ = fix_all_defects(vertices, faces_)

        # Writing an obj mesh file
        with (bem_output_path_ /
              file_name_).with_suffix(".obj").open('w') as file_obj:
            file_obj.write(
                trimesh.exchange.obj.export_obj(
                    trimesh.Trimesh(vertices, faces_)))

        # Writing a freesufer mesh file
        print("Writing {}...".format(str(bem_output_path_ / file_name_)))
        write_geometry(str(bem_output_path_ / file_name_),
                       vertices,
                       faces_,
                       volume_info=volume_info_)