def dog_detection(overlay,img, imp, cal): # Create a variable of the correct type (UnsignedByteType) for the value-extended view zero = img.randomAccess().get().createVariable() # Run the difference of Gaussian cell = 8.0 # microns in diameter min_peak = 2.0 # min intensity for a peak to be considered dog = DogDetection(Views.extendValue(img, zero), img, [cal.pixelWidth, cal.pixelHeight,cal.pixelDepth], cell / 2, cell, DogDetection.ExtremaType.MINIMA, min_peak, False, DoubleType()) peaks = dog.getPeaks() roi = OvalRoi(0, 0, cell/cal.pixelWidth, cell/cal.pixelHeight) print ('Number of cells = ', len(peaks)) p = zeros(img.numDimensions(), 'i') boundRect = imp.getRoi() for peak in peaks: # Read peak coordinates into an array of integers XYZ location of spots peak.localize(p) print(p) if(boundRect is not None and boundRect.contains(p[0], p[1])): oval = OvalRoi(p[0], p[1],cell/cal.pixelWidth, cell/cal.pixelHeight) oval.setColor(Color.RED) overlay.add(oval)
def create(self, index): cell_dimensions = [ self.grid.cellDimension(0), self.grid.cellDimension(1) ] n_cols = grid.imgDimension(0) / cell_dimensions[0] x0 = (index % n_cols) * cell_dimensions[0] y0 = (index / n_cols) * cell_dimensions[1] index += 1 # 1-based slice indices in ij.ImageStack if index < 1 or index > self.stack.size(): # Return blank image: a ByteAccess that always returns 255 return Cell( cell_dimensions, [x0, y0], type('ConstantValue', (ByteAccess, ), { 'getValue': lambda self, index: 255 })()) else: # ImageJ stack slice indices are 1-based img = IL.wrap(ImagePlus("", self.stack.getProcessor(index))) # Create extended image with the padding color value imgE = Views.extendValue(img, self.t.copy()) # A view that includes the padding between slices minC = [-self.cell_padding for d in xrange(img.numDimensions())] maxC = [ img.dimension(d) - 1 + self.cell_padding for d in xrange(img.numDimensions()) ] imgP = Views.interval(imgE, minC, maxC) return Cell(cell_dimensions, [x0, y0], ProxyByteAccess(imgP, self.grid))
def findEdgePixels(img): edge_pix = [] zero = img.firstElement().createVariable() zero.setZero() imgE = Views.extendValue(img, zero) pos = zeros(img.numDimensions(), 'l') inc = partial(operator.add, 1) dec = partial(operator.add, -1) cursor = img.cursor() while cursor.hasNext(): t = cursor.next() # A pixel is on the edge of the binary mask # if it has a non-zero value if 0 == t.getByte(): continue # ... and its immediate neighbors ... cursor.localize(pos) minimum = array(imap(dec, pos), 'l') # map(dec, pos) also works, less performance maximum = array(imap(inc, pos), 'l') # map(inc, pos) also works, less performance neighborhood = Views.interval(imgE, minimum, maximum) # ... have at least one zero value: # Good performance: the "if x in <iterable>" approach stops upon finding the first x if 0 in imap(UnsignedByteType.getByte, neighborhood): edge_pix.append(RealPoint(array(list(pos), 'f'))) return edge_pix
def findEdgePixels(img): edge_pix = [] zero = img.firstElement().createVariable() zero.setZero() imgE = Views.extendValue(img, zero) pos = zeros(img.numDimensions(), 'l') inc = partial(operator.add, 1) dec = partial(operator.add, -1) cursor = img.cursor() while cursor.hasNext(): t = cursor.next() if 0 == t.getIntegerLong(): continue # Sum neighbors of non-zero pixel: if any is zero, sum is less than 27 # and we have found an edge pixel cursor.localize(pos) minimum = map(dec, pos) maximum = map(inc, pos) box = Views.interval(imgE, minimum, maximum) if sum(imap(UnsignedByteType.getIntegerLong, box)) < 27: edge_pix.append(RealPoint(array(list(pos), 'f'))) return edge_pix
iso.getDimension(1), iso.getDimension(2)], FloatType()) c1 = iso.createCursor() c2 = copy.cursor() while c1.hasNext(): c1.fwd() c2.fwd() c2.get().set(c1.getType().getRealFloat()) #ImageJFunctions.show(copy) # Measure mean intensity at every peak sphereFactory = HyperSphereNeighborhood.factory() radius = 2 intensities1 = [] copy2 = Views.extendValue(copy, FloatType(0)) ra = copy2.randomAccess() t1 = System.currentTimeMillis() for peak in peaks: sphere = sphereFactory.create( [int(peak[0]), int(peak[1]), int(peak[2])], radius, ra) s = sum(imap(FloatType.getRealFloat, sphere.cursor())) intensities1.append(float(s) / sphere.size()) t2 = System.currentTimeMillis() print "Elapsed time:", (t2 - t1), "ms" #print intensities1
rm.addRoi(boundRect) imp = IJ.getImage() cal = imp.getCalibration() # in microns img = IJF.wrap(imp) print(img.dimensions) # Create a variable of the correct type (UnsignedByteType) for the value-extended view zero = img.randomAccess().get().createVariable() # Run the difference of Gaussian cell = 30.0 # microns in diameter min_peak = 10.0 # min intensity for a peak to be considered dog = DogDetection( Views.extendValue(img, zero), img, [cal.pixelWidth, cal.pixelHeight], cell / 2, cell, DogDetection.ExtremaType.MAXIMA, #MAXIMA min_peak, False, DoubleType()) peaks = dog.getPeaks() roi = OvalRoi(0, 0, cell / cal.pixelWidth, cell / cal.pixelHeight) print('Number of cells = ', len(peaks)) p = zeros(img.numDimensions(), 'i') overlay = Overlay() imp.setOverlay(overlay)
width = img.dimension(0) # same as imp.getWidth() height = img.dimension(1) # same as imp.getHeight() # from half an image beyond 0,0 (to the left and up) to half an image beyond width,height imgExtended = Views.interval(extendedView, [-width / 2, -height / 2], [width + width / 2 - 1, height + height / 2 - 1 ]) # RandomAccessibleInterval IL.show(imgExtended, "enlarged canvas with extended mirror symmetry") # The viewing interval doesn't have to overlap with the interval where the original image is defined # For example: imgSomewhere = Views.interval(extendedView, [41000, 60000], [42000, 61000]) IL.show(imgSomewhere, "Arbitrary interval somewhere") # Other forms of extended views: extendedEmptyView = Views.extendZero(img) extendedValueView = Views.extendValue(img, 50) # Find out the pixel type and its min and max values t = img.randomAccess().get() min_value = t.getMinValue() # minimum value for the pixel type max_value = t.getMaxValue() # maximum extendedRandomView = Views.extendRandom(img, min_value, max_value) imgExtRandom = Views.interval(extendedRandomView, [-width / 2, -height / 2], [width + width / 2, height + height / 2]) IL.show(imgExtRandom, "extended with random noise")
cal = imp.getCalibration() # in microns img = IJF.wrap(imp) # Create a variable of the correct type (UnsignedByteType) for the value-extended view zero = img.randomAccess().get().createVariable() # Run the difference of Gaussian cell = 4.0 # microns in diameter min_peak = 5.0 # min intensity for a peak to be considered WhiteBackground = True if WhiteBackground: Type = DogDetection.ExtremaType.MAXIMA else: Type = DogDetection.ExtremaType.MINIMA dog = DogDetection(Views.extendValue(img, zero), img, [cal.pixelWidth, cal.pixelHeight], cell / 2, cell, Type, min_peak, False, DoubleType()) peaks = dog.getPeaks() roi = OvalRoi(0, 0, cell / cal.pixelWidth, cell / cal.pixelHeight) p = zeros(img.numDimensions(), 'i') overlay = Overlay() imp.setOverlay(overlay) regionpeak = 0 for peak in peaks: # Read peak coordinates into an array of integers peak.localize(p)
for t in img1: if 0 != t.getIntegerLong(): t.setOne() for t in img2: if 0 != t.getIntegerLong(): t.setOne() # Make both fit within the same window, centered dims1 = Intervals.dimensionsAsLongArray(img1) dims2 = Intervals.dimensionsAsLongArray(img2) dims3 = [max(a, b) for a, b in izip(dims1, dims2)] zero = UnsignedByteType(0) img1E = Views.extendValue(img1, zero) img2E = Views.extendValue(img2, zero) img1M = Views.interval( img1E, [(dim1 - dim3) / 2 for dim1, dim3 in izip(dims1, dims3)], [dim1 + (dim3 - dim1) / 2 - 1 for dim1, dim3 in izip(dims1, dims3)]) img2M = Views.interval( img2E, [(dim2 - dim3) / 2 for dim2, dim3 in izip(dims2, dims3)], [dim2 + (dim3 - dim2) / 2 - 1 for dim2, dim3 in izip(dims2, dims3)]) IL.show(img1M, "img1M") IL.show(img2M, "img2M") # Scale by half (too slow otherwise) -- ERROR: the smaller one (img1) doesn't remain centered. s = [0.5 for d in xrange(img1.numDimensions())]
separator ) coordinateTracker.addVisitor( fitTracker ) if doRender: coordinateTracker.addVisitor( matrixTracker ) coordinateTracker.addVisitor( floorTracker ) coordinateTracker.addVisitor( multiplierTracker ) coordinateTracker.addVisitor( weightsTracker ) # ImageJFunctions.show( subStrip ) tf = StripToMatrix( wholeStrip.dimension(0) / 2 ) store = ArrayImgs.doubles( subStrip.dimension( 0 ), subStrip.dimension( 1 ) ) CopyFromIntervalToInterval.copyToRealType( subStrip, store ) matrix = Views.interval( TransformView( Views.extendValue( store, DoubleType( Double.NaN ) ), tf ), FinalInterval( [subStrip.dimension(1), subStrip.dimension(1)] ) ) # ImageJFunctions.show( matrix ) # ImageJFunctions.show( subStrip ) # ImageJFunctions.show( matrix ) print "adding thread for lower=%d and upper=%d" % (lower, upper) t = Thread( target = run, args = ( matrix, startingCoordinates, coordinateTracker, options ) ) t.start() threads.append( t ) for t in threads: t.join()