def draw_layer(path, position): # load a png with a scale 0->1 and four color channels (an extra alpha channel for transparency). im = pl.imread(path, format='png') * 255 colors = tvtk.UnsignedCharArray() colors.from_array(im.transpose((1, 0, 2)).reshape(-1, 4)) m_image = mlab.imshow( np.ones(im.shape[:2]), extent=[0, 0, 0, 0, position, position], opacity=0.6) m_image.actor.input.point_data.scalars = colors
def _plot_imshow(self, im, alpha, position=(0, 0, 0), orientation=(0, 0, 0), scale=1.0, **kwargs): """ Plot a color image with mayavi.mlab.imshow. im is a ndarray with dim (n, m, 3) and scale (0->255] alpha is a single number or a ndarray with dim (n*m) and scale (0->255] **kwargs is passed onto mayavi.mlab.imshow(..., **kwargs) """ if len(alpha.shape) != 1: alpha = alpha.flatten() # The lut is a Nx4 array, with the columns representing RGBA # (red, green, blue, alpha) coded with integers going from 0 to 255, # we create it by stacking all the pixles (r,g,b,alpha) as rows. # lut = np.c_[im.reshape(-1, 3), alpha] / 255.0 # lut_lookup_array = np.arange( # im.shape[0] * im.shape[1]).reshape(im.shape[0], im.shape[1]) # We can display an color image by using mlab.imshow, a lut color list and # a lut lookup table. # temporary colormap # imshow = mlab.imshow(lut_lookup_array, # colormap='binary', # **kwargs) colors = tvtk.UnsignedCharArray() im_shape = im.shape im_rgba = np.concatenate((im, alpha.reshape(im_shape[0], im_shape[1], 1)), axis=-1) im_rgba_T = im_rgba.transpose((1, 0, 2)) colors.from_array(im_rgba_T.reshape(-1, 4)) image = mlab.imshow(np.ones(im_rgba_T.shape[:2])) image.actor.input.point_data.scalars = colors image.actor.scale = (scale, scale, 1.0) image.actor.position = position image.actor.orientation = orientation #imshow.module_manager.scalar_lut_manager.lut.table = lut # print 'xxx' # imshow.module_manager.scalar_lut_manager.load_lut_from_list(lut) # print 'yyy' return image
def draw_lidar_simple(pc, color=None): ''' Draw lidar points. simplest set up. ''' fig = mlab.figure(figure=None, bgcolor=(0, 0, 0), fgcolor=None, engine=None, size=(1600, 1000)) if color is None: color = pc[:, 2] #draw points # mlab.points3d(pc[:,0], pc[:,1], pc[:,2], pc[:,2], color=color, mode='point', colormap = 'gnuplot', scale_factor=1, figure=fig) pts = mlab.points3d(pc[:, 0], pc[:, 1], pc[:, 2]) sc = tvtk.UnsignedCharArray() sc.from_array(color) pts.mlab_source.dataset.point_data.scalars = sc pts.mlab_source.dataset.modified() #draw origin mlab.points3d(0, 0, 0, color=(1, 1, 1), mode='sphere', scale_factor=0.02) #draw axis axes = np.array([ [2., 0., 0., 0.], [0., 2., 0., 0.], [0., 0., 2., 0.], ], dtype=np.float64) mlab.plot3d([0, axes[0, 0]], [0, axes[0, 1]], [0, axes[0, 2]], color=(1, 0, 0), tube_radius=None, figure=fig) mlab.plot3d([0, axes[1, 0]], [0, axes[1, 1]], [0, axes[1, 2]], color=(0, 1, 0), tube_radius=None, figure=fig) mlab.plot3d([0, axes[2, 0]], [0, axes[2, 1]], [0, axes[2, 2]], color=(0, 0, 1), tube_radius=None, figure=fig) mlab.view(azimuth=180, elevation=70, focalpoint=[12.0909996, -1.04700089, -2.03249991], distance=62.0, figure=fig) return fig
def mlab_imshowColor(im, alpha = 255, **kwargs): """ Plot a color image with mayavi.mlab.imshow. im is a ndarray with dim (n, m, 3) and scale (0->255] alpha is a single number or a ndarray with dim (n*m) and scale (0->255] **kwargs is passed onto mayavi.mlab.imshow(..., **kwargs) """ from tvtk.api import tvtk # Homogenous coordinate conversion im = np.concatenate((im, alpha * np.ones((im.shape[0], im.shape[1], 1), dtype = np.uint8)), axis = -1) colors = tvtk.UnsignedCharArray() colors.from_array(im.reshape(-1, 4)) m_image = mlab.imshow(np.ones(im.shape[:2][::-1])) m_image.actor.input.point_data.scalars = colors mlab.draw() mlab.show() return
def frame_to_actor(frame, width, height, b=5): # Dimensions with borders. w, h = width+2*b, height+2*b # Initialize array. data = np.zeros((w, h, 4)) if frame is None: data[:,:,-1] = 255 data[b:-b,b:-b,-1] = 0 else: data[b:-b,b:-b,:3] = frame data[:,:,-1] = 255 data = np.transpose(data, (1,0,2)) colors = np.reshape(data, (w*h,4)) # Create actor. image = tvtk.ImageData() image.point_data.scalars=tvtk.UnsignedCharArray() image.point_data.scalars.from_array(colors) image.dimensions = np.array((w, h, 1)) actor = tvtk.ImageActor() configure_input(actor, image) return actor
def screenshot(figure=None, mode='rgb', antialiased=False): """ Return the current figure pixmap as an array. **Parameters** :figure: a figure instance or None, optional If specified, the figure instance to capture the view of. :mode: {'rgb', 'rgba'} The color mode of the array captured. :antialiased: {True, False} Use anti-aliasing for rendering the screenshot. Uses the number of aa frames set by figure.scene.anti_aliasing_frames **Notes** On most systems, this works similarly to taking a screenshot of the rendering window. Thus if it is hidden by another window, you will capture the other window. This limitation is due to the heavy use of the hardware graphics system. **Examples** This function can be useful for integrating 3D plotting with Mayavi in a 2D plot created by matplotlib. >>> from mayavi import mlab >>> mlab.test_plot3d() >>> arr = mlab.screenshot() >>> import pylab as pl >>> pl.imshow(arr) >>> pl.axis('off') >>> pl.show() """ if figure is None: figure = gcf() # don't use figure.scene.get_size() here because this will be incorrect # for HiDPI systems x, y = tuple(figure.scene.render_window.size) # Try to lift the window figure.scene._lift() if mode == 'rgb': out = tvtk.UnsignedCharArray() shape = (y, x, 3) pixel_getter = figure.scene.render_window.get_pixel_data if vtk_major_version > 7: pg_args = (0, 0, x - 1, y - 1, 1, out, 0) else: pg_args = (0, 0, x - 1, y - 1, 1, out) elif mode == 'rgba': out = tvtk.FloatArray() shape = (y, x, 4) pixel_getter = figure.scene.render_window.get_rgba_pixel_data if vtk_major_version > 7: pg_args = (0, 0, x - 1, y - 1, 1, out, 0) else: pg_args = (0, 0, x - 1, y - 1, 1, out) else: raise ValueError('mode type not understood') if antialiased: # save the current aa value to restore it later render_window = figure.scene.render_window if hasattr(render_window, 'aa_frames'): old_aa = render_window.aa_frames render_window.aa_frames = figure.scene.anti_aliasing_frames else: old_aa = render_window.multi_samples render_window.multi_samples = figure.scene.anti_aliasing_frames figure.scene.render() pixel_getter(*pg_args) if hasattr(render_window, 'aa_frames'): render_window.aa_frames = old_aa else: render_window.multi_samples = old_aa figure.scene.render() else: pixel_getter(*pg_args) # Return the array in a way that pylab.imshow plots it right: out = out.to_array() out.shape = shape out = np.flipud(out) return out
def visualize_skeleton(current_path, filename_skeleton, filename_pcloud): model_skeleton = current_path + filename_skeleton print("Loading 3D skeleton file {}...\n".format(filename_skeleton)) model_skeleton_name_base = os.path.splitext(model_skeleton)[0] #load the ply format skeleton file try: with open(model_skeleton, 'rb') as f: plydata_skeleton = PlyData.read(f) num_vertex_skeleton = plydata_skeleton.elements[0].count N_edges_skeleton = len(plydata_skeleton['edge'].data['vertex_indices']) array_edges_skeleton = plydata_skeleton['edge'].data['vertex_indices'] print("Ply data structure:") #print(plydata_skeleton) print("Number of 3D points in skeleton model: {0} \n".format(num_vertex_skeleton)) print("Number of edges: {0} \n".format(N_edges_skeleton)) except: print("Model skeleton file does not exist!") sys.exit(0) #Parse ply format skeleton file and Extract the data Data_array_skeleton = np.zeros((num_vertex_skeleton, 3)) Data_array_skeleton[:,0] = plydata_skeleton['vertex'].data['x'] Data_array_skeleton[:,1] = plydata_skeleton['vertex'].data['y'] Data_array_skeleton[:,2] = plydata_skeleton['vertex'].data['z'] X_skeleton = Data_array_skeleton[:,0] Y_skeleton = Data_array_skeleton[:,1] Z_skeleton = Data_array_skeleton[:,2] #Load ply point cloud file if not (filename_pcloud is None): model_pcloud = current_path + filename_pcloud print("Loading 3D point cloud {}...\n".format(filename_pcloud)) model_pcloud_name_base = os.path.splitext(model_pcloud)[0] pcd = o3d.io.read_point_cloud(model_pcloud) Data_array_pcloud = np.asarray(pcd.points) if pcd.has_colors(): print("Render colored point cloud") pcd_color = np.asarray(pcd.colors) if len(pcd_color) > 0: pcd_color = np.rint(pcd_color * 255.0) #pcd_color = tuple(map(tuple, pcd_color)) else: print("Generate randdom color") pcd_color = np.random.randint(256, size = (len(Data_array_pcloud),3)) #print(Data_array_pcloud.shape) #print(len(Data_array_pcloud)) #print(pcd_color.shape) #print(type(pcd_color)) #Visualization pipeline #################################################################### # The number of points per line N = 2 mlab.figure(1, size=(800, 800), bgcolor=(0, 0, 0)) mlab.clf() #pts = mlab.points3d(X_skeleton, Y_skeleton, Z_skeleton, mode = 'point') #pts = mlab.points3d(Data_array_pcloud[:,0], Data_array_pcloud[:,1], Data_array_pcloud[:,2], mode = 'point') #visualize point cloud model with color #################################################################### if not (filename_pcloud is None): x, y, z = Data_array_pcloud[:,0], Data_array_pcloud[:,1], Data_array_pcloud[:,2] pts = mlab.points3d(x,y,z, mode = 'point') sc = tvtk.UnsignedCharArray() sc.from_array(pcd_color) pts.mlab_source.dataset.point_data.scalars = sc pts.mlab_source.dataset.modified() #visualize skeleton model, edge, nodes #################################################################### x = list() y = list() z = list() s = list() connections = list() # The index of the current point in the total amount of points index = 0 for i in range(N_edges_skeleton): #print("Edges {0} has nodes {1}, {2}\n".format(i, array_edges[i][0], array_edges[i][1])) x.append(X_skeleton[array_edges_skeleton[i][0]]) y.append(Y_skeleton[array_edges_skeleton[i][0]]) z.append(Z_skeleton[array_edges_skeleton[i][0]]) x.append(X_skeleton[array_edges_skeleton[i][1]]) y.append(Y_skeleton[array_edges_skeleton[i][1]]) z.append(Z_skeleton[array_edges_skeleton[i][1]]) # The scalar parameter for each line s.append(array_edges_skeleton[i]) # This is the tricky part: in a line, each point is connected # to the one following it. We have to express this with the indices # of the final set of points once all lines have been combined # together, this is why we need to keep track of the total number of # points already created (index) #connections.append(np.vstack(array_edges[i]).T) connections.append(np.vstack( [np.arange(index, index + N - 1.5), np.arange(index + 1, index + N - .5)] ).T) index += 2 # Now collapse all positions, scalars and connections in big arrays x = np.hstack(x) y = np.hstack(y) z = np.hstack(z) s = np.hstack(s) #connections = np.vstack(connections) # Create the points src = mlab.pipeline.scalar_scatter(x, y, z, s) # Connect them src.mlab_source.dataset.lines = connections src.update() # The stripper filter cleans up connected lines #lines = mlab.pipeline.stripper(src) # display the set of lines mlab.pipeline.surface(src, colormap = 'Accent', line_width = 5, opacity = 1.0) # And choose a nice view #mlab.view(33.6, 106, 5.5, [0, 0, .05]) #mlab.roll(125) mlab.show()
def draw_rgbd_pointcloud(points3d,seg=None,seg_color=None,box3d_gt=None,box3d_pred=None,gt_color=(1,0,0),pred_color=(0,0,1),\ draw_text=False, text_scale=(1,1,1),color_list=None,camera_axis=True,save_name=None): if save_name is not None: mlab.options.offscreen = True figall = mlab.figure(figure=None, bgcolor=(0.4, 0.4, 0.4), fgcolor=None, engine=None, size=(640, 480)) if seg is not None: if seg_color is not None: nodes = mlab.points3d(points3d[:, 0], points3d[:, 1], points3d[:, 2], scale_factor=0.004, scale_mode='none', figure=figall) sc = tvtk.UnsignedCharArray() sc.from_array(seg_color) nodes.mlab_source.dataset.point_data.scalars = sc nodes.mlab_source.dataset.modified() else: nodes = mlab.points3d(points3d[:, 0], points3d[:, 1], points3d[:, 2], seg, scale_factor=0.004, scale_mode='none', colormap='gnuplot', figure=figall) else: nodes = mlab.points3d(points3d[:, 0], points3d[:, 1], points3d[:, 2], mode='point', figure=figall) # nodes.glyph.scale_mode = 'scale_by_vector' sc = tvtk.UnsignedCharArray() sc.from_array(points3d[:, 3:6]) nodes.mlab_source.dataset.point_data.scalars = sc nodes.mlab_source.dataset.modified() # camre_location = mlab.points3d(0, 0, 0,mode='point', figure=figall) #figall.scene.camera.position = [0, 0, 0] # camera_axis if camera_axis: mlab.quiver3d(0, 0, 0, 0, 0, 1, line_width=1, scale_factor=1, figure=figall) mlab.quiver3d(0, 0, 0, 0, 1, 0, line_width=1, scale_factor=1, figure=figall) mlab.quiver3d(0, 0, 0, 1, 0, 0, line_width=1, scale_factor=1, figure=figall) if box3d_gt is not None: draw_gt_boxes3d(box3d_gt, fig=figall,color=gt_color,draw_text=draw_text, text_scale=text_scale,color_list=color_list) if box3d_pred is not None: draw_gt_boxes3d(box3d_pred, fig=figall,color=pred_color,draw_text=draw_text, text_scale=text_scale,color_list=color_list) mlab.orientation_axes() #View 3dBox mlab.view(azimuth=180, elevation=190, focalpoint=[0.2,0, 0],distance=1.2, figure=figall) #View seg # mlab.view(azimuth=70, elevation=230, focalpoint=[0,0,0],distance=1, figure=figall) # # figall.scene.camera.position = [0.5094065699609567, -0.37414143582103904, -0.16310331266395872] # figall.scene.camera.focal_point = [-0.00448428033927202, -0.019859567181363474, 0.06892466310025992] # figall.scene.camera.view_angle = 30.0 # figall.scene.camera.view_up = [-0.1517561654542513, 0.37800573712006946, -0.9132807503451144] # figall.scene.camera.clipping_range = [0.0017838578690604107, 1.7838578690604106] # figall.scene.camera.compute_view_plane_normal() # figall.scene.render() if save_name is not None: mlab.savefig(filename=save_name,figure=figall) else: mlab.show() input
import numpy as np from tvtk.api import tvtk colors = np.random.randint(256, size=(100, 3)) an_image = tvtk.ImageData() an_image.number_of_scalar_components = 3 # an_image.scalar_type='unsigned_char' an_image.point_data.scalars = tvtk.UnsignedCharArray() an_image.point_data.scalars.from_array(colors) an_image.dimensions = np.array((10, 10, 1)) an_actor = tvtk.ImageActor() an_actor.input = an_image an_actor.interpolate = False ren = tvtk.Renderer() renWin = tvtk.RenderWindow() renWin.add_renderer(ren) ren.add_actor2d(an_actor) iren = tvtk.RenderWindowInteractor() iren.render_window = renWin iren.interactor_style = tvtk.InteractorStyleTrackballCamera() renWin.render() iren.start()
slice_num = int(input('Input single slice number(0-143): ')) # 3D Plot with radius data t = my.plot3d(x_plot, y_plot, z_plot, s, tube_radius=0.1, color=(1, 0, 0)) t.parent.parent.filter.vary_radius = 'vary_radius_by_scalar' # Assign posistion based on ratio of slice images to total points along the path j = int(slice_num * 11.805556666) # Reads the image data into and variable im = plt.imread('test' + str(slice_num) + '.png', format='png') * 255 colors = tvtk.UnsignedCharArray() # converts the PNG color data to variables Mayavi could understand colors.from_array(im.transpose((1, 0, 2)).reshape(-1, 3)) m_image = my.imshow(np.ones(im.shape[:2])) # Sets the color data on a 2D plane in 3D space m_image.actor.input.point_data.scalars = colors # position in 3D space of the slice m_image.actor.position = [x_axis[j], y_axis[j], z_axis[j]] if slice_type == '2': slice_num = input('Input slice numbers(0-143) separated by a space: ') slice_num_list = slice_num.split() # Error check user input while int(min([int(i) for i in slice_num_list])) < 0 or int( max([int(i) for i in slice_num_list])) > 143:
def el_add(elecs, color=(1., 0., 0.), msize=2, numbers=None, label_offset=-1.0, ambient=0.3261, specular=1, specular_power=16, diffuse=0.6995, interpolation='phong', elec_sizes=None, **kwargs): '''This function adds the electrode matrix [elecs] (nchans x 3) to the scene. Parameters ---------- elecs : array-like [nchans x 3] matrix of electrode coordinate values in 3D color : tuple (triplet) or numpy array Electrode color is either a triplet (r, g, b), or a numpy array with the same shape as [elecs] to plot one color per electrode msize : float size of the electrode. default = 2 label_offset : float how much to move the number labels out by (so not blocked by electrodes) **kwargs : any other keyword arguments that can be passed to points3d ''' # Get the current keyword arguments cur_kwargs = dict(color=color, scale_factor=msize, resolution=25) # Allow the user to override the default keyword arguments using kwargs cur_kwargs.update(kwargs) # plot the electrodes as spheres # If we have one color for each electrode, color them separately if type(color) is np.ndarray: cur_kwargs.pop('color') cur_kwargs.pop('scale_factor') if type(msize) is not np.ndarray: # ...then make it so #### msize = np.full(color.shape, msize / 20) # Why 1/20? Not sure, but that appears to be produce the same size # spheres as the old method. #### points = mlab.points3d(elecs[:, 0], elecs[:, 1], elecs[:, 2], **cur_kwargs) points.glyph.scale_mode = 'scale_by_vector' sc = tvtk.UnsignedCharArray() sc.from_array(color * 256) points.mlab_source.dataset.point_data.scalars = sc points.mlab_source.dataset.point_data.vectors = msize # if color.shape[0] == elecs.shape[0]: # # for e in np.arange(elecs.shape[0]): # # points = mlab.points3d(elecs[e,0], elecs[e,1], elecs[e,2], scale_factor = msize, # # color = tuple( color[e,:] ) , resolution=25) # unique_colors = np.array(list(set([tuple(row) for row in color]))) # for individual_color in unique_colors: # indices = np.where((color==individual_color).all(axis=1))[0] # cur_kwargs.update(color=tuple(individual_color)) # points = mlab.points3d(elecs[indices,0],elecs[indices,1],elecs[indices,2], **cur_kwargs) # else: # print('Warning: color array does not match size of electrode matrix') # Otherwise, use the same color for all electrodes else: points = mlab.points3d(elecs[:, 0], elecs[:, 1], elecs[:, 2], **cur_kwargs) # Set display properties points.actor.property.ambient = ambient points.actor.property.specular = specular points.actor.property.specular_power = specular_power points.actor.property.diffuse = diffuse points.actor.property.interpolation = interpolation #points.scene.light_manager.light_mode = 'vtk' if numbers is not None: for ni, n in enumerate(numbers): mayavi.mlab.text3d( elecs[ni, 0] + label_offset, elecs[ni, 1], elecs[ni, 2], str(n), orient_to_camera=True) # line_width=5.0, scale=1.5) return points, mlab