def plot_3D_data(x,y,z,fig_title): # Visualize it with mlab.surf from mayavi import mlab mlab.figure(bgcolor=(1, 1, 1)) surf = mlab.surf(z, colormap='cool') # Retrieve the LUT of the surf object. lut = surf.module_manager.scalar_lut_manager.lut.table.to_array() # The lut is a 255x4 array, with the columns representing RGBA # (red, green, blue, alpha) coded with integers going from 0 to 255. # We modify the alpha channel to add a transparency gradient lut[:, -1] = np.linspace(0, 255, 256) # and finally we put this LUT back in the surface object. We could have # added any 255*4 array rather than modifying an existing LUT. surf.module_manager.scalar_lut_manager.lut.table = lut mlab.title(fig_title,color=(0,0,0)) # mlab.axes(surf, color=(.7, .7, .7), ranges=(0, 1, 0, 1, 0, 1), xlabel='', ylabel='', zlabel='Probability', x_axis_visibility=False, z_axis_visibility=False) # We need to force update of the figure now that we have changed the LUT. #mlab.axes() mlab.orientation_axes() #mlab.xlabel('X') mlab.view(40, 85) #mlab.title(fig_title) mlab.draw() mlab.show()
def animateGIF(filename, prices): '''Creates a mayavi visualization of a pd DataFrame containing stock prices Then uses MoviePy to animate and save as a gif Inputs: prices => a pd DataFrame, w/ index: dates; columns: company names ''' #Imports mlab here to delay starting of mayavi engine until necessary from mayavi import mlab #Because of current mayavi requirements, replaces dates and company names with integers x_length, y_length = prices.shape xTime = np.array([list(xrange(x_length)),] * y_length).transpose() yCompanies = np.array([list(xrange(y_length)),] * x_length) #Sort indexed prices by total return on last date lastDatePrices = prices.iloc[-1] lastDatePrices.sort_values(inplace=True) sortOrder = lastDatePrices.index zPrices = prices[sortOrder] #Create mayavi2 object fig = mlab.figure(bgcolor=(.4,.4,.4)) vis = mlab.surf(xTime, yCompanies, zPrices) mlab.outline(vis) mlab.orientation_axes(vis) mlab.axes(vis, nb_labels=0, xlabel = 'Time', ylabel = 'Company', zlabel = 'Price') animation = mpy.VideoClip(make_frame, duration = 4).resize(1.0) animation.write_gif(filename, fps=20)
def animateGIF(filename, prices): '''Creates a mayavi visualization of a pd DataFrame containing stock prices Then uses MoviePy to animate and save as a gif Inputs: prices => a pd DataFrame, w/ index: dates; columns: company names ''' #Because of mayavi requirements, replace dates and company names with integers #until workaround is figured out x_length, y_length = prices.shape xTime = np.array([list(xrange(x_length)),] * y_length).transpose() yCompanies = np.array([list(xrange(y_length)),] * x_length) #Sort indexed prices by total return on last date lastDatePrices = prices.iloc[-1] lastDatePrices.sort_values(inplace=True) sortOrder = lastDatePrices.index zPrices = prices[sortOrder] #Create mayavi2 object dims = xTime.shape fig = mlab.figure(bgcolor=(.4,.4,.4)) vis = mlab.surf(xTime, yCompanies, zPrices) mlab.outline(vis) mlab.orientation_axes(vis) mlab.axes(vis, nb_labels=0, xlabel = 'Time', ylabel = 'Company', zlabel = 'Price') # duration = 2 #Duration of the animation in seconds (will loop) animation = mpy.VideoClip(make_frame, duration = 4).resize(1.0) # animation.write_videofile('prototype_animation.mp4', fps=20) animation.write_gif(filename, fps=20)
def plot(self): """Plots the geometry using the package Mayavi.""" print('\nPlot the three-dimensional geometry ...') from mayavi import mlab x_init = self.gather_coordinate('x', position='initial') y_init = self.gather_coordinate('y', position='initial') z_init = self.gather_coordinate('z', position='initial') x = self.gather_coordinate('x') y = self.gather_coordinate('y') z = self.gather_coordinate('z') figure = mlab.figure('body', size=(600, 600)) figure.scene.disable_render = False same = (numpy.allclose(x, x_init, rtol=1.0E-06) and numpy.allclose(y, y_init, rtol=1.0E-06) and numpy.allclose(z, z_init, rtol=1.0E-06)) if not same: mlab.points3d(x_init, y_init, z_init, name='initial', scale_factor=0.01, color=(0, 0, 1)) mlab.points3d(x, y, z, name='current', scale_factor=0.01, color=(1, 0, 0)) mlab.axes() mlab.orientation_axes() mlab.outline() figure.scene.disable_render = True mlab.show()
def plot_points(pc_sequence, scale_factor): fig = mlab.figure(figure=None, bgcolor=(0, 0, 0), fgcolor=(1, 1, 1), engine=None, size=(1600, 1000)) for idx, pc in enumerate(pc_sequence): mlab.points3d(pc[:, 0], pc[:, 1], pc[:, 2], color=COLORS_LIST[idx], scale_factor=scale_factor, figure=fig, mode=MODE) # draw a line between 2 PC pc1 = pc_sequence[0] pc1_sf = pc_sequence[1] draw_line(pc1, pc1_sf) # show all PCs mlab.view( 90, # azimuth 150, # elevation 50, # distance [0, -1.4, 18], # focalpoint roll=0) mlab.orientation_axes() mlab.show()
def visualize_plane(pt, plane, show=False): """ Visualize the RANSAC PLANE (4-tuple) fit to PT (nx3 array). Also draws teh normal. """ # # plot the point-cloud: if show and mym.gcf(): mym.clf() # plot the plane: plane_eq = '%f*x+%f*y+%f*z+%f' % tuple(plane.tolist()) m, M = np.percentile(pt, [10, 90], axis=0) implicit_plot(plane_eq, (m[0], M[0], m[1], M[1], m[2], M[2])) # plot surface normal: mu = np.percentile(pt, 50, axis=0) mu_z = -(mu[0] * plane[0] + mu[1] * plane[1] + plane[3]) / plane[2] mym.quiver3d(mu[0], mu[1], mu_z, plane[0], plane[1], plane[2], scale_factor=0.3) if show: mym.view(180, 180) mym.orientation_axes() mym.show(True)
def visualizePrices(prices): '''Creates a mayavi visualization of a pd DataFrame containing stock prices Inputs: prices => a pd DataFrame, w/ index: dates; columns: company names ''' #Imports mlab here to delay starting of mayavi engine until necessary from mayavi import mlab #Because of current mayavi requirements, replaces dates and company names with integers x_length, y_length = prices.shape xTime = np.array([list(xrange(x_length)),] * y_length).transpose() yCompanies = np.array([list(xrange(y_length)),] * x_length) #Sort indexed prices by total return on last date lastDatePrices = prices.iloc[-1] lastDatePrices.sort_values(inplace=True) sortOrder = lastDatePrices.index zPrices = prices[sortOrder] #Create mayavi2 object fig = mlab.figure(bgcolor=(.4,.4,.4)) vis = mlab.surf(xTime, yCompanies, zPrices) mlab.outline(vis) mlab.orientation_axes(vis) #mlab.title('S&P 500 Market Data Visualization', size = .25) mlab.axes(vis, nb_labels=0, xlabel = 'Time', ylabel = 'Company', zlabel = 'Price') mlab.show()
def render_body(self): from mayavi import mlab body = self.to_body() mask, bounds = body.get_seeded_component( CONFIG.postprocessing.closing_shape) fig = mlab.figure(size=(1280, 720)) if self.target is not None: target_grid = mlab.pipeline.scalar_field(self.target) target_grid.spacing = CONFIG.volume.resolution target_grid = mlab.pipeline.iso_surface(target_grid, contours=[0.5], color=(1, 0, 0), opacity=0.1) grid = mlab.pipeline.scalar_field(mask) grid.spacing = CONFIG.volume.resolution mlab.pipeline.iso_surface(grid, color=(0, 1, 0), contours=[0.5], opacity=0.6) mlab.orientation_axes(figure=fig, xlabel='Z', zlabel='X') mlab.view(azimuth=45, elevation=30, focalpoint='auto', roll=90, figure=fig) mlab.show()
def mayavi_animate_surface_trajectory(smesh: np.ndarray, trajectory: np.ndarray = None, abs_speed: np.ndarray = None): mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(800, 800)) mlab.clf() mlab.mesh(*smesh, opacity=0.3, colormap='cool') # , color=(0.1, 0.1, 0.6) mlab.mesh(*smesh, opacity=0.1, color=(0, 0, 0), representation='wireframe') solid_point = mlab.points3d(*trajectory[0], color=(1, 0, 0), scale_factor=0.05) mlab.plot3d(trajectory[:, xi], trajectory[:, yi], trajectory[:, zi], abs_speed, tube_radius=0.01) mlab.orientation_axes() @mlab.animate(delay=10) def anim(): while True: for i, (x, y, z) in enumerate(trajectory): solid_point.mlab_source.set(x=x, y=y, z=z) # mlab.savefig("./temp/f{0}.png".format(i)) yield return anim
def update_scene(): # clear figure mlab.clf(fig1) # Plot new data feature points = all_points[file_i] # Rescale points for visu points = (points * 1.5 + np.array([1.0, 1.0, 1.0])) * 50.0 # Show point clouds colorized with activations activations = mlab.points3d(points[:, 0], points[:, 1], points[:, 2], points[:, 2], scale_factor=3.0, scale_mode='none', figure=fig1) # New title mlab.title(str(file_i), color=(0, 0, 0), size=0.3, height=0.01) text = '<--- (press g for previous)' + 50 * ' ' + '(press h for next) --->' mlab.text(0.01, 0.01, text, color=(0, 0, 0), width=0.98) mlab.orientation_axes() return
def plot_camera_sequence(C_list): fig = mlab.figure(figure=None, bgcolor=(0, 0, 0), fgcolor=(1, 1, 1), engine=None, size=(1600, 1000)) mlab.points3d(C_list[0][0], C_list[0][1], C_list[0][2], color=(1, 0, 0), scale_factor=0.008, figure=fig, mode='cube') for i in range(1, len(C_list)): # camera center center = C_list[i] mlab.points3d(center[0], center[1], center[2], color=(0, 1, 0), scale_factor=0.005, figure=fig, mode='sphere') # show all PCs mlab.view( 90, # azimuth 150, # elevation 50, # distance [0, -1.4, 18], # focalpoint roll=0) mlab.orientation_axes() mlab.show()
def update_scene(): # clear figure mlab.clf(fig1) # Plot new data feature points = all_points[file_i] responses = all_responses[file_i] min_response, max_response = np.min(responses), np.max(responses) responses = (responses - min_response) / (max_response - min_response) # Rescale points for visu points = (points * 1.5 / config.in_radius + np.array([1.0, 1.0, 1.0])) * 50.0 # Show point clouds colorized with activations activations = mlab.points3d(points[:, 0], points[:, 1], points[:, 2], responses, scale_factor=3.0, scale_mode='none', vmin=0.1, vmax=0.9, figure=fig1) # New title mlab.title(feature_files[file_i], color=(0, 0, 0), size=0.3, height=0.01) text = '<--- (press g for previous)' + 50 * ' ' + '(press h for next) --->' mlab.text(0.01, 0.01, text, color=(0, 0, 0), width=0.98) mlab.orientation_axes() return
def visualizePrices(prices): '''Creates a mayavi visualization of a pd DataFrame containing stock prices Inputs: prices => a pd DataFrame, w/ index: dates; columns: company names ''' #Because of mayavi requirements, replace dates and company names with integers #until workaround is figured out x_length, y_length = prices.shape xTime = np.array([list(xrange(x_length)),] * y_length).transpose() yCompanies = np.array([list(xrange(y_length)),] * x_length) #Sort indexed prices by total return on last date lastDatePrices = prices.iloc[-1] lastDatePrices.sort_values(inplace=True) sortOrder = lastDatePrices.index zPrices = prices[sortOrder] #Create mayavi2 object dims = xTime.shape fig = mlab.figure(bgcolor=(.4,.4,.4)) vis = mlab.surf(xTime, yCompanies, zPrices) mlab.outline(vis) mlab.orientation_axes(vis) #mlab.title('S&P 500 Market Data Visualization', size = .25) mlab.axes(vis, nb_labels=0, xlabel = 'Time', ylabel = 'Company', zlabel = 'Price') #Functionality to be added: # cursor3d = mlab.points3d(0., 0., 0., mode='axes', # color=(0, 0, 0), # scale_factor=20) #picker = fig.on_mouse_pick(picker_callback) mlab.show()
def show_lung(event): if not button4.get_status()[0]: tmp_3d = pred_st_tmp.copy() threshold = slider1.val tmp_3d[tmp_3d < threshold] = 0 tmp_3d[tmp_3d >= threshold] = 1 mlab.clf() mlab.contour3d(tmp_3d, colormap = 'hot', opacity = 1.0, vmin = 0, vmax = 1) if button1.get_status()[0]: mlab.contour3d(mask, colormap = 'Greys', opacity = 0.1, vmin = 0, vmax = 150) elif button4.get_status()[0]: pred_class = pred_VGG(im_ct, pred_st, res, VGG_model_path) pred_class = zoom(pred_class.copy(), zoom = zoom_fac, order = 0) mlab.clf() print(np.unique(pred_class)[1:]) for i in np.unique(pred_class): if i == 0: continue tmp = pred_class.copy() tmp[pred_class != i] = 0 mlab.contour3d(tmp, colormap = 'OrRd', color = tuple(colors[int(round((9/5)*i)),:]), vmin = 1, vmax = 5) mlab.scalarbar(orientation = 'vertical', nb_labels = 9, label_fmt='%.1f') if button1.get_status()[0]: mlab.contour3d(mask, colormap = 'Greys', opacity = 0.1, vmin = 0, vmax = 150) mlab.orientation_axes(xlabel = 'z', ylabel = 'y', zlabel = 'x')
def plot(view="iso"): redis = Redis() px, py, pz = (redis.get('x'), redis.get('y'), redis.get('z')) if None in (px, py, pz): raise UntrainedException("You must train first!") x, y, z = (pickle.loads(px), pickle.loads(py), pickle.loads(pz)) fig = mlab.figure(size=(800, 600)) # these options could help on certain platforms # mlab.options.offscreen = True # fig.scene.off_screen_rendering = True # Define the points in 3D space # including color code based on Z coordinate. mlab.points3d(x, y, z, y) xlabel = "day of week" ylabel = "# logins" zlabel = "hour" mlab.axes(xlabel=xlabel, ylabel=ylabel, zlabel=zlabel, ranges=[0, 6, min(y), max(y), 0, 23]) mlab.scalarbar(title=ylabel, nb_labels=10, orientation='vertical') mlab.orientation_axes(xlabel=xlabel, ylabel=ylabel, zlabel=zlabel) views = {"xp": fig.scene.x_plus_view, "xm": fig.scene.x_minus_view, "yp": fig.scene.y_plus_view, "ym": fig.scene.y_minus_view, "zp": fig.scene.z_plus_view, "zm": fig.scene.z_minus_view, "iso": fig.scene.isometric_view } try: views[view]() except KeyError as e: raise PlotException("Invalid view option: %s" % view) # can't save directly to stringIO, so have to go through a file fig.scene.save_png('fig.png') # mayavi doesn't seem to play well with celery on some platforms and # doesn't shut down properly - probably because it's in a background thread # on centos, celery just throws a WorkerLostError after a couple of requests. # fig.remove() # fig.parent.close_scene(fig) # this doesn't work on centos: # mlab.close() with open('fig.png', 'rb') as f: buf = f.read() return buf
def draw_dots3d(dots, edges, fignum, clear=True, title='', size=(1024, 768), graph_colormap='viridis', bgcolor=(1, 1, 1), node_color=(0.3, 0.65, 0.3), node_size=0.01, edge_color=(0.3, 0.3, 0.9), edge_size=0.003, text_size=0.14, text_color=(0, 0, 0), text_coords=[0.84, 0.75], text={}, title_size=0.3, angle=get_ra()): # https://stackoverflow.com/questions/17751552/drawing-multiplex-graphs-with-networkx # numpy array of x, y, z positions in sorted node order xyz = shrink_to_3d(dots) if fignum == 0: mlab.figure(fignum, bgcolor=bgcolor, fgcolor=text_color, size=size) # Mayavi is buggy, and following code causes sockets leak. #if mlab.options.offscreen: # mlab.figure(fignum, bgcolor=bgcolor, fgcolor=text_color, size=size) #elif fignum == 0: # mlab.figure(fignum, bgcolor=bgcolor, fgcolor=text_color, size=size) if clear: mlab.clf() # the x,y, and z co-ordinates are here # manipulate them to obtain the desired projection perspective pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2], scale_factor=node_size, scale_mode='none', color=node_color, #colormap=graph_colormap, resolution=20, transparent=False) mlab.text(text_coords[0], text_coords[1], '\n'.join(['{} = {}'.format(n, v) for n, v in text.items()]), width=text_size) if clear: mlab.title(title, height=0.95) mlab.roll(next(angle)) mlab.orientation_axes(pts) mlab.outline(pts) """ for i, (x, y, z) in enumerate(xyz): label = mlab.text(x, y, str(i), z=z, width=text_size, name=str(i), color=text_color) label.property.shadow = True """ pts.mlab_source.dataset.lines = edges tube = mlab.pipeline.tube(pts, tube_radius=edge_size) mlab.pipeline.surface(tube, color=edge_color)
def draw_lidar(pc, color=None, fig=None, bgcolor=(0,0,0), pts_scale=1, pts_mode='point', pts_color=None, display=False): ''' Draw lidar points Args: pc: numpy array (n,3) of XYZ color: numpy array (n) of intensity or whatever fig: mayavi figure handler, if None create new one otherwise will use it Returns: fig: created or used fig ''' if fig is None: fig = mlab.figure(figure=None, bgcolor=bgcolor, fgcolor=None, engine=None, size=(1600, 1000)) if color is None: color = pc[:,2] mlab.points3d(pc[:,0], pc[:,1], pc[:,2], color, color=pts_color, mode=pts_mode, colormap = 'gnuplot', scale_factor=pts_scale, figure=fig) #draw origin mlab.points3d(0, 0, 0, color=(1,1,1), mode='sphere', scale_factor=0.2) #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) # draw fov (todo: update to real sensor spec.) # fov=np.array([ # 45 degree # [20., 20., 0.,0.], # [20.,-20., 0.,0.], # ],dtype=np.float64) # # mlab.plot3d([0, fov[0,0]], [0, fov[0,1]], [0, fov[0,2]], color=(1,1,1), tube_radius=None, line_width=1, figure=fig) # mlab.plot3d([0, fov[1,0]], [0, fov[1,1]], [0, fov[1,2]], color=(1,1,1), tube_radius=None, line_width=1, figure=fig) # # # draw square region # TOP_Y_MIN=-20 # TOP_Y_MAX=20 # TOP_X_MIN=0 # TOP_X_MAX=40 # TOP_Z_MIN=-2.0 # TOP_Z_MAX=0.4 # # x1 = TOP_X_MIN # x2 = TOP_X_MAX # y1 = TOP_Y_MIN # y2 = TOP_Y_MAX # mlab.plot3d([x1, x1], [y1, y2], [0,0], color=(0.5,0.5,0.5), tube_radius=0.1, line_width=1, figure=fig) # mlab.plot3d([x2, x2], [y1, y2], [0,0], color=(0.5,0.5,0.5), tube_radius=0.1, line_width=1, figure=fig) # mlab.plot3d([x1, x2], [y1, y1], [0,0], color=(0.5,0.5,0.5), tube_radius=0.1, line_width=1, figure=fig) # mlab.plot3d([x1, x2], [y2, y2], [0,0], color=(0.5,0.5,0.5), tube_radius=0.1, line_width=1, figure=fig) mlab.orientation_axes() mlab.view(azimuth=180, elevation=70, focalpoint=[ 12.0909996 , -1.04700089, -2.03249991], distance=62.0, figure=fig) return fig
def fill_render(self, model, save_movie=True, **kwargs): from mayavi import mlab body = self.to_body() mask = body.mask fig = mlab.figure(size=(1280, 720)) if self.target is not None: target_grid = mlab.pipeline.scalar_field(np.transpose(self.target)) target_grid.spacing = np.flipud(CONFIG.volume.resolution) target_grid = mlab.pipeline.iso_surface(target_grid, contours=[0.5], color=(1, 0, 0), opacity=0.1) grid = mlab.pipeline.scalar_field(np.transpose(mask.astype(np.int32))) grid.spacing = np.flipud(CONFIG.volume.resolution) contour = mlab.pipeline.iso_surface(grid, color=(0, 1, 0), contours=[0.5], opacity=0.6) contour.actor.property.backface_culling = True grid = contour.mlab_source mlab.orientation_axes(figure=fig) mlab.view(azimuth=45, elevation=60, focalpoint='auto', figure=fig) fill_generator = self.fill(model, generator=True, **kwargs) FRAMES_PER_MOVE = 2 FPS = 60.0 ORBIT_RATE = 0.125 @mlab.animate(delay=int(1000.0/FPS), ui=True) def animate(): try: for _, _ in fill_generator: body = self.to_body() mask = body.mask grid.set(scalars=np.transpose(mask.astype(np.int32))) for _ in range(FRAMES_PER_MOVE): view = list(mlab.view(figure=fig)) view[0] = (view[0] + ORBIT_RATE * 360.0 / FPS) % 360.0 mlab.view(azimuth=view[0], elevation=view[1], focalpoint='auto') fig.scene.render() # fig.scene.movie_maker.animation_step() yield except Region.EarlyFillTermination: pass fig.scene.movie_maker.record = False fig.scene.movie_maker.animation_stop() if save_movie: fig.scene.movie_maker.record = True a = animate() # noqa mlab.show()
def visualize_space(self): x, y, z = np.mgrid[self.shape['x'][0]:self.shape['x'][1]:self.pos_resolution, self.shape['y'][0]:self.shape['y'][1]:self.pos_resolution, self.shape['theta'][0] * self.theta_factor: self.shape['theta'][1] * self.theta_factor:complex(0, self.theta_size / float( self.theta_resolution))] mlab.figure() mlab.contour3d(x, y, z, self.space, opacity=0.7) # mlab.points3d(x, y, z, self.space_boundary) mlab.axes(xlabel="x", ylabel="y", zlabel="theta") mlab.orientation_axes(xlabel="x", ylabel="y", zlabel="theta")
def decorate_figure(f, viz, distance=400, focal_point=(150, 40, 0)): mlab.view(0, 0, distance, np.array(focal_point), figure=f) mlab.orientation_axes(viz.src, figure=f) axes = mlab.axes(viz.src, figure=f) axes.label_text_property.trait_set( font_family='times', italic=False ) axes.title_text_property.font_family = 'times' axes.axes.trait_set( x_label='x', y_label='y', z_label='z' )
def decorate_figure(f, viz): mlab.view(0, 0, 140, np.array([50., 5., 0.]), figure=f) mlab.orientation_axes(viz.src, figure=f) axes = mlab.axes(viz.src, figure=f) axes.label_text_property.trait_set( font_family='times', italic=False ) axes.title_text_property.font_family = 'times' axes.axes.trait_set( x_label='x', y_label='y', z_label='z' )
def plot3d(self, pnt_blocks=[], pnt_masks=[], pnt_pegs=[], pnt_grasping1=[], pnt_grasping2=[]): pnt_blocks = np.array(pnt_blocks) pnt_masks = np.array(pnt_masks) pnt_pegs = np.array(pnt_pegs) pnt_grasping1 = np.array(pnt_grasping1) pnt_grasping2 = np.array(pnt_grasping2) mlab.figure("FLS Peg Transfer", fgcolor=(0., 0., 0.), bgcolor=(1, 1, 1), size=(1200, 900)) # black background for pnt_mask in pnt_masks: if pnt_mask != []: mlab.points3d(pnt_mask[:, 0], pnt_mask[:, 1], pnt_mask[:, 2], color=(0.0, 1.0, 0.0), scale_factor=0.3) # green on masks for pnt_block in pnt_blocks: if pnt_block != []: mlab.points3d(pnt_block[:, 0], pnt_block[:, 1], pnt_block[:, 2], color=(1.0, 1.0, 0.0), scale_factor=.5) # yellow on blocks if pnt_grasping1 != []: mlab.points3d(pnt_grasping1[:, 0], pnt_grasping1[:, 1], pnt_grasping1[:, 2], color=(1.0, 0.0, 0.0), scale_factor=1.7) # red on grasping point if pnt_grasping2 != []: mlab.points3d(pnt_grasping2[:, 0], pnt_grasping2[:, 1], pnt_grasping2[:, 2], color=(0.0, 0.0, 1.0), scale_factor=1.7) # blue on grasping point if pnt_pegs != []: mlab.points3d(pnt_pegs[:, 0], pnt_pegs[:, 1], pnt_pegs[:, 2], color=(0.0, 0.0, 0.0), scale_factor=3.2) # black on pegs # mlab.axes(xlabel='x', ylabel='y', zlabel='z', z_axis_visibility=False) mlab.orientation_axes() # mlab.outline(color=(.7, .7, .7)) mlab.view(azimuth=180, elevation=180) mlab.show()
def update_3Dimage(self): self.azimuth = self.slider3.get() self.elevation = self.slider4.get() mlab.figure() mlab.contour3d(self.gt_data, colormap='gray') mlab.axes(xlabel='X', ylabel='Y', zlabel='Z') #Display axis mlab.orientation_axes() mlab.view(azimuth=self.azimuth, elevation=self.elevation, distance=self.distance, focalpoint=self.focalpoint) mlab.savefig('gt_3d.png') mlab.close() mlab.figure() mlab.contour3d(self.rw_data[:, :, :, self.img_timestep], colormap='gray') mlab.axes(xlabel='X', ylabel='Y', zlabel='Z') #Display axis mlab.orientation_axes() mlab.view(azimuth=self.azimuth, elevation=self.elevation, distance=self.distance, focalpoint=self.focalpoint) mlab.savefig('segm_3d.png') mlab.close() # mlab.figure() # mlab.flow(self.flow_data1,self.flow_data2,self.flow_data3) # mlab.axes(xlabel='X', ylabel='Y', zlabel='Z') #Display axis # mlab.orientation_axes() # mlab.view(azimuth=self.azimuth, elevation=self.elevation) # mlab.savefig('flow_3d.png') # mlab.close() png3dgtimage = Image.open('.//gt_3d.png').resize( size=(self.x_size * 3, self.y_size * 3), resample=Image.BICUBIC) self.gtimg3D = ImageTk.PhotoImage(image=png3dgtimage) self.canvas4.create_image(0, 0, anchor=NW, image=self.gtimg3D) png3dsegimage = Image.open('.//segm_3d.png').resize( size=(self.x_size * 3, self.y_size * 3), resample=Image.BICUBIC) self.segimg3D = ImageTk.PhotoImage(image=png3dsegimage) self.canvas5.create_image(0, 0, anchor=NW, image=self.segimg3D) # png3dflowimage = Image.open('.//flow_3d.png').resize(size=(self.x_size*3,self.y_size*3),resample = Image.BICUBIC ) # self.flowimg3D = ImageTk.PhotoImage(image=png3dflowimage) # self.canvas6.create_image(0,0, anchor=NW, image=self.flowimg3D) return
def plot_contours_contourlist(x, y, z, s, contourlist=[.9], logplot=False, logrange=log(1e3), **kwargs): #plot specified contours of set s, with opacity ranging from 0 for smallest to #1 for largest values if (logplot): maxval = log(abs(s).max()) minval = maxval - logrange logs = log(abs(s)) logs = where(logs > minval, logs - minval, 0) logs /= logs.max() src = mlab.pipeline.scalar_field(x, y, z, logs) else: maxval = abs(s).max() src = mlab.pipeline.scalar_field(x, y, z, abs(s) / maxval) #add phase of inparray as an additional array (tricky!) #see example at #http://docs.enthought.com/mayavi/mayavi/auto/example_atomic_orbital.html#example-atomic-orbital src.image_data.point_data.add_array(angle(s).T.ravel()) #give it the name 'angle' src.image_data.point_data.get_array(1).name = 'angle' #make sure the dataset is up to date src.image_data.point_data.update() abscontlist = [] for val in contourlist: mlab.pipeline.set_active_attribute(src, point_scalars='scalar') tmpcontour = mlab.pipeline.iso_surface(src, contours=[val], opacity=val) #.2+val*.6) #mlab.pipeline.set_active_attribute(src, point_scalars='angle') #surf2=mlab.pipeline.iso_surface(tmpcontour, colormap='hsv', vmin=-pi, vmax=pi) # surf2=mlab.pipeline.set_active_attribute(tmpcontour, point_scalars='angle') # mlab.pipeline.surface(surf2, colormap='hsv', vmin=-pi, vmax=pi) abscontlist.append(tmpcontour) # surf=mlab.pipeline.iso_surface(src,contours=contourlist, opacity=.1) # mlab.pipeline.set_active_attribute(surf, point_scalars='angle') # mlab.pipeline.iso_surface(surf,colormap='hsv', vmin=-pi, vmax=pi) mlab.colorbar(title='norm', orientation='vertical', nb_labels=3) #mlab.axes()#cube-shaped axes surrounding data mlab.orientation_axes() #mlab.show() return src
def tf_main_points_in_hull(): import os import numpy as np import sys BASE_DIR = os.path.dirname(os.path.abspath(__file__)) print(BASE_DIR) sys.path.append(os.path.join(BASE_DIR,'../utils')) import utils def get_2048_points(depth): num_point = depth.shape[0] pc_in_box_fov = depth[:, :3] if num_point > 2048: choice = np.random.choice(depth.shape[0], 2048, replace=False) pc_in_box_fov = depth[choice, :3] return pc_in_box_fov pc_in_box_fov = get_2048_points(np.loadtxt('/Users/jiangyy/projects/VoteNet/utils/test_datas/000001.txt')) calib = utils.SUNRGBD_Calibration("/Users/jiangyy/projects/VoteNet/utils/test_datas/000001.txt.calib") objects = utils.read_sunrgbd_label("/Users/jiangyy/projects/VoteNet/utils/test_datas/000001.txt.label") box_list = [] for obj_idx in range(len(objects)): obj = objects[obj_idx] box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib) box_list.append(box3d_pts_3d) tf_points1 = tf.Variable(pc_in_box_fov, dtype=tf.float32) # print(box_list) e = np.array(box_list) tf_boxes1 = tf.Variable(np.array(box_list), dtype=tf.float32) tf_points2 = tf.expand_dims(tf_points1, axis=0) tf_boxes2 = tf.expand_dims(tf_boxes1, axis=0) tf_idx_in_hull = tf_points_in_hull(tf_points2, tf_boxes2) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) tf_idx_in_hull_1 = sess.run(tf_idx_in_hull) print(tf_idx_in_hull_1.shape) tf_idx_in_hull_2 = tf_idx_in_hull_1.sum(axis=2) import mayavi.mlab as mlab fig = mlab.figure(figure=None, bgcolor=(0.4, 0.4, 0.4), fgcolor=None, engine=None, size=(1000, 500)) mlab.points3d(pc_in_box_fov[:, 0], pc_in_box_fov[:, 1], pc_in_box_fov[:, 2], tf_idx_in_hull_2[0], mode='point', colormap='gnuplot', scale_factor=1, figure=fig) mlab.points3d(0, 0, 0, color=(1, 1, 1), mode='sphere', scale_factor=0.2, figure=fig) sys.path.append(os.path.join(BASE_DIR, '../mayavi')) from viz_util import draw_gt_boxes3d draw_gt_boxes3d(box_list, fig, color=(1, 0, 0)) mlab.orientation_axes() mlab.show()
def _process_series(self, series): mlab.clf(self._fig) self._init_cyclers() cm = iter(self.colormaps) for i, s in enumerate(series): if s.is_3Dline: x, y, z = s.get_data() u = s.discretized_var mlab.plot3d( x, y, z, u, color=None if self._use_cm else next(self._cl), figure=self._fig, tube_radius=0.05, colormap=next(cm), ) elif s.is_3Dsurface: mlab.mesh(*s.get_data(), color=None if self._use_cm else next(self._cl), figure=self._fig, colormap=next(cm), representation="wireframe" if self._kwargs.get( "wireframe", False) else "surface") else: raise NotImplementedError("{} is not supported by {}\n".format( type(s), type(self).__name__) + "Mayavi only supports 3D plots.") if self.grid: mlab.axes( xlabel="", ylabel="", zlabel="", x_axis_visibility=True, y_axis_visibility=True, z_axis_visibility=True, ) mlab.outline() xl = self.xlabel if self.xlabel else "x" yl = self.ylabel if self.ylabel else "y" zl = self.zlabel if self.zlabel else "z" mlab.orientation_axes(xlabel=xl, ylabel=yl, zlabel=zl) if self.title: mlab.title(self.title, figure=self._fig, size=0.5)
def update_scene(): mlab.clf(fig1) # 清空figure vis_points = points[file_i] # 取出要显示的点云 vis_points = (vis_points * 1.5 + np.array([1.0, 1.0, 1.0])) * 50 # 对点云进行缩放 # show point clouds colorized with activations activations = mlab.points3d(vis_points[:, 0], vis_points[:, 1], vis_points[:, 2], vis_points[:, 2], scale_factor=3.0, scale_mode='none', figure=fig1) mlab.title(str(file_i), color=(0, 0, 0), size=0.3, height=0.01) text = '<--- (press g for previous' + 50 * '' + '(press h for next)---->' mlab.text(0.01, 0.01, text, color=(0, 0, 0), width=0.98) mlab.orientation_axes() return
def viz_regions(img, xyz, seg, planes, labels): """ img,depth,seg are images of the same size. visualizes depth masks for top NOBJ objects. """ # plot the RGB-D point-cloud: su.plot_xyzrgb(xyz.reshape(-1, 3), img.reshape(-1, 3)) # plot the RANSAC-planes at the text-regions: for i, l in enumerate(labels): mask = seg == l xyz_region = xyz[mask, :] su.visualize_plane(xyz_region, np.array(planes[i])) mym.view(180, 180) mym.orientation_axes() mym.show(True)
def draw_scenes(points, gt_boxes=None, ref_boxes=None, ref_scores=None, ref_labels=None, show_intensity=True, bgcolor=(0.,0.,0.), scores=None, dist_mask=None, num_pts_mask=None, det_ignore_mask=None): num_gts=num_dets=0 if not isinstance(points, np.ndarray): points = points.cpu().numpy() if ref_boxes is not None and not isinstance(ref_boxes, np.ndarray): ref_boxes = ref_boxes.cpu().numpy() if gt_boxes is not None and not isinstance(gt_boxes, np.ndarray): gt_boxes = gt_boxes.cpu().numpy() if ref_scores is not None and not isinstance(ref_scores, np.ndarray): ref_scores = ref_scores.cpu().numpy() if ref_labels is not None and not isinstance(ref_labels, np.ndarray): ref_labels = ref_labels.cpu().numpy() fig = visualize_pts(points, bgcolor=bgcolor, show_intensity=show_intensity, size=(1000, 600)) # fig = draw_multi_grid_range(fig, bv_range=(-5, -20, 40, 20)) if gt_boxes is not None: num_gts = len(gt_boxes) corners3d = boxes_to_corners_3d(gt_boxes) fig = draw_corners3d(corners3d, fig=fig, color=(0, 0, 1), max_num=100, dist_mask=dist_mask, num_pts_mask=num_pts_mask) if ref_boxes is not None and len(ref_boxes) > 0: num_dets = len(ref_boxes) ref_corners3d = boxes_to_corners_3d(ref_boxes) if ref_labels is None: fig = draw_corners3d(ref_corners3d, fig=fig, color=(0, 1, 0), cls=ref_scores, max_num=100, scores=scores, dist_mask=det_ignore_mask) else: for k in range(ref_labels.min(), ref_labels.max() + 1): cur_color = tuple(box_colormap[k % len(box_colormap)]) mask = (ref_labels == k) fig = draw_corners3d(ref_corners3d[mask], fig=fig, color=cur_color, cls=ref_scores[mask], max_num=100) tit = mlab.title('{} predictions, {} ground truth objects'.format(num_dets, num_gts), size=0.2, height=0.05) # mlab.text3d(, b[4,1], b[4,2], '%.2f'%scores[n], scale=(0.3,0.3,0.3), color=color, figure=fig) # tit.actor.text_scale_mode = True tit.actor.text_property.font_family = 'times' tit.actor.text_property.font_size = 5 tit.actor.text_property.italic = False # mlab.view(azimuth=-180, elevation=180.0, distance=40.0, roll=90.0, focalpoint=(0, 0, 0)) mlab.view(azimuth=-180, elevation=5, roll=90.0, distance=40.0, focalpoint=(0, 0, 0)) mlab.orientation_axes() return fig
def classify(event): if button4.get_status()[0]: pred_class = pred_VGG(im_ct, pred_st, res, VGG_model_path) pred_class = zoom(pred_class.copy(), zoom = zoom_fac, order = 0) mlab.clf() print(np.unique(pred_class)[1:]) for i in np.unique(pred_class): if i == 0: continue tmp = pred_class.copy() tmp[pred_class != i] = 0 mlab.contour3d(tmp, colormap = 'OrRd', color = tuple(colors[int(round((9/5)*i)),:]), vmin = 1, vmax = 5) mlab.scalarbar(orientation = 'vertical', nb_labels = 9, label_fmt='%.1f') if button1.get_status()[0]: mlab.contour3d(mask, colormap = 'Greys', opacity = 0.1, vmin = 0, vmax = 150) mlab.orientation_axes(xlabel = 'z', ylabel = 'y', zlabel = 'x') elif not button4.get_status()[0]: # re-plot without classify tmp_thr = pred_st[int(slider2.val)].copy() tmp_3d = pred_st_tmp.copy() threshold = slider1.val tmp_thr[tmp_thr < threshold] = 0 tmp_thr[tmp_thr >= threshold] = 1 tmp_3d[tmp_3d < threshold] = 0 tmp_3d[tmp_3d >= threshold] = 1 mlab.clf() mlab.contour3d(tmp_3d, colormap = 'hot', opacity = 1.0, vmin = 0, vmax = 1) if button1.get_status()[0]: mlab.contour3d(mask, colormap = 'Greys', opacity = 0.1, vmin = 0, vmax = 150) ax.clear() ax.imshow(im_ct[int(slider2.val), :, :], cmap = 'gray') #ax.imshow(tmp_thr, cmap = 'inferno', vmin = 0, vmax = 1, alpha = 0.3) ax.imshow(tmp_thr, cmap = 'gnuplot', vmin = 0, vmax = 2, alpha = 0.3) ax.set_axis_off() f.suptitle('slice '+str(slider2.val)) f.canvas.draw_idle()
def plot_xyzrgb(xyz, rgb, show=False): """ xyz : nx3 float rgb : nx3 uint8 Plots a RGB-D point-cloud in mayavi. """ rgb_s = LUT_RGB.rgb2scalar(rgb) pts_glyph = mym.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2], rgb_s, mode='point') LUT_RGB.set_rgb_lut(pts_glyph) if show: mym.view(180, 180) mym.orientation_axes() mym.show()
def draw_voxel(voxel_coords, is_grid=False, is_axis = True, fig=None): pxs=voxel_coords[:,0] pys=voxel_coords[:,1] pzs=voxel_coords[:,2] if fig is None: fig = mlab.figure(figure=None, bgcolor=(0,0,0), fgcolor=None, engine=None, size=(10000, 50000)) mlab.points3d( pxs, pys, pzs, #prs, mode="cube", # 'point' 'sphere' colormap='cool', #'bone', #'spectral', #'copper', scale_factor=1, figure=fig) mlab.orientation_axes() mlab.view(azimuth=180,elevation=None,distance=100,focalpoint=[ 0 , 0, 0])#2.0909996 , -1.04700089, -2.03249991 return fig
def visualize(self, img_id, box2d, ps, segp, box3d, corners_3d_pred, center): img_filename = os.path.join(glb.IMG_DIR, '%06d.jpg' % (img_id)) img = cv2.imread(img_filename) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.rectangle(img, (int(box2d[0]), int(box2d[1])), (int(box2d[2]), int(box2d[3])), (0, 255, 0), 3) Image.fromarray(img).show() # Draw figures fig = mlab.figure(figure=None, bgcolor=(0.6, 0.6, 0.6), fgcolor=None, engine=None, size=(1000, 500)) mlab.points3d(0, 0, 0, color=(1, 1, 1), mode='sphere', scale_factor=0.2, figure=fig) mlab.points3d(ps[:, 0], ps[:, 1], ps[:, 2], segp, mode='point', colormap='gnuplot', scale_factor=1, figure=fig) draw_gt_boxes3d([box3d], fig, color=(0, 0, 1), draw_text=False) draw_gt_boxes3d([corners_3d_pred], fig, color=(0, 1, 0), draw_text=False) mlab.points3d(center[0], center[1], center[2], color=(0, 1, 0), mode='sphere', scale_factor=0.4, figure=fig) mlab.orientation_axes() input()
def update_scene(): # clear figure mlab.clf(fig1) # Rescale points for visu p1 = (query * 1.5 + np.array([1.0, 1.0, 1.0])) * 50.0 p2 = (supports * 1.5 + np.array([1.0, 1.0, 1.0])) * 50.0 l1 = p1[:, 2] * 0 l1[file_i] = 1 l2 = p2[:, 2] * 0 + 2 l2[neighbors[file_i]] = 3 # Show point clouds colorized with activations activations = mlab.points3d(p1[:, 0], p1[:, 1], p1[:, 2], l1, scale_factor=2.0, scale_mode='none', vmin=0.0, vmax=3.0, figure=fig1) activations = mlab.points3d(p2[:, 0], p2[:, 1], p2[:, 2], l2, scale_factor=3.0, scale_mode='none', vmin=0.0, vmax=3.0, figure=fig1) # New title mlab.title(str(file_i), color=(0, 0, 0), size=0.3, height=0.01) text = '<--- (press g for previous)' + 50 * ' ' + '(press h for next) --->' mlab.text(0.01, 0.01, text, color=(0, 0, 0), width=0.98) mlab.orientation_axes() return
def show_mask(masks, images): # bb = np.logical_or(*masks).mbb(100) bb = masks[0].mbb(100) masks = [x[bb] for x in masks] images = [x[bb] for x in images] logging.warning('### %s', [np.count_nonzero(x) for x in masks]) fig = mlab.figure() for i, a in enumerate(masks): src = mlab.pipeline.scalar_field(a) src.name += ': {}'.format(Path(a.info['path']).name) logging.warning(src.name) # src.spacing = a.info['attrs']['voxel_spacing'] src.spacing = a.spacing src.update_image_data = True blur = mlab.pipeline.user_defined(src, filter='ImageGaussianSmooth') voi = mlab.pipeline.extract_grid(blur) d = dict(contours=[0.3]) if i == 0: d.update(color=(0, 0, 1), opacity=0.2) elif i == 1: d.update(color=(1, 1, 0), opacity=0.4) else: d.update(color=(1, 0, 0), opacity=0.8) mlab.pipeline.iso_surface(voi, **d) for a in images: src = mlab.pipeline.scalar_field(a) src.name += ': {}'.format(a.info['path']) # src.spacing = a.info['attrs']['voxel_spacing'] src.spacing = a.spacing src.update_image_data = True blur = mlab.pipeline.user_defined(src, filter='ImageGaussianSmooth') voi = mlab.pipeline.extract_grid(blur) d = dict(contours=5, colormap='viridis', opacity=0.3) mlab.pipeline.iso_surface(voi, **d) mlab.view(0, 0) mlab.orientation_axes(figure=fig) mlab.show()
' '] text = "\n".join(words) if not args.notext: mlab.text(.8, .9, text, figure=figure, width=.2) if iteration == -1: iterword = 'initial grid' elif iteration == -2: iterword = 'post initialization' elif iteration == -3: iterword = 'latest save' else: iterword = '%08i' %iteration itertext = mlab.text(.02, .95, iterword, figure=figure, width=.2) if not args.notext: mlab.orientation_axes(figure=figure, name='bob') # Draw the cell cell = mlab.outline(extent=[0, dim[0], 0, dim[1], 0, dim[2]], color=(0,0,0), line_width=3) if(celltype == 'walls' and not args.hide_walls): sheet_points = array([[0, 0, 0], [dim[0], 0, 0], [dim[0], dim[1], 0], [0, dim[1], 0], [0, 0, dim[2]], [dim[0], 0, dim[2]], [dim[0], dim[1], dim[2]], [0, dim[1], dim[2]]]) sheet_connections = array([[0, 1, 2, 3], [4, 5, 6, 7]]) sheetmesh = tvtk.PolyData(points=sheet_points, polys=sheet_connections) mlab.pipeline.surface(sheetmesh, opacity=.6, color=(1,1,1)) # if(celltype == 'walls'and not args.hide_walls): # nbars = 11 # x = tile(repeat(linspace(0, dim[0], nbars), 2), 2)
def ToyModel3d(sample): """ This script configure the 3D render motor (Mayavi) to show an interactive reconstruction of the asphalt mixture sample """ src = mlab.pipeline.scalar_field(sample) inverse_lut = False colors = 5 iso = mlab.pipeline.iso_surface(src, contours=[1], opacity=0.4, colormap = 'blue-red') iso.module_manager.scalar_lut_manager.reverse_lut = inverse_lut iso.module_manager.scalar_lut_manager.number_of_colors = colors ipw = mlab.pipeline.image_plane_widget(src, plane_orientation='y_axes', slice_index=10, colormap = 'blue-red') ipw.module_manager.scalar_lut_manager.reverse_lut = inverse_lut ipw.module_manager.scalar_lut_manager.number_of_colors = colors scp = mlab.pipeline.scalar_cut_plane(src, colormap = 'blue-red') scp.module_manager.scalar_lut_manager.reverse_lut = inverse_lut scp.module_manager.scalar_lut_manager.number_of_colors = colors #Set the Mayavi Colorbar Ranges scp.module_manager.scalar_lut_manager.use_default_range = False scp.module_manager.scalar_lut_manager.scalar_bar.position2 = array([ 0.1, 0.8]) scp.module_manager.scalar_lut_manager.scalar_bar.position = array([ 0.01, 0.15]) scp.module_manager.scalar_lut_manager.data_range = array([ 0., 2.]) scp.module_manager.scalar_lut_manager.scalar_bar.position2 = array([ 0.1, 0.8]) scp.module_manager.scalar_lut_manager.scalar_bar.position = array([ 0.01, 0.15]) scp.module_manager.scalar_lut_manager.data_range = array([ 0., 2.]) engine = mlab.get_engine() textAggregate = Text() textMastic = Text() textVoids = Text() engine.add_filter(textAggregate, scp.module_manager) engine.add_filter(textMastic, ipw.module_manager) engine.add_filter(textVoids, iso.module_manager) textAggregate.text = 'Aggregate' textMastic.text = 'Mastic' textVoids.text = 'Air Voids' textAggregate.actor.text_scale_mode = 'viewport' textMastic.actor.text_scale_mode = 'viewport' textVoids.actor.text_scale_mode = 'viewport' textAggregate.actor.minimum_size = array([ 1, 10]) textMastic.actor.minimum_size = array([ 1, 10]) textVoids.actor.minimum_size = array([ 1, 10]) textAggregate.actor.position = array([ 0.115, 0.7 ]) textMastic.actor.position = array([ 0.115, 0.45]) textVoids.actor.position = array([ 0.115, 0.23]) mlab.orientation_axes() mlab.title("Asphalt Mixture Reconstruction", size=0.25) mlab.colorbar(title='Material', orientation='vertical', nb_labels=0, nb_colors=3) mlab.show()
def main(): parser = argparse.ArgumentParser(description="Test calc") parser.add_argument("--show", "--plot", action="store_true") args = vutil.common_argparse(parser) f3d = viscid.load_file(_viscid_root + '/../sample/sample.3df.[0].xdmf') f_iono = viscid.load_file(_viscid_root + "/../sample/*.iof.[0].xdmf") b = f3d["b"] pp = f3d["pp"] # plot a scalar cut plane of pressure pp_src = mvi.field2source(pp, center='node') scp = mlab.pipeline.scalar_cut_plane(pp_src, plane_orientation='z_axes', transparent=True, opacity=0.5, view_controls=False) scp.implicit_plane.normal = [0, 0, -1] scp.implicit_plane.origin = [0, 0, 0] # i don't know why this log10 doesn't seem to work scp.module_manager.scalar_lut_manager.lut.scale = 'log10' scp.module_manager.scalar_lut_manager.lut_mode = 'Reds' scp.module_manager.scalar_lut_manager.reverse_lut = True scp.module_manager.scalar_lut_manager.show_scalar_bar = True # calculate B field lines && topology in viscid and plot them seeds = viscid.SphericalPatch([0, 0, 0], [2, 0, 1], 30, 15, r=5.0, nalpha=5, nbeta=5) b_lines, topo = viscid.calc_streamlines(b, seeds, ibound=3.5, obound0=[-25, -20, -20], obound1=[15, 20, 20]) mvi.plot_lines(b_lines, scalars=viscid.topology2color(topo)) # Use Mayavi (VTK) to calculate field lines using an interactive seed b_src = mvi.field2source(b, center='node') bsl2 = mlab.pipeline.streamline(b_src, seedtype='sphere', integration_direction='both', seed_resolution=4) bsl2.stream_tracer.maximum_propagation = 20. bsl2.seed.widget.center = [-11, 0, 0] bsl2.seed.widget.radius = 1.0 bsl2.streamline_type = 'tube' bsl2.tube_filter.radius = 0.03 bsl2.stop() # this stop/start was a hack to get something to work? bsl2.start() bsl2.seed.widget.enabled = True # Plot the ionosphere too fac_tot = 1e9 * f_iono['fac_tot'] crd_system = 'gse' m = mvi.plot_ionosphere(fac_tot, crd_system=crd_system, bounding_lat=30.0, vmin=-300, vmax=300, opacity=0.75) m.module_manager.scalar_lut_manager.lut_mode = 'RdBu' m.module_manager.scalar_lut_manager.reverse_lut = True mvi.plot_blue_marble(r=1.0, orientation=(0, 21.5, -45.0)) # now shade the night side with a transparent black hemisphere mvi.plot_earth_3d(radius=1.01, crd_system="gse", night_only=True, opacity=0.5) mlab.axes(pp_src, nb_labels=5) mlab.orientation_axes() mvi.resize([1200, 800]) mlab.view(azimuth=40, elevation=70, distance=35.0, focalpoint=[-3, 0, 0]) # # Save Figure # print("saving png") # mvi.mlab.savefig('mayavi_msphere_sample.png') # print("saving x3d") # # x3d files can be turned into COLLADA files with meshlab, and # # COLLADA (.dae) files can be opened in OS X's preview # # # # IMPORTANT: for some reason, using bounding_lat in mvi.plot_ionosphere # # causes a segfault when saving x3d files # # # mvi.mlab.savefig('mayavi_msphere_sample.x3d') # print("done") if args.show: mlab.show()
def dataset_viz(show_frustum=False): sunrgbd = sunrgbd_object(data_dir) idxs = np.array(range(1,len(sunrgbd)+1)) np.random.shuffle(idxs) for idx in range(len(sunrgbd)): data_idx = idxs[idx] print('--------------------', data_idx) pc = sunrgbd.get_depth(data_idx) print(pc.shape) # Project points to image calib = sunrgbd.get_calibration(data_idx) uv,d = calib.project_upright_depth_to_image(pc[:,0:3]) print(uv) print(d) raw_input() import matplotlib.pyplot as plt cmap = plt.cm.get_cmap('hsv', 256) cmap = np.array([cmap(i) for i in range(256)])[:,:3]*255 img = sunrgbd.get_image(data_idx) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for i in range(uv.shape[0]): depth = d[i] color = cmap[int(120.0/depth),:] cv2.circle(img, (int(np.round(uv[i,0])), int(np.round(uv[i,1]))), 2, color=tuple(color), thickness=-1) Image.fromarray(img).show() raw_input() # Load box labels objects = sunrgbd.get_label_objects(data_idx) print(objects) raw_input() # Draw 2D boxes on image img = sunrgbd.get_image(data_idx) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for i,obj in enumerate(objects): cv2.rectangle(img, (int(obj.xmin),int(obj.ymin)), (int(obj.xmax),int(obj.ymax)), (0,255,0), 2) cv2.putText(img, '%d %s'%(i,obj.classname), (max(int(obj.xmin),15), max(int(obj.ymin),15)), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 2) Image.fromarray(img).show() raw_input() # Draw 3D boxes on depth points box3d = [] ori3d = [] for obj in objects: corners_3d_image, corners_3d = utils.compute_box_3d(obj, calib) ori_3d_image, ori_3d = utils.compute_orientation_3d(obj, calib) print('Corners 3D: ', corners_3d) box3d.append(corners_3d) ori3d.append(ori_3d) raw_input() bgcolor=(0,0,0) fig = mlab.figure(figure=None, bgcolor=bgcolor, fgcolor=None, engine=None, size=(1600, 1000)) mlab.points3d(pc[:,0], pc[:,1], pc[:,2], pc[:,2], mode='point', colormap='gnuplot', figure=fig) mlab.points3d(0, 0, 0, color=(1,1,1), mode='sphere', scale_factor=0.2) draw_gt_boxes3d(box3d, fig=fig) for i in range(len(ori3d)): ori_3d = ori3d[i] x1,y1,z1 = ori_3d[0,:] x2,y2,z2 = ori_3d[1,:] mlab.plot3d([x1, x2], [y1, y2], [z1,z2], color=(0.5,0.5,0.5), tube_radius=None, line_width=1, figure=fig) mlab.orientation_axes() for i,obj in enumerate(objects): print('Orientation: ', i, np.arctan2(obj.orientation[1], obj.orientation[0])) print('Dimension: ', i, obj.l, obj.w, obj.h) raw_input() if show_frustum: img = sunrgbd.get_image(data_idx) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for i,obj in enumerate(objects): box2d_fov_inds = (uv[:,0]<obj.xmax) & (uv[:,0]>=obj.xmin) & (uv[:,1]<obj.ymax) & (uv[:,1]>=obj.ymin) box2d_fov_pc = pc[box2d_fov_inds, :] img2 = np.copy(img) cv2.rectangle(img2, (int(obj.xmin),int(obj.ymin)), (int(obj.xmax),int(obj.ymax)), (0,255,0), 2) cv2.putText(img2, '%d %s'%(i,obj.classname), (max(int(obj.xmin),15), max(int(obj.ymin),15)), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 2) Image.fromarray(img2).show() fig = mlab.figure(figure=None, bgcolor=bgcolor, fgcolor=None, engine=None, size=(1000, 1000)) mlab.points3d(box2d_fov_pc[:,0], box2d_fov_pc[:,1], box2d_fov_pc[:,2], box2d_fov_pc[:,2], mode='point', colormap='gnuplot', figure=fig) raw_input()
def extract_roi_seg(idx_filename, split, output_filename, viz, perturb_box2d=False, augmentX=1, type_whitelist=['bed','table','sofa','chair','toilet','desk','dresser','night_stand','bookshelf','bathtub']): dataset = sunrgbd_object('/home/rqi/Data/mysunrgbd', split) data_idx_list = [int(line.rstrip()) for line in open(idx_filename)] id_list = [] # int number box2d_list = [] # [xmin,ymin,xmax,ymax] box3d_list = [] # (8,3) array in upright depth coord input_list = [] # channel number = 6, xyz,rgb in upright depth coord label_list = [] # 1 for roi object, 0 for clutter type_list = [] # string e.g. bed heading_list = [] # face of object angle, radius of clockwise angle from positive x axis in upright camera coord box3d_size_list = [] # array of l,w,h frustum_angle_list = [] # angle of 2d box center from pos x-axis (clockwise) pos_cnt = 0 all_cnt = 0 for data_idx in data_idx_list: print('------------- ', data_idx) calib = dataset.get_calibration(data_idx) objects = dataset.get_label_objects(data_idx) pc_upright_depth = dataset.get_depth(data_idx) pc_upright_camera = np.zeros_like(pc_upright_depth) pc_upright_camera[:,0:3] = calib.project_upright_depth_to_upright_camera(pc_upright_depth[:,0:3]) pc_upright_camera[:,3:] = pc_upright_depth[:,3:] if viz: mlab.points3d(pc_upright_camera[:,0], pc_upright_camera[:,1], pc_upright_camera[:,2], pc_upright_camera[:,1], mode='point') mlab.orientation_axes() raw_input() img = dataset.get_image(data_idx) img_height, img_width, img_channel = img.shape pc_image_coord,_ = calib.project_upright_depth_to_image(pc_upright_depth) #print('PC image coord: ', pc_image_coord) for obj_idx in range(len(objects)): obj = objects[obj_idx] if obj.classname not in type_whitelist: continue # 2D BOX: Get pts rect backprojected box2d = obj.box2d for _ in range(augmentX): try: # Augment data by box2d perturbation if perturb_box2d: xmin,ymin,xmax,ymax = random_shift_box2d(box2d) print(xmin,ymin,xmax,ymax) else: xmin,ymin,xmax,ymax = box2d box_fov_inds = (pc_image_coord[:,0]<xmax) & (pc_image_coord[:,0]>=xmin) & (pc_image_coord[:,1]<ymax) & (pc_image_coord[:,1]>=ymin) pc_in_box_fov = pc_upright_camera[box_fov_inds,:] # Get frustum angle (according to center pixel in 2D BOX) box2d_center = np.array([(xmin+xmax)/2.0, (ymin+ymax)/2.0]) uvdepth = np.zeros((1,3)) uvdepth[0,0:2] = box2d_center uvdepth[0,2] = 20 # some random depth box2d_center_upright_camera = calib.project_image_to_upright_camerea(uvdepth) print('UVdepth, center in upright camera: ', uvdepth, box2d_center_upright_camera) frustum_angle = -1 * np.arctan2(box2d_center_upright_camera[0,2], box2d_center_upright_camera[0,0]) # angle as to positive x-axis as in the Zoox paper print('Frustum angle: ', frustum_angle) # 3D BOX: Get pts velo in 3d box box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib) box3d_pts_3d = calib.project_upright_depth_to_upright_camera(box3d_pts_3d) _,inds = extract_pc_in_box3d(pc_in_box_fov, box3d_pts_3d) print(len(inds)) label = np.zeros((pc_in_box_fov.shape[0])) label[inds] = 1 # Get 3D BOX heading print('Orientation: ', obj.orientation) print('Heading angle: ', obj.heading_angle) # Get 3D BOX size box3d_size = np.array([2*obj.l,2*obj.w,2*obj.h]) print('Box3d size: ', box3d_size) print('Type: ', obj.classname) print('Num of point: ', pc_in_box_fov.shape[0]) # Subsample points.. num_point = pc_in_box_fov.shape[0] if num_point > 2048: choice = np.random.choice(pc_in_box_fov.shape[0], 2048, replace=False) pc_in_box_fov = pc_in_box_fov[choice,:] label = label[choice] # Reject object with too few points if np.sum(label) < 5: continue id_list.append(data_idx) box2d_list.append(np.array([xmin,ymin,xmax,ymax])) box3d_list.append(box3d_pts_3d) input_list.append(pc_in_box_fov) label_list.append(label) type_list.append(obj.classname) heading_list.append(obj.heading_angle) box3d_size_list.append(box3d_size) frustum_angle_list.append(frustum_angle) # collect statistics pos_cnt += np.sum(label) all_cnt += pc_in_box_fov.shape[0] # VISUALIZATION if viz: img2 = np.copy(img) cv2.rectangle(img2, (int(obj.xmin),int(obj.ymin)), (int(obj.xmax),int(obj.ymax)), (0,255,0), 2) utils.draw_projected_box3d(img2, box3d_pts_2d) Image.fromarray(img2).show() p1 = input_list[-1] seg = label_list[-1] fig = mlab.figure(figure=None, bgcolor=(0.4,0.4,0.4), fgcolor=None, engine=None, size=(500, 500)) mlab.points3d(p1[:,0], p1[:,1], p1[:,2], seg, mode='point', colormap='gnuplot', scale_factor=1, figure=fig) mlab.points3d(0, 0, 0, color=(1,1,1), mode='sphere', scale_factor=0.2) draw_gt_boxes3d([box3d_pts_3d], fig=fig) mlab.orientation_axes() raw_input() except: pass print('Average pos ratio: ', pos_cnt/float(all_cnt)) print('Average npoints: ', float(all_cnt)/len(id_list)) utils.save_zipped_pickle([id_list,box2d_list,box3d_list,input_list,label_list,type_list,heading_list,box3d_size_list,frustum_angle_list],output_filename)
def slicing(data, xlabel = '', ylabel = '', zlabel = '', title = ''): ''' Description This function generates a Mayavi scene that shows cut planes along the x, y and z axes of a data cube. This is an interactive scene, and so the cut planes can be moved through the cube, and the entire cube can be rotated. Required Input data: A three dimensional numpy array. Each entry of the array must contain a scalar value. xlabel: A string specifying a label for the x axis of the data cube. ylabel: A string specifying a label for the y axis of the data cube. zlabel: A string specifying a label for the z axis of the data cube. title: A string specifying the title for the visualisation. Output This function returns 1 if it runs to completion. An interactive Mayavi scene is produced by the function, allowing slices through a three dimensional data cube to be viewed. ''' # Create a new Mayavi scene to visualise slicing the data cube scene = mlab.figure(size = (800,700)) # Run a widget that allows you to visualise three dimensional data sets # This creates a slice along the x axis that can be interactively varied mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(data),\ plane_orientation='x_axes', slice_index=0) # This creates a slice along the y axis that can be interactively varied, # on the same image as the x axis slice mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(data),\ plane_orientation='y_axes', slice_index=0) # This creates a slice along the z axis that can be interactively varied, # on the same image as the x and y axis slices mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(data), plane_orientation='z_axes', slice_index=0) # Add axes to the visualisation of the image cube, and label the x, y and # z axes mlab.axes(xlabel = xlabel, ylabel = ylabel, zlabel = zlabel) # Add a title to the visualisation of the image cube mlab.title(title, height = 1.0) # Make the outline of the image cube appear mlab.outline() # Make a colourbar for the data mlab.scalarbar(orientation = 'vertical') # Add a little symbol to show the orientation of the data mlab.orientation_axes() # Allow interaction with the produced visualisation mlab.show() # Once the interactive 3D view has been created, return 0 return 1
def plot_trajectories(self,init = False): # Rendering disabled self.mayavi_view.scene.disable_render = True # Clear the scene self.mayavi_view.scene.mlab.clf() t1 = time.clock() G = nx.convert_node_labels_to_integers(self.directed_graph,ordering="sorted") xyts = np.array([(self.directed_graph.node[i]["x"], self.directed_graph.node[i]["y"], self.directed_graph.node[i]["t"], self.directed_graph.node[i]["s"]) for i in sorted(self.directed_graph)]) # Compute reasonable scaling factor according to the data limits. # We want the plot to be roughly square, to avoid nasty Mayavi axis scaling issues later. # Unfortunately, adjusting the surface.actor.actor.scale seems to lead to more problems than solutions. # See: http://stackoverflow.com/questions/13015097/how-do-i-scale-the-x-and-y-axes-in-mayavi2 t_scaling = np.mean( [(max(xyts[:,0])-min(xyts[:,0])), (max(xyts[:,1])-min(xyts[:,1]))] ) / (max(xyts[:,2])-min(xyts[:,2])) xyts[:,2] *= t_scaling ## Create the lines self.source = mlab.pipeline.scalar_scatter(xyts[:,0], xyts[:,1], xyts[:,2], xyts[:,3], figure = self.mayavi_view.scene.mayavi_scene) # Connect them self.source.mlab_source.dataset.lines = np.array(G.edges()) # Finally, display the set of lines self.line_collection = mlab.pipeline.surface(mlab.pipeline.stripper(self.source), # The stripper filter cleans up connected lines; it regularizes surfaces by creating triangle strips line_width=1, colormap=self.selected_colormap) self.trajectory_labels = np.array([self.directed_graph.node[i]["l"] for i in sorted(self.directed_graph)]) # Generate the corresponding set of nodes pts = mlab.points3d(xyts[:,0], xyts[:,1], xyts[:,2], xyts[:,3], scale_factor = 0.0, # scale_factor = 'auto' results in huge pts: pts.glyph.glpyh.scale_factor = 147 scale_mode = 'none', colormap = self.selected_colormap, figure = self.mayavi_view.scene.mayavi_scene) pts.glyph.color_mode = 'color_by_scalar' pts.mlab_source.dataset.lines = np.array(G.edges()) self.node_collection = pts # Add object label text self.text_collection = [mlab.text3d(self.directed_graph.node[i[-1]]["x"], self.directed_graph.node[i[-1]]["y"], self.directed_graph.node[i[-1]]["t"]*t_scaling, str(i[-1][0]), line_width = 20, scale = 10, name = str(i[-1][0]), figure = self.mayavi_view.scene.mayavi_scene) for i in self.connected_nodes] # Add outline to be used later when selecting points self.selection_outline = mlab.outline(line_width=3) self.selection_outline.outline_mode = 'cornered' # Using axes doesn't work until the scene is avilable: # http://docs.enthought.com/mayavi/mayavi/building_applications.html#making-the-visualization-live #mlab.axes() self.mayavi_view.scene.reset_zoom() # An trajectory picker object is created to trigger an event when a trajectory is picked. # TODO: Figure out how to re-activate picker on scene refresh # E.g., (not identical problem) http://www.mail-archive.com/[email protected]/msg00583.html self.trajectory_picker = self.mayavi_view.scene.mayavi_scene.on_mouse_pick(self.on_pick_one_trajectory) # Figure decorations # Orientation axes mlab.orientation_axes(zlabel = "t", figure = self.mayavi_view.scene.mayavi_scene, line_width = 5) # Colormap # TODO: Figure out how to scale colorbar to smaller size #c = mlab.colorbar(orientation = "horizontal", title = self.selected_feature) #c.scalar_bar_representation.position2[1] = 0.05 #c.scalar_bar.height = 0.05 # Re-enable the rendering self.mayavi_view.scene.disable_render = False t2 = time.clock() logging.info("Computed layout (%.2f sec)"%(t2-t1))
def plot3Dslice(geodata,surfs,vbounds, titlestr='', time = 0,gkey = None,cmap=defmap3d, ax=None,fig=None,method='linear',fill_value=np.nan,view = None,units='',colorbar=False,outimage=False): """ This function create 3-D slice image given either a surface or list of coordinates to slice through Inputs: geodata - A geodata object that will be plotted in 3D surfs - This is a three element list. Each element can either be altlist - A list of the altitudes that RISR parameter slices will be taken at xyvecs- A list of x and y numpy arrays that have the x and y coordinates that the data will be interpolated over. ie, xyvecs=[np.linspace(-100.0,500.0),np.linspace(0.0,600.0)] vbounds = a list of bounds for the geodata objec's parameters. ie, vbounds=[500,2000] title - A string that holds for the overall image ax - A handle for an axis that this will be plotted on. Returns a mayavi image with a surface """ if mlab is None: print('mayavi was not successfully imported') return assert geodata.coordnames.lower() =='cartesian' datalocs = geodata.dataloc xvec = sp.unique(datalocs[:,0]) yvec = sp.unique(datalocs[:,1]) zvec = sp.unique(datalocs[:,2]) assert len(xvec)*len(yvec)*len(zvec)==datalocs.shape[0] #determine if the ordering is fortran or c style ordering diffcoord = sp.diff(datalocs,axis=0) if diffcoord[0,1]!=0.0: ord='f' elif diffcoord[0,2]!=0.0: ord='c' elif diffcoord[0,0]!=0.0: if len(np.where(diffcoord[:,1])[0])==0: ord = 'f' elif len(np.where(diffcoord[:,2])[0])==0: ord = 'c' matshape = (len(yvec),len(xvec),len(zvec)) # reshape the arrays into a matricies for plotting x,y,z = [sp.reshape(datalocs[:,idim],matshape,order=ord) for idim in range(3)] if gkey is None: gkey = geodata.datanames()[0] porig = geodata.data[gkey][:,time] mlab.figure(fig) #determine if list of slices or surfaces are given islists = isinstance(surfs[0],list) if isinstance(surfs[0],np.ndarray): onedim = surfs[0].ndim==1 #get slices for each dimension out surflist = [] if islists or onedim: p = np.reshape(porig,matshape,order= ord ) xslices = surfs[0] for isur in xslices: indx = sp.argmin(sp.absolute(isur-xvec)) xtmp = x[:,indx] ytmp = y[:,indx] ztmp = z[:,indx] ptmp = p[:,indx] pmask = sp.zeros_like(ptmp).astype(bool) pmask[sp.isnan(ptmp)]=True surflist.append( mlab.mesh(xtmp,ytmp,ztmp,scalars=ptmp,vmin=vbounds[0],vmax=vbounds[1],colormap=cmap,mask=pmask)) surflist[-1].module_manager.scalar_lut_manager.lut.nan_color = 0, 0, 0, 0 yslices = surfs[1] for isur in yslices: indx = sp.argmin(sp.absolute(isur-yvec)) xtmp = x[indx] ytmp = y[indx] ztmp = z[indx] ptmp = p[indx] pmask = sp.zeros_like(ptmp).astype(bool) pmask[sp.isnan(ptmp)]=True surflist.append( mlab.mesh(xtmp,ytmp,ztmp,scalars=ptmp,vmin=vbounds[0],vmax=vbounds[1],colormap=cmap,mask=pmask)) surflist[-1].module_manager.scalar_lut_manager.lut.nan_color = 0, 0, 0, 0 zslices = surfs[2] for isur in zslices: indx = sp.argmin(sp.absolute(isur-zvec)) xtmp = x[:,:,indx] ytmp = y[:,:,indx] ztmp = z[:,:,indx] ptmp = p[:,:,indx] pmask = sp.zeros_like(ptmp).astype(bool) pmask[sp.isnan(ptmp)]=True surflist.append( mlab.mesh(xtmp,ytmp,ztmp,scalars=ptmp,vmin=vbounds[0],vmax=vbounds[1],colormap=cmap,mask=pmask)) surflist[-1].module_manager.scalar_lut_manager.lut.nan_color = 0, 0, 0, 0 else: # For a general surface. xtmp,ytmp,ztmp = surfs[:] gooddata = ~np.isnan(porig) curparam = porig[gooddata] curlocs = datalocs[gooddata] new_coords = np.column_stack((xtmp.flatten(),ytmp.flatten(),ztmp.flatten())) ptmp = spinterp.griddata(curlocs,curparam,new_coords,method,fill_value) pmask = sp.zeros_like(ptmp).astype(bool) pmask[sp.isnan(ptmp)]=True surflist.append( mlab.mesh(xtmp,ytmp,ztmp,scalars=ptmp,vmin=vbounds[0],vmax=vbounds[1],colormap=cmap,mask=pmask)) surflist[-1].module_manager.scalar_lut_manager.lut.nan_color = 0, 0, 0, 0 mlab.title(titlestr,color=(0,0,0)) #mlab.outline(color=(0,0,0)) mlab.axes(color=(0,0,0),x_axis_visibility=True,xlabel = 'x in km',y_axis_visibility=True, ylabel = 'y in km',z_axis_visibility=True,zlabel = 'z in km') mlab.orientation_axes(xlabel = 'x in km',ylabel = 'y in km',zlabel = 'z in km') if view is not None: mlab.view(view[0],view[1]) if colorbar: if len(units)>0: titlstr = gkey +' in ' +units else: titlestr = gkey mlab.colorbar(surflist[-1],title=titlstr,orientation='vertical') if outimage: arr = mlab.screenshot(fig,antialiased = True) mlab.close(fig) return arr else: return surflist
print box3d_new print 'Ground/3D IoU: ', iou_2d, iou_3d correct = int(iou_3d >= 0.25) total_cnt += 1 correct_cnt += correct class_total_cnt[classname] += 1 class_correct_cnt[classname] += correct if VISU: #and iou_3d<0.7: img_filename = os.path.join(IMG_DIR, '%06d.jpg'%(img_id)) img = cv2.imread(img_filename) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.rectangle(img, (int(box2d[0]),int(box2d[1])), (int(box2d[2]),int(box2d[3])), (0,255,0), 3) Image.fromarray(img).show() # Draw figures fig = mlab.figure(figure=None, bgcolor=(0.6,0.6,0.6), fgcolor=None, engine=None, size=(1000, 500)) mlab.points3d(0, 0, 0, color=(1,1,1), mode='sphere', scale_factor=0.2, figure=fig) mlab.points3d(ps[:,0], ps[:,1], ps[:,2], segp, mode='point', colormap='gnuplot', scale_factor=1, figure=fig) draw_gt_boxes3d([box3d], fig, color = (0,0,1), draw_text=False) draw_gt_boxes3d([corners_3d_pred], fig, color = (0,1,0), draw_text=False) mlab.points3d(center[0], center[1], center[2], color=(0,1,0), mode='sphere', scale_factor=0.4, figure=fig) mlab.orientation_axes() raw_input() print '-----------------------' print 'Total cnt: %d, acuracy: %f' % (total_cnt, correct_cnt/float(total_cnt)) for classname in type_whitelist: print 'Class: %s\tcnt: %d\taccuracy: %f' % (classname.ljust(15), class_total_cnt[classname], class_correct_cnt[classname]/float(class_total_cnt[classname]))
def pickOriginAndZone(self,x,y,z,substrateHeightGuess,altitude=0.001,pickZone=True): # dirty hack to suppress VTK errors when setting origin's mlab_data output=vtk.vtkFileOutputWindow() output.SetFileName("log.txt") vtk.vtkOutputWindow().SetInstance(output) s = mlab.mesh(x, y, z) s.components[1].property.lighting = False maximumHeight = z.max() safeHeight = maximumHeight+0.01 origin = mlab.points3d(float(x.min()), float(y.min()), 0.0, mode='axes', color=(1, 0, 0), scale_factor=0.03) if pickZone: scanZone = HorizontalRectangle((0,0,0),height=0,displayZOffset=self.calibration.mechanical.spotZ.asUnit('m')) substrateHeightZone = HorizontalRectangle((1,0,0),height=0) def updateSubstrateHeight(newHeight): colorRangeHeight = altitude-self.calibration.mechanical.spotZ.asUnit('m') s.module_manager.scalar_lut_manager.data_range = (newHeight-colorRangeHeight,newHeight+colorRangeHeight) origin.mlab_source.set(z = newHeight) if pickZone: scanZone.height = newHeight+float(altitude) substrateHeightZone.height = newHeight updateSubstrateHeight(float(substrateHeightGuess)) def originSelectorCallBack(picker): origin.mlab_source.set(x = picker.pick_position[0], y = picker.pick_position[1]) selectedPosition = Position(picker.pick_position,unit='m') try: self.setProbeLocation(Position([selectedPosition[0],selectedPosition[1],safeHeight],'m')) self.setProbeLocation(selectedPosition + Position([0,0,0.002],'m')) except Warning: pass picker = mlab.gcf().on_mouse_pick(originSelectorCallBack, type='cell', button='Middle') def zoneSelectorCallback(picker,zone): pickedPosition = picker.pick_position[0:2] if pickZone: self.setProbeLocation(Position([pickedPosition[0],pickedPosition[1],safeHeight],'m')) self.setProbeLocation(Position([pickedPosition[0],pickedPosition[1],scanZone.height],'m')) if picker.topRightPicked: zone.bottomLeft = pickedPosition else: zone.topRight = pickedPosition picker.topRightPicked = not(picker.topRightPicked) def scanZoneSelectorCallback(picker): return zoneSelectorCallback(picker,scanZone) def substrateHeightZoneSelectorCallback(picker): zoneSelectorCallback(picker,substrateHeightZone) if not(picker.topRightPicked): substrateHeightZone.sort() zValuesInZone = z[(x > substrateHeightZone.bottomLeft[0]) & (x < substrateHeightZone.topRight[0]) & (y > substrateHeightZone.bottomLeft[1]) & (y < substrateHeightZone.topRight[1])] print 'Estimating substrate height based on',zValuesInZone.shape,'values...' if len(zValuesInZone) > 0: heightEstimate = self.estimateSubstrateHeight(zValuesInZone) updateSubstrateHeight(heightEstimate) picker = mlab.gcf().on_mouse_pick(scanZoneSelectorCallback, type='cell', button='Right') picker.add_trait('topRightPicked',api.Bool(False)) picker = mlab.gcf().on_mouse_pick(substrateHeightZoneSelectorCallback, type='cell', button='Left') picker.add_trait('topRightPicked',api.Bool(False)) mlab.orientation_axes() mlab.view(azimuth=0,elevation=0) mlab.show() originPosition = Position(origin.mlab_source.points[0][:],'m') if pickZone: scanZone.sort() lastProbeLaserPosition = self.getProbeLocation() lastProbeLaserPosition[2] = safeHeight self.setProbeLocation(lastProbeLaserPosition) return (originPosition,scanZone) else: return originPosition
def draw_3d(grid_x, grid_y, fval, title='pi'): mlab.figure() mlab.surf(grid_x, grid_y, fval)#, warp_scale="auto") mlab.axes(xlabel='x', ylabel='z', zlabel=title) mlab.orientation_axes(xlabel='x', ylabel='z', zlabel=title) mlab.title(title)