def visualizeByLIC(vf): row_,col_,dep_ = vf.shape texture = np.random.rand(col_,row_).astype(np.float32) kernellen=9 kernel = np.sin(np.arange(kernellen)*np.pi/kernellen) kernel = kernel.astype(np.float32) vf = vf.astype(np.float32) img = lic_internal.line_integral_convolution(vf, texture, kernel) return img
def visualizeByLIC(vf): row_, col_, dep_ = vf.shape texture = np.random.rand(col_, row_).astype(np.float32) kernellen = 9 kernel = np.sin(np.arange(kernellen) * np.pi / kernellen) kernel = kernel.astype(np.float32) vf = vf.astype(np.float32) img = lic_internal.line_integral_convolution(vf, texture, kernel) return img
def lic(vectors, output_name): rsize, csize, _ = vectors.shape kernellen = 20 kernel = np.sin(np.arange(kernellen) * np.pi / kernellen) kernel = kernel.astype(np.float32) texture = np.random.rand(rsize, csize).astype(np.float32) image = lic_internal.line_integral_convolution(vectors, texture, kernel) scipy.misc.imsave(output_name, image)
def vector_flow(output_file, i_factor=1): # DPI resolution of the image to be saved dpi = 200 file_path_x = "example/vector_field_x_0.csv" file_path_y = "example/vector_field_y_0.csv" vector_field_x = np.loadtxt(file_path_x, delimiter=",") vector_field_y = np.loadtxt(file_path_y, delimiter=",") x_steps, y_steps = vector_field_x.shape # Interpolation factor. For 1 no interpolation occurs. if i_factor > 1: vector_field_x = scipy.ndimage.zoom(vector_field_x, i_factor) vector_field_y = scipy.ndimage.zoom(vector_field_y, i_factor) x_steps *= i_factor y_steps *= i_factor # Putting data in the expected format vectors = np.zeros((x_steps, y_steps, 2), dtype=np.float32) vectors[...,0] += vector_field_y vectors[...,1] += vector_field_x texture = np.random.rand(x_steps,y_steps).astype(np.float32) kernellen=20 kernel = np.sin(np.arange(kernellen)*np.pi/kernellen) kernel = kernel.astype(np.float32) image = lic_internal.line_integral_convolution(vectors, texture, kernel) mag = np.hypot(vector_field_x, vector_field_y) plt.jet() plt.figure() plt.axis('off') plt.imshow(texture, interpolation='nearest') plt.savefig(output_file+"-texture.png",dpi=dpi) plt.figure() fig = plt.quiver(vector_field_y, vector_field_x, mag) plt.colorbar() plt.savefig(output_file+".png",dpi=dpi) plt.bone() fig = plt.imshow(image, interpolation='nearest') # plt.colorbar() plt.savefig(output_file+"-flow.png",dpi=dpi)
def lic(vectors, output_name): rsize, csize, _ = vectors.shape eps = 1e-7 for x in range(rsize): for y in range(csize): if vectors[x, y, 0] == 0: vectors[x, y, 0] = eps if vectors[x, y, 1] == 0: vectors[x, y, 1] = eps kernellen=20 kernel = np.sin(np.arange(kernellen)*np.pi/kernellen) kernel = kernel.astype(np.float32) texture = np.random.rand(rsize, csize).astype(np.float32) image = lic_internal.line_integral_convolution(vectors, texture, kernel) scipy.misc.imsave(output_name, image)
def step(self, frame_count): kernellen = 31 t = frame_count/(16*5.) kernel = np.sin(np.arange(kernellen)*np.pi/kernellen)*(1+np.sin(2*np.pi*5*(np.arange(kernellen)/float(kernellen)+t))) kernel = kernel.astype(np.float32) image1 = lic_internal.line_integral_convolution(self.vectors, self.texture, kernel) image1.shape = (-1,) image = np.digitize(image1, np.linspace(0., 32., 256)) image.shape = (self.gc.width()/2., self.gc.height()/2., 1) self.image = np.empty(image.shape[:2]+(3,), dtype='uint8') self.image[:,:,:] = image gc = self.gc with gc: gc.draw_image(self.image, (0, 0, 512, 512))
texture = np.random.rand(size, size).astype(np.float32) plt.bone() frame = 0 if video: kernellen = 31 for t in np.linspace(0, 1, 16 * 5): kernel = np.sin(np.arange(kernellen) * np.pi / kernellen) * ( 1 + np.sin(2 * np.pi * 5 * (np.arange(kernellen) / float(kernellen) + t)) ) kernel = kernel.astype(np.float32) image = lic_internal.line_integral_convolution(vectors, texture, kernel) plt.clf() plt.axis("off") plt.figimage(image) plt.gcf().set_size_inches((size / float(dpi), size / float(dpi))) plt.savefig("flow-%04d.png" % frame, dpi=dpi) frame += 1 else: kernellen = 31 kernel = np.sin(np.arange(kernellen) * np.pi / kernellen) kernel = kernel.astype(np.float32) image = lic_internal.line_integral_convolution(vectors, texture, kernel) plt.clf()
xs = np.linspace(-1,1,size).astype(np.float32)[None,:] ys = np.linspace(-1,1,size).astype(np.float32)[:,None] vectors = np.zeros((size,size,2),dtype=np.float32) #for (x,y) in vortices: # rsq = (xs-x)**2+(ys-y)**2 # vectors[...,0] += (ys-y)/rsq # vectors[...,1] += -(xs-x)/rsq print np.shape(vectors) asdf() texture = np.random.rand(size,size).astype(np.float32) plt.bone() frame=0 kernellen=100 kernel = np.arange(kernellen) #np.sin(np.arange(kernellen)*np.pi/kernellen) kernel = kernel.astype(np.float32) image = lic_internal.line_integral_convolution(vectors, texture, kernel) plt.clf() plt.axis('off') plt.figimage(image) plt.gcf().set_size_inches((size/float(dpi),size/float(dpi))) plt.savefig("flow-image.png",dpi=dpi)
def plot_streamlines(u, v, xlim=(-1, 1), ylim=(-1, 1)): global COUNTER #define a grid on which to calculate everything Y,X = np.ogrid[y0 - 0.25 * abs(y0):y1 + 0.25 * abs(y1):size*1j, x0 - 0.25 * abs(x0):x1 + 0.25 * abs(x1):size*1j] print "Evaluating the velocity at each grid point..." uu = u(X,Y) #Evaluate the horizontal derivative at each grid point. vv = v(X,Y) #Evaluate the vertical derivative at each grid point. print "Plotting..." #color map for the convolution plot cmap = LinearSegmentedColormap.from_list('name', ['black','white','black','white']) #define the kernel (just a vector of numbers) kernel = np.arange(kernel_density).astype(np.float32) #reshape the velocities to fill in grid squ = np.reshape(uu, (int(size),int(size))).astype(np.float32) sqv = np.reshape(vv, (int(size),int(size))).astype(np.float32) #stack the velocities element-wise so we have a 2-tuple at each grid point. vectors = np.dstack((squ,sqv)).astype(np.float32) #generate the background noise. texture = np.random.rand(size/grain_size,size/grain_size).astype(np.float32) #resize the background noise to the resolution of the image by nearest neighbor interpolation (in order to provide support for larger grain size) texture = scipy.ndimage.zoom(texture, grain_size, order=1) print " Integrating for LIC plot (if this takes too long, try decreasing the kernel_density)" #Do the Line Integral Convolution. image = lic_internal.line_integral_convolution(vectors, texture, kernel) if flip_h: image = np.fliplr(image) if flip_v: image = np.flipud(image) plt.axis('off') plt.figimage(image,cmap=cmap) #calculate the velocities (ie: norm of velocity vector) velocity = np.linalg.norm(vectors, axis=2) #Cap the velocities at 10x the mean velocity (to prevent singularities from #skewing the color map # np.putmask(velocity, velocity>10*np.mean(velocity), 10*np.mean(velocity)) #sqrt the velocities, to make the differences less drastic. velocity = np.sqrt(velocity) theCM = plt.cm.get_cmap('bwr') theCM._init() #oldcm = matplotlib.cm.bwr oldcm = matplotlib.cm.Spectral_r v_at_infty = math.sqrt(u(1e9,1e9)**2 + v(1e9,1e9)**2) scm = cmap_center_point_adjust(oldcm, (np.min(velocity),np.max(velocity)), v_at_infty) if flip_h: velocity = np.fliplr(velocity) if flip_v: velocity = np.flipud(velocity) #plot the heat map dpi=300 imm = plt.figimage(velocity, cmap=scm, alpha=0.5) plt.gcf().set_size_inches((size/float(dpi),size/float(dpi))) plt.savefig("flow-image{0}.png".format(name),dpi=dpi)
def lic(u1,u2,d,nmax=600,fig=None,kernellen=31,color=None,saturation=0.8,lmin=0.05,lmax=0.95,vmin=None,vmax=None,ncolors=256): if fig==None: ax=plt.gca() else: ax=fig.figure.gca() xrange=[ax.get_xlim()[0],ax.get_xlim()[1]] yrange=[ax.get_ylim()[0],ax.get_ylim()[1]] if nmax == 600 and fig != None: nmax = fig.dpi * max([fig.fig_w,fig.fig_h]) # Aspect ratio: r=(xrange[1]-xrange[0])/(yrange[1]-yrange[0]) if r<1: ny=nmax nx=int(r*nmax) else: nx=nmax ny=int(nmax/r) nregrid = [nx,ny] CC=d.getCenterPoints() tmp0=np.complex(0,nregrid[0]) tmp1=np.complex(0,nregrid[1]) x=np.linspace(xrange[0],xrange[1],nregrid[0]) y=np.linspace(yrange[0],yrange[1],nregrid[1]) grid_x, grid_y = np.mgrid[xrange[0]:xrange[1]:tmp0, yrange[0]:yrange[1]:tmp1] u = griddata(CC, u1, (grid_x, grid_y), method='linear') v = griddata(CC, u2, (grid_x, grid_y), method='linear') uisnan=np.isnan(u) visnan=np.isnan(v) un = np.empty(np.shape(u)) vn = np.empty(np.shape(v)) un[uisnan] = griddata(CC, u1, (grid_x[uisnan], grid_y[uisnan]), method='nearest') vn[visnan] = griddata(CC, u2, (grid_x[visnan], grid_y[visnan]), method='nearest') u[uisnan]=un[uisnan] v[visnan]=vn[visnan] texture = np.random.rand(nx,ny).astype(np.float32) vectors = np.zeros((ny,nx,2),dtype=np.float32) vectors[...,0] = u.transpose().astype(np.float32) vectors[...,1] = v.transpose().astype(np.float32) kernel = np.sin(np.arange(kernellen)*np.pi/kernellen) kernel = kernel.astype(np.float32) image = lic_internal.line_integral_convolution(vectors, texture, kernel) if color == None: plt.gray() plt.figure() plt.imshow(image,origin='lower',extent=(xrange[0],xrange[1],yrange[0],yrange[1])) else: col = griddata(CC, color, (grid_x, grid_y), method='linear') colisnan=np.isnan(col) col[colisnan] = griddata(CC, color, (grid_x[colisnan], grid_y[colisnan]), method='nearest') col = col.transpose() if vmin==None: vmin=col.min() if vmax==None: vmax=col.max() col = np.clip(col,vmin,vmax) color_scaled = (1.-scale_to_unit_interval(col))*0.7 luminance_scaled = scale_to_unit_interval(image)*(lmax-lmin) + lmin image_rgb = np.empty((ny,nx,3)) for i in range(image.shape[0]): for j in range(image.shape[1]): image_rgb[i,j,:] = colorsys.hls_to_rgb(color_scaled[i,j],luminance_scaled[i,j],saturation) # Create the colormap for the colorbar: colors_h = (1.-np.linspace(0,1,ncolors))*0.7 colormap_rgb = np.empty((ncolors,3)) for i in range(ncolors): colormap_rgb[i,:] = colorsys.hls_to_rgb(colors_h[i],0.5,saturation) cmap_instance = colors.ListedColormap(colormap_rgb,'custom_map',ncolors) figure=plt.figure() image=plt.imshow(image_rgb,origin='lower',extent=(xrange[0],xrange[1],yrange[0],yrange[1])) plt.set_cmap(cmap_instance) plt.clim(vmin,vmax) plt.colorbar()