def sampling_def(image1, dist_img, threshold, Sample_Density): Samples2 = SpatialSampling.DiskSamples(image1, dist_img, threshold) Samples2.GenSamples( Sample_Density, 80000) # This distance will be used to calculate strains point_arr2 = Samples2.samples sample_point2 = np.transpose(point_arr2) Visualization.DataVisulization(image1, threshold).scatterplot(point_arr2) return sample_point2
def sampling_def(image1, dist_img, threshold): """ this is the sampling function input: image1 --- (N,d) np.array dist_img --- (N,d) np.array threshold --- a float value output: sample_point2 ---(N,3) np.array """ Samples2 = SpatialSampling.DiskSamples(image1, dist_img, threshold) Samples2.GenSamples(3, 80000) # this number controls the density point_arr2 = Samples2.samples sample_point2 = np.transpose(point_arr2) Visualization.DataVisulization(image1, threshold).scatterplot(point_arr2) return sample_point2
for dummy_i in range(1): Mesh_optimizer = SpatialSampling.MeshOptimizer(point_arr1.T,mesh2.ntri) Mesh_optimizer.edge_connector_smooth() displacements = [inputDVC.getDisplacementX()[0], inputDVC.getDisplacementY()[0], inputDVC.getDisplacementZ()[0]] strain = calculateStrain(point_arr1, mesh2.ntri, displacements, Sample_Density/2, 2) strain = np.array(strain) a = np.mean(strain,1) smoothStrains(strain, mesh2.ntri,4) b = np.mean(strain,1) image1 = inputDVC.getImage1() plot = Visualization.DataVisulization(image1, image1.mean()+1400) plot.contour3d() surf = plot.surf_points inter_z = interpolate.LinearNDInterpolator(mesh2.points, strain[0,:], fill_value=0.0, rescale=True) extrapo_z = inter_z.__call__(surf) plot.plot3dpoint(extrapo_z) #### strain plot #### ix, iy, iz = image1.shape new_image1 = np.empty((ix+20,iy+20,iz+20)) new_image1[20:-20,20:-20,20:-20] = image1[10:-10,10:-10,10:-10] trans_point = np.empty_like(mesh2.points) trans_point[:,0] = mesh2.points[:,0] + 10 trans_point[:,1] = mesh2.points[:,1] + 10
Index_logic = Inhull_point.reshape(imx, imy) plt.imshow(Index_logic) ### take the region 1 out from big image region1_ref = np.empty_like(im1) for i in range(im1.shape[2]): region1_ref[:, :, i] = im1[:, :, i] * Index_logic ### take the region 2 out from big image region1_def = np.empty_like(im2) for i in range(im2.shape[2]): region1_def[:, :, i] = im2[:, :, i] * Index_logic region1_def_hist = ImageProcessing.hist_match(region1_def, region1_ref) plot = Visualization.DataVisulization(ndimage.gaussian_filter(region1_ref, 5), 90) plot.contour3d() #sxx, syy = im1[:,:,60].shape #mask = np.zeros((sxx,syy)) #mask[280:500,90:350] = 1.0 #plt.imshow(mask) sio.savemat("C:/Users/HT/Desktop/test run/combine regions/Region1/mask.mat", {"par1": Index_logic}) sio.savemat( "C:/Users/HT/Desktop/test run/combine regions/Region1/region1_ref.mat", {"par1": region1_ref}) sio.savemat( "C:/Users/HT/Desktop/test run/combine regions/Region1/region1_def.mat", {"par1": region1_def_hist})
im1 = volume000['par1'][280:500,90:350,:] #[350:450,150:250,:] volume125 = sio.loadmat('/Users/junchaowei/Desktop/SpaceRegistration_000_125/volume125_regi.mat')# big region three point alignment im2 = volume125['par1'][280:500,90:350,:] plt.figure(0) plt.imshow(im1[:,:,70]) plt.show() stop #im2_matched = ImageProcessing.hist_match(im2, im1) plot = Visualization.DataVisulization(ndimage.gaussian_filter(im1,5), 110) plot.contour3d() plot2 = Visualization.DataVisulization(ndimage.gaussian_filter(im2,5), 110) plot2.contour3d() nodes, elements = plot.surf_points, plot.surf_elements nodes2, elements2 = plot2.surf_points, plot2.surf_elements whole_nodes = np.concatenate((nodes,nodes2), axis=0) sx, sy = nodes.shape elements3 = elements2 + sx whole_elements = np.concatenate((elements, elements3), axis=0) #### help function ##### def reshapePoint(point1, point2): # reshape the points array xpos = np.ndarray((2))
""" Samples2 = SpatialSampling.DiskSamples(image1, dist_img, threshold) Samples2.GenSamples(3, 80000) # this number controls the density point_arr2 = Samples2.samples sample_point2 = np.transpose(point_arr2) Visualization.DataVisulization(image1, threshold).scatterplot(point_arr2) return sample_point2 dist_img1 = mesh_dist(image1, threshold1) point_arr1 = sampling_def(image1, dist_img1, threshold1) dist_img2 = mesh_dist(image2, threshold1) point_arr2 = sampling_def(image2, dist_img2, threshold1) vl = Visualization.DataVisulization(image1, threshold1) vl.contour3d() Sample_Density = 3 mesh2 = SpatialSampling.MeshDelaunay( point_arr1.T) # use the original point_arr1 mesh2.mesh() mesh2.alpha_shape(Sample_Density) mesh2.mesh_triangle() # find unique triangles for dummy_i in range(1): Mesh_optimizer = SpatialSampling.MeshOptimizer(point_arr1.T, mesh2.ntri) Mesh_optimizer.edge_connector_smooth() NodeList = [] subSize = 21
overall_displacementZ = np.empty_like(region1_displacementZ) overall_displacementZ = overall_displacementZ + region1_displacementZ overall_displacementZ = overall_displacementZ + region2_displacementZ import matplotlib.pyplot as plt plt.figure(1) plt.imshow(overall_displacementZ[:, :, 90]) plt.show() from scipy import ndimage import Visualization from scipy import interpolate plot = Visualization.DataVisulization(ndimage.gaussian_filter(image1, 5), 110) plot.contour3d() surf = plot.surf_points inter_z = interpolate.LinearNDInterpolator(nodes, nodeDisplacementY, fill_value=0.0, rescale=True) extrapo_z = inter_z.__call__(surf) plot.plot3dpoint(extrapo_z) mlab.colorbar(object=None, title="test", orientation="vertical", nb_labels=20, nv_colors=None, label_fmt=None)
# number1 = 100000 # base line # number2 = 100000 # # sampling = SpatialSampling.RandomSampling # icp = CorrelationFunction.ICP # # #modi_volume = orig_volume2 # #for i in range(iterations): # #print "iteration #: " + str(i) # # volume_points_1 = sampling(ndimage.gaussian_filter(points_volume1,3), level1, number1) # generating new point avoiding local minimum # volume_points_2 = sampling(ndimage.gaussian_filter(points_volume2,3), level2, number2) # # new_volume2, change_points, rotation_matrix_list, translation_vector_list = icp(orig_volume2,volume_points_1,volume_points_2,5) # plot = Visualization.DataVisulization(de_volume1, 0.3) plot.contour3d() plot = Visualization.DataVisulization(de_volume2, 0.3) plot.contour3d() test = ndimage.median_filter(Refvolume.getOriginVolume()[:,:,60], 10) plt.figure(1) plt.imshow(test,cmap='Greys_r') plt.show() from skimage import exposure denoise_volume = exposure.rescale_intensity(test, in_range=(50,255)) plt.figure(2)
def selectPoints(self, visu_factor, iso): plot = Visualization.DataVisulization( ndimage.gaussian_filter(self.image1, visu_factor), iso) plot.contour3d() plot2 = Visualization.DataVisulization( ndimage.gaussian_filter(self.image2, visu_factor), iso) plot2.contour3d() nodes, elements = plot.surf_points, plot.surf_elements nodes2, elements2 = plot2.surf_points, plot2.surf_elements whole_nodes = np.concatenate((nodes, nodes2), axis=0) sx, sy = nodes.shape elements3 = elements2 + sx whole_elements = np.concatenate((elements, elements3), axis=0) #### help function ##### def reshapePoint(point1, point2): # reshape the points array xpos = np.ndarray((2)) ypos = np.ndarray((2)) zpos = np.ndarray((2)) xpos[0] = point1[0] xpos[1] = point2[0] ypos[0] = point1[1] ypos[1] = point2[1] zpos[0] = point1[2] zpos[1] = point2[2] color = 0.5 * np.ones(xpos.shape) return xpos, ypos, zpos, color # A first plot in 3D fig = mlab.figure(2) mlab.clf() mesh = mlab.triangular_mesh(whole_nodes[:, 0], whole_nodes[:, 1], whole_nodes[:, 2], whole_elements) cursor3d = mlab.points3d(0., 0., 0., mode='axes', color=(0, 0, 0), scale_factor=0.5) mlab.title('Click on the LC Region') ## A second plot, flat projection image, this is under development #fig2d = mlab.figure(2) #mlab.clf() #im = mlab.imshow(s) #cursor = mlab.points3d(0, 0, 0, mode='2dthick_cross', # color=(0, 0, 0), # scale_factor=10) #mlab.view(90, 0) ################################################################################ def picker_callback(picker_obj): print "selecting a new point" picked = picker_obj.actors if mesh.actor.actor._vtk_obj in [o._vtk_obj for o in picked]: # m.mlab_source.points is the points array underlying the vtk # dataset. GetPointId return the index in this array. print picker_obj.point_id x_ = np.lib.index_tricks.unravel_index(picker_obj.point_id, whole_nodes[:, 0].shape) print whole_nodes[:, 0][x_], whole_nodes[:, 1][ x_], whole_nodes[:, 2][x_] cursor3d.mlab_source.reset(x=whole_nodes[:, 0][x_], y=whole_nodes[:, 1][x_], z=whole_nodes[:, 2][x_]) fig.on_mouse_pick(picker_callback) mlab.show()
def computeAffine(self): ### this is help function ### def fixed_SVD(target, source): """ this module calculates the fixed SVD for the paired input points """ translation = np.zeros([1, 3]) vector = target.mean(0) - source.mean(0) # first dimension mean source = source + vector ########## rotation ############ matrix1 = np.matrix(source) matrix2 = np.matrix(target) # building the corresponding matrix resultMatrix = matrix1.T.dot(matrix2) # perform the SVD algorithm U, s, Vh = linalg.svd(resultMatrix) Rotation = Vh.T.dot(U.T) source2 = Rotation.dot(np.matrix(source).T).T #print newsource == newsource2 source = np.array(source2) # updating the source points translation = translation + vector # translation is zero initially, this does not need to add iteratively, but keep the original formula return source, Rotation, translation new = np.concatenate( (np.array(self.target_list).T, np.array(self.source_list).T), axis=1) Visualization.DataVisulization(self.image1, 1).scatterplot(new) newPoints, rotation2, translation2 = fixed_SVD( np.array(self.target_list), np.array(self.source_list)) new2 = np.concatenate( (np.array(self.target_list).T, np.array(newPoints).T), axis=1) # visualize transformed source point and target point Visualization.DataVisulization(self.image1, 1).scatterplot(new2) translation3 = ((np.array(self.source_list).mean(0)).dot(rotation2) - np.array(self.target_list).mean(0)).dot( linalg.inv(rotation2)) self.image3 = ndimage.interpolation.affine_transform( self.image2, rotation2, order=3, offset=translation3, cval=0.0) # rigid affine registration ##### Affine Model Displacement ######### affineMatrix = np.zeros((4, 4)) affineMatrix[0:3, 0:3] = rotation2 affineMatrix[0:3, 3] = translation3 affineMatrix[3, 3] = 1.0 ix, iy, iz = self.image1.shape mapX = np.empty_like(self.image1) mapY = np.empty_like(self.image1) mapZ = np.empty_like(self.image1) self.displacementX = np.empty_like(self.image1) self.displacementY = np.empty_like(self.image1) self.displacementZ = np.empty_like(self.image1) for i in range(ix): for j in range(iy): for k in range(iz): # coordinator is (i,j,k,1) cord = np.array([i, j, k, 1]) new_cord = affineMatrix.dot(cord) mapX[i, j, k] = new_cord[0] mapY[i, j, k] = new_cord[1] mapZ[i, j, k] = new_cord[2] self.displacementX[i, j, k] = new_cord[0] - i self.displacementY[i, j, k] = new_cord[1] - j self.displacementZ[i, j, k] = new_cord[2] - k