ptcode = 'LSPEAS_002' # ctcode, nr = 'discharge', 1 ctcode, nr = 'pre', 2 cropname = 'stent' meshfile = 'LSPEAS_002_Smoothed5x0.8_Wrapped5mm_plus r renalis2 9_001.stl' # Load ssdf s = loadvol(basedir, ptcode, ctcode, cropname, 'avgreg') volavg = s.vol origin = volavg.origin # z y x # Load deformations (apply forward for mesh) s = loadvol(basedir, ptcode, ctcode, cropname, 'deforms') deforms = [s['deform%i' % (i * 10)] for i in range(10)] deforms = [[field[::2, ::2, ::2] for field in fields] for fields in deforms] deforms_f = [pirt.DeformationFieldForward(*f) for f in deforms] # Load mesh basedir2 = select_dir(r'C:\MedData\LSPEAS_Mimics', r'F:\LSPEAS_Mimics_backup', r'E:\LSPEAS_Mimics_backup') mesh = vv.meshRead(os.path.join(basedir2, ptcode + '_' + ctcode, meshfile)) # z is negative, must be flipped to match dicom orientation for vertice in mesh._vertices: vertice[-1] = vertice[-1] * -1 #mesh = vv.meshRead(r'D:\Profiles\koenradesma\Desktop\001_preavgreg 20150522 test itk.stl') # x and y values of vertices are negative (as in original dicom), flip to match local coordinates #mesh._vertices = mesh._vertices*-1 # when stl from itksnap ## Start vis f = vv.figure(1) vv.clf()
sigma = 20 amplitude = 25 deform = [] for d in range(2): a = np.zeros_like(im) if d == 1: amplitude_ = amplitude * 1 else: amplitude_ = amplitude * 0.0 k = pirt.gaussfun.gaussiankernel2(sigma, 0, 0, N=-6 * sigma) k *= amplitude_ * (1.0 / k.max()) # k[k>amplitude_*0.5] = amplitude_ # k[k<amplitude_] = 0.0 patch_image(a, k, (51, 51)) deform.append(a) deform_forward = pirt.DeformationFieldForward(deform) # Create a deformations in backward mode deform = [] for d in range(2): a = np.zeros_like(im) if d == 1: amplitude_ = amplitude * 1 else: amplitude_ = amplitude * 0.0 k = pirt.gaussfun.gaussiankernel2(sigma, 0, 0, N=-6 * sigma) k *= amplitude_ * (1.0 / k.max()) # k[k>amplitude_*0.5] = amplitude_ # k[k<amplitude_] = 0.0 patch_image(a, -k, (51, 51 + amplitude_)) deform.append(a)
foo = recordMovie(frameRate=7) ## Show 3D movie, by showing one volume that is moved by motion fields # Load volume s = loadvol(basedir, ptcode, ctcode, cropname, 'avgreg') vol = s.vol # Load deformations (use backward mapping to deform texture 3D volume) s = loadvol(basedir, ptcode, ctcode, cropname, 'deforms') deforms_forward = [s['deform%i' % (i * 10)] for i in range(10)] deforms_forward = [[field[::2, ::2, ::2] for field in fields] for fields in deforms_forward] deforms_forward = [ pirt.DeformationFieldForward(*fields) for fields in deforms_forward ] # wrap fields deforms_backward = [deform.as_backward() for deform in deforms_forward] # get backward mapping # Start vis f = vv.figure(nr) vv.clf() if nr == 1: f.position = 8.00, 30.00, 667.00, 690.00 else: f.position = 691.00, 30.00, 667.00, 690.00 a = vv.gca() a.axis.axisColor = 1, 1, 1 a.axis.visible = False a.bgcolor = 0, 0, 0
def __init__(self, ptcode, ctcode, basedir, showVol='mip', meshWithColors=False, motion='amplitude', clim2=(0, 2)): """ Script to show the stent model. [ nellix] """ import os import pirt import visvis as vv from stentseg.utils.datahandling import select_dir, loadvol, loadmodel from pirt.utils.deformvis import DeformableTexture3D, DeformableMesh from stentseg.stentdirect.stentgraph import create_mesh from stentseg.stentdirect import stentgraph from stentseg.utils.visualization import show_ctvolume from stentseg.motion.vis import create_mesh_with_abs_displacement import copy from stentseg.motion.dynamic import incorporate_motion_nodes, incorporate_motion_edges from lspeas.utils.ecgslider import runEcgSlider from stentseg.utils import _utils_GUI import numpy as np cropname = 'prox' # params nr = 1 # motion = 'amplitude' # amplitude or sum dimension = 'xyz' showVol = showVol # MIP or ISO or 2D or None clim0 = (0, 2000) # clim2 = (0,2) motionPlay = 9, 1 # each x ms, a step of x % s = loadvol(basedir, ptcode, ctcode, cropname, what='deforms') m = loadmodel(basedir, ptcode, ctcode, cropname, 'centerline_total_modelavgreg_deforms') v = loadmodel(basedir, ptcode, ctcode, cropname, modelname='centerline_total_modelvesselavgreg_deforms') s2 = loadvol(basedir, ptcode, ctcode, cropname, what='avgreg') vol_org = copy.deepcopy(s2.vol) s2.vol.sampling = [ vol_org.sampling[1], vol_org.sampling[1], vol_org.sampling[2] ] s2.sampling = s2.vol.sampling vol = s2.vol # merge models into one for dynamic visualization model_total = stentgraph.StentGraph() for key in dir(m): if key.startswith('model'): model_total.add_nodes_from( m[key].nodes(data=True)) # also attributes model_total.add_edges_from(m[key].edges(data=True)) for key in dir(v): if key.startswith('model'): model_total.add_nodes_from( v[key].nodes(data=True)) # also attributes model_total.add_edges_from(v[key].edges(data=True)) # Load deformations (forward for mesh) deformkeys = [] for key in dir(s): if key.startswith('deform'): deformkeys.append(key) deforms = [s[key] for key in deformkeys] deforms = [[field[::2, ::2, ::2] for field in fields] for fields in deforms] # These deforms are forward mapping. Turn into DeformationFields. # Also get the backwards mapping variants (i.e. the inverse deforms). # The forward mapping deforms should be used to deform meshes (since # the information is used to displace vertices). The backward mapping # deforms should be used to deform textures (since they are used in # interpolating the texture data). deforms_f = [pirt.DeformationFieldForward(*f) for f in deforms] deforms_b = [f.as_backward() for f in deforms_f] # Create mesh if meshWithColors: try: modelmesh = create_mesh_with_abs_displacement(model_total, radius=0.7, dim=dimension, motion=motion) except KeyError: print('Centerline model has no pathdeforms so we create them') # use unsampled deforms deforms2 = [s[key] for key in deformkeys] # deforms as backward for model deformsB = [ pirt.DeformationFieldBackward(*fields) for fields in deforms2 ] # set sampling to original # for i in range(len(deformsB)): # deformsB[i]._field_sampling = tuple(s.sampling) # not needed because we use unsampled deforms # Combine ... incorporate_motion_nodes(model_total, deformsB, s2.origin) convert_paths_to_PointSet(model_total) incorporate_motion_edges(model_total, deformsB, s2.origin) convert_paths_to_ndarray(model_total) modelmesh = create_mesh_with_abs_displacement(model_total, radius=0.7, dim=dimension, motion=motion) else: modelmesh = create_mesh(model_total, 0.7, fullPaths=True) ## Start vis f = vv.figure(nr) vv.clf() if nr == 1: f.position = 8.00, 30.00, 944.00, 1002.00 else: f.position = 968.00, 30.00, 944.00, 1002.00 a = vv.gca() a.axis.axisColor = 1, 1, 1 a.axis.visible = False a.bgcolor = 0, 0, 0 a.daspect = 1, 1, -1 t = vv.volshow(vol, clim=clim0, renderStyle=showVol, axes=a) vv.xlabel('x (mm)') vv.ylabel('y (mm)') vv.zlabel('z (mm)') if meshWithColors: if dimension == 'xyz': dim = '3D' vv.title( 'Model for chEVAS %s (color-coded %s of movement in %s in mm)' % (ptcode[8:], motion, dim)) else: vv.title('Model for chEVAS %s' % (ptcode[8:])) colorbar = True # Create deformable mesh dm = DeformableMesh(a, modelmesh) # in x,y,z dm.SetDeforms(*[list(reversed(deform)) for deform in deforms_f]) # from z,y,x to x,y,z if meshWithColors: dm.clim = clim2 dm.colormap = vv.CM_JET #todo: use colormap Viridis or Magma as JET is not linear (https://bids.github.io/colormap/) if colorbar: vv.colorbar() colorbar = False else: dm.faceColor = 'g' # Run mesh a.SetLimits() # a.SetView(viewringcrop) dm.MotionPlay(motionPlay[0], motionPlay[1]) # (10, 0.2) = each 10 ms do a step of 20% dm.motionSplineType = 'B-spline' dm.motionAmplitude = 0.5 ## run ecgslider ecg = runEcgSlider(dm, f, a, motionPlay)