def rendTri(T, imageBounds, pixelSize, c=None, im=None, geometric_mean=False): from PYME.Analysis.points.SoftRend import drawTriang, drawTriangles xs = T.x[ T. triangles] # x posititions of vertices [nm], dimensions (# triangles, 3) ys = T.y[ T. triangles] # y posititions of vertices [nm], dimensions (# triangles, 3) if c is None: # We didn't pass anything in for c - use 1/ area of triangle a01 = numpy.vstack((xs[:, 0] - xs[:, 1], ys[:, 0] - ys[:, 1])).T a02 = numpy.vstack((xs[:, 0] - xs[:, 2], ys[:, 0] - ys[:, 2])).T a12 = numpy.vstack((xs[:, 1] - xs[:, 2], ys[:, 1] - ys[:, 2])).T a_ = ((a01 * a01).sum(1)) b_ = ((a02 * a02).sum(1)) b2_ = ((a12 * a12).sum(1)) # use the median edge length^2 as a proxy for area (this avoids "slithers" getting really bright) c = 0.5 * numpy.median([b_, a_, b2_], 0) #c_neighbours = c[T.triangle_neighbors].sum(1) #c = 1.0/(c + c_neighbours + 1) #c = numpy.maximum(c, pixelsize**2) #try to avoid spikes # Changed pre-factor 4/3/2019 so that image is calibrated in localizations/um^2 rather than # localizations per nm^2 in the hope that this will play better with other software (e.g. ImageJ) if geometric_mean: # calculate the mean areas first, then invert c = c else: # default - arithmetic mean (of density) c = 1e6 / (c + 1) if im is None: print('Some thing is wrong - we should already have allocated memory') sizeX = (imageBounds.x1 - imageBounds.x0) / pixelSize sizeY = (imageBounds.y1 - imageBounds.y0) / pixelSize im = numpy.zeros((sizeX, sizeY)) # convert vertices [nm] to pixel position in output image (still floating point) xs = (xs - imageBounds.x0) / pixelSize ys = (ys - imageBounds.y0) / pixelSize # NOTE 1: drawTriangles truncates co-ordinates to the nearest pixel on the left. # NOTE 2: this truncation means that nothing is drawn for triangles < 1 pixel # NOTE 3: triangles which would intersect with the edge of the image are discarded drawTriangles(im, xs, ys, c) return im
def rendTri2(T, imageBounds, pixelSize, c=None, im=None, im1=None): from PYME.Analysis.points.SoftRend import drawTriang, drawTriangles xs = T.x[T.triangles] ys = T.y[T.triangles] a = numpy.vstack((xs[:, 0] - xs[:, 1], ys[:, 0] - ys[:, 1])).T b = numpy.vstack((xs[:, 0] - xs[:, 2], ys[:, 0] - ys[:, 2])).T b2 = numpy.vstack((xs[:, 1] - xs[:, 2], ys[:, 1] - ys[:, 2])).T #area of triangle #c = 0.5*numpy.sqrt((b*b).sum(1) - ((a*b).sum(1)**2)/(a*a).sum(1))*numpy.sqrt((a*a).sum(1)) #c = 0.5*numpy.sqrt((b*b).sum(1)*(a*a).sum(1) - ((a*b).sum(1)**2)) #c = numpy.maximum(((b*b).sum(1)),((a*a).sum(1))) c = numpy.abs(a[:, 0] * b[:, 1] + a[:, 1] * b[:, 0]) if c is None: if numpy.version.version > '1.2': c = numpy.median([(b * b).sum(1), (a * a).sum(1), (b2 * b2).sum(1)], 0) else: c = numpy.median([(b * b).sum(1), (a * a).sum(1), (b2 * b2).sum(1)]) #c = c*c/1e6 a_ = ((a * a).sum(1)) b_ = ((b * b).sum(1)) b2_ = ((b2 * b2).sum(1)) #c_neighbours = c[T.triangle_neighbors].sum(1) #c = 1.0/(c + c_neighbours + 1) #c = numpy.maximum(c, self.pixelsize**2) #c = 1.0/(c + 1) sizeX = int((imageBounds.x1 - imageBounds.x0) / pixelSize) sizeY = int((imageBounds.y1 - imageBounds.y0) / pixelSize) xs = (xs - imageBounds.x0) / pixelSize ys = (ys - imageBounds.y0) / pixelSize if im is None: im = numpy.zeros((sizeX, sizeY)) im1 = numpy.zeros_like(im) drawTriangles(im, xs, ys, c * c * c) drawTriangles(im1, xs, ys, c * c * c * c) return im, im1
def rendVoronoi(x, y, imageBounds, pixelSize): from matplotlib import tri from PYME.Analysis.points.SoftRend import drawTriang, drawTriangles from PYME.recipes.pointcloud import Tesselation sizeX = int((imageBounds.x1 - imageBounds.x0) / pixelSize) sizeY = int((imageBounds.y1 - imageBounds.y0) / pixelSize) im = np.zeros((sizeX, sizeY)) #T = tri.Triangulation(x, y) Ts = Tesselation({'x': x, 'y': y, 'z': 0 * x}, three_d=False) cc = Ts.circumcentres() T = Ts.T tdb = [] for i in range(len(x)): tdb.append([]) for i in range(len(T.simplices)): nds = T.simplices[i] for n in nds: tdb[n].append(i) xs_ = None ys_ = None c_ = None area_colouring = True for i in range(len(x)): #get triangles around point impingentTriangs = tdb[i] #numpy.where(T.triangle_nodes == i)[0] if len(impingentTriangs) >= 3: circumcenters = cc[impingentTriangs] #get their circumcenters #add current point - to deal with edge cases newPts = np.array(list(circumcenters) + [[x[i], y[i]]]) #re-triangulate (we could try and sort the triangles somehow, but this is easier) T2 = tri.Triangulation(newPts[:, 0], newPts[:, 1]) #now do the same as for the standard triangulation xs = T2.x[T2.triangles] ys = T2.y[T2.triangles] a = np.vstack((xs[:, 0] - xs[:, 1], ys[:, 0] - ys[:, 1])).T b = np.vstack((xs[:, 0] - xs[:, 2], ys[:, 0] - ys[:, 2])).T #area of triangle c = 0.5 * np.sqrt((b * b).sum(1) - ((a * b).sum(1) ** 2) / (a * a).sum(1)) * np.sqrt((a * a).sum(1)) #c = numpy.maximum(((b*b).sum(1)),((a*a).sum(1))) #c_neighbours = c[T.triangle_neighbors].sum(1) #c = 1.0/(c + c_neighbours + 1) c = c.sum() * np.ones(c.shape) c = 1.0 / (c + 1) #print xs.shape #print c.shape if xs_ is None: xs_ = xs ys_ = ys c_ = c else: xs_ = np.vstack((xs_, xs)) ys_ = np.vstack((ys_, ys)) c_ = np.hstack((c_, c)) # convert vertices [nm] to pixel position in output image (still floating point) xs = (xs_ - imageBounds.x0) / pixelSize ys = (ys_ - imageBounds.y0) / pixelSize # NOTE 1: drawTriangles truncates co-ordinates to the nearest pixel on the left. # NOTE 2: this truncation means that nothing is drawn for triangles < 1 pixel # NOTE 3: triangles which would intersect with the edge of the image are discarded drawTriangles(im, xs, ys, c_) return im