def find_peaks(imp1, imp2, sigmaSmaller, sigmaLarger, minPeakValue): # FIND PEAKS # sigmaSmaller ==> Size of the smaller dots (in pixels) # sigmaLarger ==> Size of the bigger dots (in pixels) # minPeakValue ==> Intensity above which to look for dots # Preparation Neuron channel ip1_1 = IL.wrapReal(imp1) ip1E = Views.extendMirrorSingle(ip1_1) imp1.show() #Preparation Glioma channel ip2_1 = IL.wrapReal(imp2) ip2E = Views.extendMirrorSingle(ip2_1) imp2.show() calibration = [1.0 for i in range(ip1_1.numDimensions())] extremaType = DogDetection.ExtremaType.MINIMA normalizedMinPeakValue = False dog_1 = DogDetection(ip1E, ip1_1, calibration, sigmaSmaller, sigmaLarger, extremaType, minPeakValue, normalizedMinPeakValue) dog_2 = DogDetection(ip2E, ip2_1, calibration, sigmaSmaller, sigmaLarger, extremaType, minPeakValue, normalizedMinPeakValue) peaks_1 = dog_1.getPeaks() peaks_2 = dog_2.getPeaks() return ip1_1, ip2_1, peaks_1, peaks_2
def createDoG(img, calibration, sigmaSmaller, sigmaLarger, minPeakValue): # Fixed parameters extremaType = DogDetection.ExtremaType.MAXIMA normalizedMinPeakValue = False # Infinite img imgE = Views.extendMirrorSingle(img) # In the differece of gaussian peak detection, the img acts as the interval # within which to look for peaks. The processing is done on the infinite imgE. return DogDetection(imgE, img, calibration, sigmaLarger, sigmaSmaller, extremaType, minPeakValue, normalizedMinPeakValue)
from net.imglib2.img.display.imagej import ImageJFunctions as IL from net.imglib2.view import Views from net.imglib2.algorithm.dog import DogDetection from jarray import zeros # Load a greyscale single-channel image: the "Embryos" sample image imp = IJ.openImage("https://imagej.nih.gov/ij/images/embryos.jpg") imp.show() # Convert it to 8-bit IJ.run(imp, "8-bit", "") # Access its pixel data from an ImgLib2 data structure: a RandomAccessibleInterval img = IL.wrapReal(imp) # View as an infinite image, mirrored at the edges which is ideal for Gaussians imgE = Views.extendMirrorSingle(img) # Parameters for a Difference of Gaussian to detect embryo positions calibration = [1.0 for i in range(img.numDimensions())] # no calibration: identity sigmaSmaller = 15 # in pixels: a quarter of the radius of an embryo sigmaLarger = 30 # pixels: half the radius of an embryo extremaType = DogDetection.ExtremaType.MAXIMA minPeakValue = 10 normalizedMinPeakValue = False # In the differece of gaussian peak detection, the img acts as the interval # within which to look for peaks. The processing is done on the infinite imgE. dog = DogDetection(imgE, img, calibration, sigmaSmaller, sigmaLarger, extremaType, minPeakValue, normalizedMinPeakValue)
from net.imglib2.img.display.imagej import ImageJFunctions as IL from net.imglib2.view import Views from org.scijava.plugins.scripteditor.jython import JythonDev JythonDev.debug = 2 # Grab the ImagePlus from the most recently activated Fiji image window imp = IJ.getImage() # Convert the image to an ImgLib2 image img = IL.wrap(imp) # Show it IL.show(img, "wrapped as an ImgLib2 image") # Extend it: an infinite view extendedView = Views.extendMirrorSingle(img) # RandomAccessible # View with a larger canvas 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:
#@ OpService ops #@ SCIFIO scifio #@ String input_path #@ String output_path #@ UIService ui #@ DatasetService datasets from net.imglib2.type.numeric.real import FloatType from net.imglib2.type.numeric.integer import UnsignedByteType from net.imglib2.view import Views input_dataset = scifio.datasetIO().open(input_path) kernel_a = ops.create().kernelGauss([0, 0]) a = ops.create().img(input_dataset) ops.filter().convolve(a, Views.extendMirrorSingle(input_dataset), kernel_a) kernel_b = ops.create().kernelGauss([2, 2]) b = ops.create().img(input_dataset) ops.filter().convolve(b, Views.extendMirrorSingle(input_dataset), kernel_b) difference_of_gaussian = ops.math().subtract(a, b) ui.show(input_dataset) ui.show(a) ui.show(b) ui.show(difference_of_gaussian)
import os from net.imglib2.type.numeric.real import FloatType from net.imglib2.view import Views try: os.unlink(output_path) except OSError: pass rgb = scifio.datasetIO().open(input_path) grayscale = ops.run("convert.rgb2grayscale", rgb) grayscale = ops.convert().float32(grayscale) rgb = None gauss_kernel = ops.create().kernel([ [1 / 256.0, 4 / 256.0, 6 / 256.0, 4 / 256.0, 1 / 256.0], [4 / 256.0, 14 / 256.0, 24 / 256.0, 14 / 256.0, 4 / 256.0], [6 / 256.0, 24 / 256.0, 36 / 256.0, 24 / 256.0, 6 / 256.0], [4 / 256.0, 14 / 256.0, 24 / 256.0, 14 / 256.0, 4 / 256.0], [1 / 256.0, 4 / 256.0, 6 / 256.0, 4 / 256.0, 1 / 256.0] ], FloatType()) without_noise = ops.create().img(grayscale) ops.filter().convolve(without_noise, Views.extendMirrorSingle(grayscale), gauss_kernel) edges = ops.run("edgeDetector", without_noise) scifio.datasetIO().save(datasets.create(ops.convert().uint8(ops.math().multiply(edges, 255))), output_path) ui.show(edges) print("OK")
except OSError: pass start = Measure.start("total_op") input_dataset = scifio.datasetIO().open(input_path) input_dataset = ops.convert().float32(input_dataset) preprocess = ops.create().img(input_dataset) minMax = ops.stats().minMax(input_dataset) input_dataset = ops.image().normalize(preprocess, input_dataset, minMax.getA(), minMax.getB(), FloatType(0), FloatType(1.0)) ui.show(preprocess) gauss_kernel = ops.create().kernelGauss([sigma, sigma]) print(gauss_kernel) without_noise = ops.create().img(input_dataset) ops.filter().convolve(without_noise, Views.extendMirrorSingle(input_dataset), gauss_kernel) input_dataset = None ui.show(without_noise) edges = ops.run("edgeDetector", without_noise, low_threshold, high_threshold) ui.show(edges) scifio.datasetIO().save( datasets.create(ops.convert().uint8(ops.math().multiply( edges, FloatType(255)))), output_path) Measure.end(start) print("OK")
# add constant input = ops.math().add(input, UnsignedByteType(100)) ui.show(input) # normalization normalized = ops.create().img(input) ops.image().normalize(normalized, input) ui.show(normalized) # convolution kernel = ops.run("create.img", [3, 3], FloatType()) for p in kernel: p.set(1.0 / kernel.size()) convoluted = ops.create().img(normalized) ops.filter().convolve(convoluted, Views.extendMirrorSingle(normalized), kernel) ui.show(convoluted) # min filter minfiltered = ops.create().img(convoluted) ops.filter().min(minfiltered, input, RectangleShape(3, False)) ui.show(minfiltered) # threshold thresholded = ops.create().img(minfiltered, BitType()) ops.threshold().apply(thresholded, minfiltered, UnsignedByteType(128)) ui.show(thresholded) # project dims = Intervals.dimensionsAsLongArray(thresholded) projected_dims = dims[:-1]
# Obtain 2D coordinates and map into the span range, normalized into the [0, 1] range # (which will be used as the linearized 2*pi length of the circumference) x = (index % self.diameter) / self.diameter y = (index / self.diameter) / self.diameter # Start at 2, with each sin or cos adding from -1 to +1 each, then normalize and expand into 8-bit range # (subtract -0.25 to center the white peak) return int((2 + sin((x - 0.25) * 2 * pi) + sin( (y - 0.25) * 2 * pi)) / 4.0 * 255) def setValue(self, index, value): pass # Define the dimensions of the repeat dimensions = [64, 64] data = Sinusoid(dimensions[0]) # Construct an 8-bit image t = UnsignedByteType() img = ArrayImg(data, dimensions, t.getEntitiesPerPixel()) img.setLinkedType(t.getNativeTypeFactory().createLinkedType(img)) # Define the number of repeats n_copies = 12 imgMirrored = Views.extendMirrorSingle(img) imgMosaic = Views.interval( imgMirrored, [0, 0], [d * n_copies - n_copies for d in dimensions]) # subtracts n_copies to correct for mirror single IL.wrap(imgMosaic, "sinusoid").show()