def getShiftFromViews(v1, v2): # Thread pool exe = Executors.newFixedThreadPool( Runtime.getRuntime().availableProcessors()) try: # PCM: phase correlation matrix pcm = PhaseCorrelation2.calculatePCM( v1, v2, ArrayImgFactory(FloatType()), FloatType(), ArrayImgFactory(ComplexFloatType()), ComplexFloatType(), exe) # Minimum image overlap to consider, in pixels minOverlap = v1.dimension(0) / 10 # Returns an instance of PhaseCorrelationPeak2 peak = PhaseCorrelation2.getShift(pcm, v1, v2, nHighestPeaks, minOverlap, True, True, exe) except Exception, e: print e
def getFFTFromView(v, extension, extSize, paddedDimensions, fftSize): FFTMethods.dimensionsRealToComplexFast(extSize, paddedDimensions, fftSize) fft = ArrayImgFactory(ComplexFloatType()).create(fftSize, ComplexFloatType()) FFT.realToComplex( Views.interval( PhaseCorrelation2Util.extendImageByFactor(v, extension), FFTMethods.paddingIntervalCentered( v, FinalInterval(paddedDimensions))), fft, exe) return fft
def rescale_uint8(ops, img): """ Rescale input image (imglib2) to full uint8 range """ scale_op = NormalizeScaleRealTypes() scale_op.setEnvironment(ops) scale_op.initialize() out_uint8 = ArrayImgFactory(UnsignedByteType()).create(img) ops.convert().imageType(out_uint8, img, scale_op) return out_uint8
def intoSlice(img, xOffset, yOffset): stack_slice = ArrayImgFactory( img.randomAccess().get().createVariable()).create( [canvas_width, canvas_height]) target = Views.interval( stack_slice, [xOffset, yOffset], [xOffset + img.dimension(0) - 1, yOffset + img.dimension(1) - 1]) c1 = target.cursor() c2 = img.cursor() while c1.hasNext(): c1.next().set(c2.next()) return stack_slice
def getShiftFromFFTs(fft1, fft2, v1, v2, minOverlap, nHighestPeaks): pcm = PhaseCorrelation2.calculatePCMInPlace(fft1, fft2, ArrayImgFactory(FloatType()), FloatType(), exe) peak = PhaseCorrelation2.getShift(pcm, v1, v2, nHighestPeaks, minOverlap, True, True, exe) spshift = peak.getSubpixelShift() if spshift is not None: return spshift.getFloatPosition(0), spshift.getFloatPosition(1) else: IJ.log('There is a peak.getSubpixelShift issue. sFOV ' + str(sFOV) + ' s ' + str(s)) return None
# Load an RGB or ARGB image #imp = IJ.openImage("https://imagej.nih.gov/ij/images/leaf.jpg") imp = IJ.getImage() # Access its pixel data from an ImgLib2 data structure: # a RandomAccessibleInterval<ARGBType> img = IL.wrapRGBA(imp) # Read out single channels red = Converters.argbChannel(img, 1) green = Converters.argbChannel(img, 2) blue = Converters.argbChannel(img, 3) # Create an empty image of type FloatType (floating-point values) # Here, the img is used to read out the interval: the dimensions for the new image brightness = ArrayImgFactory(FloatType()).create(img) # Compute the brightness: pick the maximum intensity pixel of every channel # and then normalize it by dividing by the number of channels compute(div(maximum([red, green, blue]), 255.0)).into(brightness) # Show the brightness image impB = IL.wrap(brightness, imp.getTitle() + " brightness") impB.show() # Compute now the image color saturation saturation = ArrayImgFactory(FloatType()).create(img) compute( let( "red", red, # store as directly readable variables (no dictionary lookups)
r1 = Rectangle(1708, 680, 1792, 1760) r2 = Rectangle(520, 248, 1660, 1652) cut1 = Views.zeroMin( Views.interval(red, [r1.x, r1.y], [r1.x + r1.width - 1, r1.y + r1.height - 1])) cut2 = Views.zeroMin( Views.interval(red, [r2.x, r2.y], [r2.x + r2.width - 1, r2.y + r2.height - 1])) # Thread pool exe = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()) try: # PCM: phase correlation matrix pcm = PhaseCorrelation2.calculatePCM(cut1, cut2, ArrayImgFactory(FloatType()), FloatType(), ArrayImgFactory(ComplexFloatType()), ComplexFloatType(), exe) # Number of phase correlation peaks to check with cross-correlation nHighestPeaks = 10 # Minimum image overlap to consider, in pixels minOverlap = cut1.dimension(0) / 10 # Returns an instance of PhaseCorrelationPeak2 peak = PhaseCorrelation2.getShift(pcm, cut1, cut2, nHighestPeaks, minOverlap, True, True, exe) print "Translation:", peak.getSubpixelShift()
) #IJ.openImage("http://pacific.mpi-cbg.de/samples/first-instar-brain.zip") # Scale the X,Y axis down to isotropy with the Z axis cal = imp.getCalibration() scale2D = cal.pixelWidth / cal.pixelDepth iso = Compute.inFloats(Scale2D(Red(ImgLib.wrap(imp)), scale2D)) #ImgLib.wrap(iso).show() # Find peaks by difference of Gaussian sigma = (cell_diameter / cal.pixelWidth) * scale2D peaks = DoGPeaks(iso, sigma, sigma * 0.5, minPeak, 1) print "Found", len(peaks), "peaks" # Copy ImgLib1 iso image into ImgLib2 copy image copy = ArrayImgFactory().create( [iso.getDimension(0), 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))
width, height = imp.getWidth(), imp.getHeight() pixelsU16 = ip.getPixels() img = ArrayImgs.unsignedShorts(pixelsU16, [width, height]) # Or creating a new pixel array from scratch from net.imglib2.img.array import ArrayImgs from jarray import zeros pixelsU16 = zeros(512 * 512, 'h') # 'h' means short[] img = ArrayImgs.unsignedShorts(pixelsU16, [512, 512]) # 2. With ArrayImgFactory: unnecessary, use ArrayImgs from net.imglib2.img.array import ArrayImgFactory from net.imglib2.type.numeric.integer import UnsignedShortType factoryU16 = ArrayImgFactory(UnsignedShortType()) img = factoryU16.create([512, 512]) # 3. With ArrayImg: low-level, shows how the enchilada is made from jarray import zeros import operator from net.imglib2.img.basictypeaccess.array import ShortArray from net.imglib2.img.basictypeaccess import ArrayDataAccessFactory from net.imglib2.img.array import ArrayImgs, ArrayImg # Dimensions dimensions = [512, 512] n_pixels = reduce(operator.mul, dimensions) # Pixel type t = UnsignedShortType() # How many pixels per unit of storage in the native array
def multiviewDeconvolution(images, blockSizes, PSF_kernels, n_iterations, lambda_val=0.0006, weights=None, filterBlocksForContent=False, PSF_type=PSFTYPE.INDEPENDENT, exe=None, printFn=syncPrint): """ Apply Bayesian-based multi-view deconvolution to the list of images, returning the deconvolved image. Uses Stephan Preibisch's library, currently available with the BigStitcher Fiji update site. images: a list of images, registered and all with the same dimensions. blockSizes: how to chop up the volume of each image for parallel processing. When None, a single block with the image dimensions is used, plus half of the transformed kernel dimensions for that view. PSF_kernels: the images containing the point spread function for each input image. Requirement: the dimensions must be an odd number. n_iterations: the number of iterations for the deconvolution. A number between 10 and 50 is desirable. The more iterations, the higher the computational cost. lambda_val: default is 0.0006 as recommended by Preibisch. weights: a list of FloatType images with the weight for every pixel. If None, then all pixels get a value of 1. filterBlocksForContent: whether to check before processing a block if the block has any data in it. Default is False. PSF_type: defaults to PSFTYPE.INDEPENDENT. exe: a thread pool for concurrent execution. If None, a new one is created, using as many threads as CPUs are available. printFn: the function to use for printing error messages. Defaults to syncPrint (thread-safe access to the built-in `print` function). Returns an imglib2 ArrayImg, or None if something went wrong. """ mvd_exe = exe if not exe: mvd_exe = newFixedThreadPool() # as many threads as CPUs try: mvd_weights = weights if not weights: mvd_weights = repeat(Views.interval(ConstantRandomAccessible(FloatType(1), images[0].numDimensions()), FinalInterval(images[0]))) for i, PSF_kernel in enumerate(PSF_kernels): for d in xrange(PSF_kernel.numDimensions()): if 0 == PSF_kernel.dimension(d) % 2: printFn("for image at index %i, PSF kernel dimension %i is not odd." % (i, d)) return None if not blockSizes: # Whole image dimensions + half of the transformed PSF kernel dimensions kernel_max = int(max(PSF_kernel.dimension(d) for d in xrange(PSF_kernel.numDimensions()) for PSF_kernel in PSF_kernels) * 2) syncPrint("kernel max dimension *2: %i" % kernel_max) blockSizes = [] for image in images: blockSizes.append([image.dimension(d) + kernel_max for d in xrange(image.numDimensions())]) syncPrint("blockSize:" + str(blockSizes[-1])) cptf = createFactory(mvd_exe, lambda_val, blockSizes[0]) # TODO which blockSize to give here? filterBlocksForContent = False # Run once with True, none were removed dviews = [DeconView(mvd_exe, img, weight, PSF_kernel, PSF_type, blockSize, 1, filterBlocksForContent) for img, blockSize, weight, PSF_kernel in izip(images, blockSizes, mvd_weights, PSF_kernels)] decon = MultiViewDeconvolutionSeq(DeconViews(dviews, mvd_exe), n_iterations, PsiInitBlurredFusedFactory(), cptf, ArrayImgFactory(FloatType())) if not decon.initWasSuccessful(): printFn("Something went wrong initializing MultiViewDeconvolution") return None else: decon.runIterations() return decon.getPSI() finally: # Only shut down the thread pool if it was created here if not exe: mvd_exe.shutdownNow()
def createFactoryFn(exe, lambda_val, blockSize): if use_cuda and cuda: return ComputeBlockSeqThreadCUDAFactory(exe, MultiViewDeconvolution.minValue, lambda_val, blockSize, cuda, HashMap(idToCudaDevice)) else: return ComputeBlockSeqThreadCPUFactory(exe, MultiViewDeconvolution.minValue, lambda_val, blockSize, ArrayImgFactory(FloatType()))
aff.set(*matrix) return aff # Transform the kernel for each view kernels = [kernel, transformPSFKernelToView(kernel, affine3D(matrices["imgB0-imgB1"])), transformPSFKernelToView(kernel, affine3D(matrices["imgB0-imgB2"])), transformPSFKernelToView(kernel, affine3D(matrices["imgB0-imgB3"]))] def deconvolve(images, kernels, name, n_iterations): # Bayesian-based multi-view deconvolution exe = newFixedThreadPool(Runtime.getRuntime().availableProcessors() -2) try: mylambda = 0.0006 blockSize = Intervals.dimensionsAsIntArray(images[0]) # [128, 128, 128] cptf = ComputeBlockSeqThreadCPUFactory(exe, mylambda, blockSize, ArrayImgFactory(FloatType())) psiInitFactory = PsiInitBlurredFusedFactory() # PsiInitAvgPreciseFactory() fails with type mismatch: UnsignedByteType (?) vs FloatType weight = Views.interval(ConstantRandomAccessible(FloatType(1), images[0].numDimensions()), FinalInterval(images[0])) filterBlocksForContent = False # Run once with True, none were removed decon_views = DeconViews([DeconView(exe, img, weight, kernel, PSFTYPE.INDEPENDENT, blockSize, 1, filterBlocksForContent) for img in images], exe) #n_iterations = 10 decon = MultiViewDeconvolutionSeq(decon_views, n_iterations, psiInitFactory, cptf, ArrayImgFactory(FloatType())) if not decon.initWasSuccessful(): print "Something went wrong initializing MultiViewDeconvolution" else: decon.runIterations() img = decon.getPSI() imp = IL.wrap(img, name + "_deconvolved_" + str(n_iterations) + "_iterations") imp.show()
def intoImg(pixels): img = ArrayImgFactory(UnsignedByteType()).create([4, 4]) System.arraycopy(pixels, 0, img.update(None).getCurrentStorageArray(), 0, len(pixels)) return img
from net.imglib2.img.array import ArrayImgFactory from net.imglib2.type.numeric.integer import UnsignedByteType, UnsignedShortType from net.imglib2.util import Intervals # An 8-bit 256x256x256 volume img = ArrayImgFactory(UnsignedByteType()).create([256, 256, 256]) # Another image of the same type and dimensions, but empty img2 = img.factory().create( [img.dimension(d) for d in xrange(img.numDimensions())]) # Same, but easier reading of the image dimensions img3 = img.factory().create(Intervals.dimensionsAsLongArray(img)) # Same, but use an existing img as an Interval from which to read out the dimensions img4 = img.factory().create(img) # Now we change the type: same kind of image and same dimensions, # but crucially a different pixel type (16-bit) via a new ImgFactory imgShorts = img.factory().imgFactory(UnsignedShortType()).create(img)
def smooth_temporal_gradient(ops, img, sigma_xy, sigma_t, frame_start, frame_end, normalize_output): """ Smooth input image (imglib2 array) xyt with Gaussian and build temporal gradient from frame_start to frame_end """ n = img.numDimensions() assert n == 3, "Input data needs to be 3-dimensional, 2D + time" dims = [img.dimension(d) for d in range(n)] dim_x, dim_y, dim_t = dims if frame_end == -1: frame_end = dim_t if frame_end > dim_t: frame_end = dim_t assert frame_start < frame_end, "'Frame start' must be smaller than 'Frame end'" ## crop image temporally by using a View # img_crop = Views.interval(img, [0, 0, frame_start], [dim_x-1, dim_y-1, frame_end-1]) # or by cropping without view img_crop = ops.transform().crop( img, Intervals.createMinMax(0, 0, frame_start, dim_x - 1, dim_y - 1, frame_end - 1)) # create output for smoothing img_smooth = ArrayImgFactory(FloatType()).create(img_crop) # update dimensions (after cropping) dims = [img_crop.dimension(d) for d in range(n)] # smooth with Gaussian (use mirror as border treatment) Gauss3.gauss([sigma_xy, sigma_xy, sigma_t], Views.extendBorder(img_crop), img_smooth) # compute gradient along (time) axis 2 gradient = ArrayImgFactory(FloatType()).create(dims) PartialDerivative.gradientBackwardDifference( Views.extendBorder(img_smooth), gradient, 2) # separate response into growing and shrinking part by thresholding and multiplying that mask thresholded = ops.run("threshold.apply", gradient, FloatType(0.)) mask = ops.convert().float32(thresholded) grow = ops.run("math.multiply", gradient, mask) # same for shrinking, but negate befor to obtain negative part gradient_neg = ops.run("math.multiply", gradient, -1) thresholded = ops.run("threshold.apply", gradient_neg, FloatType(0.)) mask = ops.convert().float32(thresholded) shrink = ops.run("math.multiply", gradient, mask) # allign growth and shrink by offset of 1 in t dimesnion shrink = Views.interval(shrink, [0, 0, 0], [dims[0] - 1, dims[1] - 1, dims[2] - 2]) grow = Views.interval(grow, [0, 0, 1], [dims[0] - 1, dims[1] - 1, dims[2] - 1]) img_smooth = Views.interval(img_smooth, [0, 0, 0], [dims[0] - 1, dims[1] - 1, dims[2] - 2]) # concatenate output if normalize_output: shrink = rescale_uint8(ops, shrink) grow = rescale_uint8(ops, grow) img_smooth = rescale_uint8(ops, img_smooth) out = Views.stack([shrink, grow, img_smooth]) # permute channel with time to make time last dim (for back-conversion) out = Views.permute(out, 2, 3) # crop view 1st time point result (empty since using backwardDifferences) out = Views.interval(out, [0, 0, 0, 1], [out.dimension(d) - 1 for d in range(4)]) # get ImagePlus back and set correct dimensions out_imp = IJF.wrap(out, "Grow and shrink") # resize IMP now one frame less (see crop view) out_imp.setDimensions(3, 1, frame_end - frame_start - 2) out_imp.setDisplayMode(IJ.COMPOSITE) # IJ.save(out_imp, "H:/projects/032_loose_speckle/grow_and_shrink_ij.tif") return out_imp
from net.imglib2.algorithm.fft2 import FFT from net.imglib2.img.display.imagej import ImageJFunctions as IL from net.imglib2.img.array import ArrayImgFactory from net.imglib2.type.numeric.complex import ComplexFloatType from ij import IJ img = IL.wrap(IJ.getImage()) # a RealType image imgfft = FFT.realToComplex(img, ArrayImgFactory(ComplexFloatType())) IL.wrap(imgfft, "fft").show()