def makeInterpolatedImage(img1, img2, weight): """ weight: float between 0 and 1 """ edge_pix1 = findEdgePixels(img1) kdtree1 = KDTree(edge_pix1, edge_pix1) search1 = NearestNeighborSearchOnKDTree(kdtree1) edge_pix2 = findEdgePixels(img2) kdtree2 = KDTree(edge_pix2, edge_pix2) search2 = NearestNeighborSearchOnKDTree(kdtree2) img3 = ArrayImgs.unsignedBytes(Intervals.dimensionsAsLongArray(img1)) c1 = img1.cursor() c2 = img2.cursor() c3 = img3.cursor() pos = zeros(img1.numDimensions(), 'l') while c3.hasNext(): t1 = c1.next() t2 = c2.next() t3 = c3.next() sign1 = -1 if 0 == t1.get() else 1 sign2 = -1 if 0 == t2.get() else 1 search1.search(c1) search2.search(c2) value1 = sign1 * search1.getDistance() * weight value2 = sign2 * search2.getDistance() * (1 - weight) if value1 + value2 > 0: t3.setOne() return img3
def merge(nuclei, peaks2): """ nuclei: a dictionary of RealPoint, representing the average position, vs the number of points averaged. peaks: a list of RealPoint Returns the updated nuclei, with possibly new nuclei, and the existing ones having their coordinates (and counts of points averaged) updated. """ peaks1 = nuclei.keys() search = RadiusNeighborSearchOnKDTree(KDTree(peaks1, peaks1)) for peak2 in peaks2: search.search(peak2, radius, False) n = search.numNeighbors() if 0 == n: # New nuclei not ever seen before nuclei[peak2] = 1 else: # Merge peak with nearest found nuclei, which should only be one given the small radius peak1 = search.getSampler(0).get() count = float(nuclei[peak1]) new_count = count + 1 fraction = count / new_count for d in xrange(3): peak1.setPosition(peak1.getDoublePosition(d) * fraction + peak2.getDoublePosition(d) / new_count, d) nuclei[peak1] = new_count # Check for more if n > 1: print "Ignoring %i additional closeby nuclei" % (n - 1) # Return nuclei to enable a reduce operation over many sets of peaks return nuclei
def makeRadiusSearch(peaks): return RadiusNeighborSearchOnKDTree(KDTree(peaks, peaks))
def makeRadiusSearch(peaks): """ Construct a KDTree-based radius search, for locating points within a given radius of a reference point. """ return RadiusNeighborSearchOnKDTree(KDTree(peaks, peaks))
this.search.search(this); if (this.search.getSquareDistance() < this.radius_squared) { return inside; } return outside; } } public final Spheres createSpheres(final KDTree kdtree, final double radius, final UnsignedShortType inside, final UnsignedShortType outside) { return new Spheres(kdtree, radius, inside, outside); } """, [ RealPoint, RealRandomAccess, KDTree, NearestNeighborSearchOnKDTree, UnsignedShortType ]) points = [RealPoint([i, i, i]) for i in xrange(10)] kdtree = KDTree(points, points) # Works: ws = ws.createSpheres(kdtree, 2, UnsignedShortType(255), UnsignedShortType(0)) # Mysteriously fails with "expected 5 args, got 4". Has to do with calling the subclass constructor like a method. #ws = ws.Spheres(kdtree, 2, UnsignedShortType(255), UnsignedShortType(0))
listener = PointRoiRefresher(com, nuclei_detections) ImagePlus.addImageListener(listener) # Visualization 2: with a 2nd channel where each each detection is painted as a sphere from net.imglib2 import KDTree, RealPoint, RealRandomAccess, RealRandomAccessible, FinalInterval from net.imglib2.neighborsearch import NearestNeighborSearchOnKDTree from net.imglib2.type.numeric.integer import UnsignedShortType # A KDTree is a data structure for fast lookup of e.g. neareast spatial coordinates # Here, we create a KDTree for each timepoint 3D volume # ('i' is the timepoint index kdtrees = { i: KDTree(peaks, peaks) for i, peaks in nuclei_detections.iteritems() } radius = 5.0 # pixels inside = UnsignedShortType(255) # 'white' outside = UnsignedShortType(0) # 'black' # The definition of one sphere in 3D space for every nuclei detection class Spheres(RealPoint, RealRandomAccess): def __init__(self, kdtree, radius, inside, outside): super(RealPoint, self).__init__(3) # 3-dimensional self.search = NearestNeighborSearchOnKDTree(kdtree) self.radius = radius
final double value1 = sign1 * search1.getDistance() * (1 - weight), value2 = sign2 * search2.getDistance() * weight; if (value1 + value2 > 0) { t3.setOne(); } } return img3; } """, [ Img, Cursor, ArrayList, UnsignedByteType, Views, RandomAccessible, RealPoint, NearestNeighborSearchOnKDTree, ArrayImgs, Intervals ]) edge_pix1 = w2.findEdgePixels(img1) kdtree1 = KDTree(edge_pix1, edge_pix1) search1 = NearestNeighborSearchOnKDTree(kdtree1) edge_pix2 = w2.findEdgePixels(img2) kdtree2 = KDTree(edge_pix2, edge_pix2) search2 = NearestNeighborSearchOnKDTree(kdtree2) steps = [] for weight in [x / 10.0 for x in xrange(2, 10, 2)]: steps.append(w2.makeInterpolatedImage(img1, search1, img2, search2, weight)) imp3 = IL.wrap(Views.stack([img1] + steps + [img2]), "interpolations") imp3.setDimensions(1, img1.dimensions(3), len(steps)) imp3.setDisplayRange(0, 1) imp3.show()
centers = dog.getSubpixelPeaks() # in floating-point coordinates class ConstantValueList(AbstractList): def __init__(self, constantvalue, count): self.constantvalue = constantvalue self.count = count def get(self, index): return self.constantvalue def size(self): return self.count kdtree = KDTree(ConstantValueList(inside, len(centers)), centers) class RRA(RealPoint, RealRandomAccess): def __init__(self, n_dimensions, kdtree, radius, inside, outside): super(RealPoint, self).__init__(n_dimensions) self.kdtree = kdtree self.search = NearestNeighborSearchOnKDTree(kdtree) self.radius = radius self.radius_sq = radius * radius self.inside = inside self.outside = outside def copyRealRandomAccess(self): return RRA(self.numDimensions(), self.kdtree, self.radius, self.inside, self.outside)
DogDetection.ExtremaType.MAXIMA, 10, False) # The spatial coordinates for the centers of all detected embryos centers = dog.getSubpixelPeaks() # in floating-point precision # A value for the inside of an embryo: white, in 8-bit inside = UnsignedByteType(255) # A value for the outside (the background): black, in 8-bit outside = UnsignedByteType(0) # The radius of a simulated embryo, same as sigmaLarger was above radius = 30 # or = sigmaLarger # KDTree: a data structure for fast spatial look up kdtree = KDTree([inside] * len(centers), centers) # The definition of circles (or spheres, or hyperspheres) in space class Circles(RealPoint, RealRandomAccess): def __init__(self, n_dimensions, kdtree, radius, inside, outside): super(RealPoint, self).__init__(n_dimensions) self.search = NearestNeighborSearchOnKDTree(kdtree) self.radius = radius self.radius_squared = radius * radius self.inside = inside self.outside = outside def copyRealRandomAccess(self): return Circles(self.numDimensions(), self.kdtree, self.radius, self.inside, self.outside)
from net.imglib2.algorithm.math.ImgMath import compute, gen from net.imglib2.img.display.imagej import ImageJFunctions as IL from net.imglib2.type.numeric.integer import UnsignedByteType from net.imglib2.img.array import ArrayImgs from net.imglib2.util import Intervals from net.imglib2 import KDTree, Point locations = [(10, 15), (25, 40), (30, 75), (80, 60)] points = [ Point.wrap([x, y]) for x, y in [(10, 15), (25, 40), (30, 75), (80, 60)] ] values = [UnsignedByteType(v) for v in [128, 164, 200, 255]] kt = KDTree(values, points) dimensions = [100, 100] op = gen(kt, 10) target = ArrayImgs.unsignedBytes( Intervals.dimensionsAsLongArray(op.getInterval())) compute(op).into(target) IL.wrap(target, "KDTree").show()
def buildSearch(img): edge_pix = findEdgePixels(img) kdtree = KDTree(edge_pix, edge_pix) return NearestNeighborSearchOnKDTree(kdtree)