Search in sources :

Example 1 with NativeType

use of net.imglib2.type.NativeType in project volumetric-tissue-exploration-analysis by icbm-iupui.

the class Imglib2ConnectedComponents method run.

private <T extends RealType<T>, I extends NumericType<I> & NativeType<I>> void run(ImagePlus imp) {
    OpService ops;
    boolean invert = false;
    ImageJ ij = new ImageJ();
    final Img<I> ImglibOriginal = ImagePlusAdapter.wrap(imp);
    final Img<I> ImglibSegmented = ImglibOriginal.copy();
    final ImgPlus<T> imgSegemented = new ImgPlus(ImglibSegmented);
    Img segmentedImg = (Img) ij.op().threshold().otsu(imgSegemented);
    double tolerance = 5;
    int conn = 26;
    boolean dams = false;
    ImagePlus segmentedImp = ImageJFunctions.wrap((RandomAccessibleInterval<I>) segmentedImg, "Segmented");
    segmentedImp.show();
    ImageStack image = segmentedImp.getImageStack();
    // find regional minima on gradient image with dynamic value of 'tolerance' and 'conn'-connectivity
    ImageStack regionalMinima = MinimaAndMaxima3D.extendedMinima(image, tolerance, conn);
    // impose minima on gradient image
    ImageStack imposedMinima = MinimaAndMaxima3D.imposeMinima(image, regionalMinima, conn);
    // label minima using connected components (32-bit output)
    ImageStack labeledMinima = BinaryImages.componentsLabeling(regionalMinima, conn, 32);
    // apply marker-based watershed using the labeled minima on the minima-imposed
    // gradient image (the last value indicates the use of dams in the output)
    ImageStack resultStack = Watershed.computeWatershed(imposedMinima, labeledMinima, conn, dams);
    ImagePlus watershedImp = new ImagePlus("Wateshed", resultStack);
    watershedImp.show();
    final Img<I> Imglib = ImagePlusAdapter.wrap(watershedImp);
    final ImgPlus<T> img = new ImgPlus(Imglib);
    ImageJFunctions.show(img);
    // 
    // IterableInterval erodedInterval = ij.op().morphology().erode(img, new DiamondShape(1));
    // RandomAccessibleInterval erodedImg = makeRai(erodedInterval, ij);
    ImgLabeling cca = ij.op().labeling().cca(makeRai(img, ij), ConnectedComponents.StructuringElement.EIGHT_CONNECTED);
    // show result
    // ij.ui().show(cca.getIndexImg());
    ImageJFunctions.show(cca.getIndexImg());
    // get count of connected components
    LabelRegions<IntegerType> regions = new LabelRegions(cca);
    int cells = regions.getExistingLabels().size();
    // print result
    System.out.println("Counted " + cells + " cells.");
}
Also used : Img(net.imglib2.img.Img) ImageStack(ij.ImageStack) ImgPlus(net.imagej.ImgPlus) ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) OpService(net.imagej.ops.OpService) ImagePlus(ij.ImagePlus) ImageJ(net.imagej.ImageJ) IntegerType(net.imglib2.type.numeric.IntegerType) LabelRegions(net.imglib2.roi.labeling.LabelRegions)

Example 2 with NativeType

use of net.imglib2.type.NativeType in project mia by mianalysis.

the class ManualThreshold method measureSpearman.

public static <T extends RealType<T> & NativeType<T>> HashMap<String, Double> measureSpearman(DataContainer<T> data) {
    // Based on code from
    // https://github.com/fiji/Colocalisation_Analysis/blob/master/src/main/java/sc/fiji/coloc/algorithms/SpearmanRankCorrelation.java
    // (Accessed 2021-08-11)
    HashMap<String, Double> measurements = new HashMap<>();
    // get the 2 images for the calculation of Spearman's rho
    RandomAccessibleInterval<T> img1 = data.getSourceImage1();
    RandomAccessibleInterval<T> img2 = data.getSourceImage2();
    RandomAccessibleInterval<BitType> mask = data.getMask();
    TwinCursor<T> cursor = new TwinCursor<T>(img1.randomAccess(), img2.randomAccess(), Views.iterable(mask).localizingCursor());
    // Count the pixels first.
    int n = 0;
    while (cursor.hasNext()) {
        n++;
        cursor.fwd();
    }
    cursor.reset();
    // calculate Spearman's rho value
    SpearmanRankCorrelation src = new SpearmanRankCorrelation<T>();
    double rhoValue = src.calculateSpearmanRank(cursor);
    measurements.put(Measurements.SPEARMAN_RHO, rhoValue);
    measurements.put(Measurements.SPEARMAN_DF, new Double(src.getSpearmanDF(n)).doubleValue());
    measurements.put(Measurements.SPEARMAN_T_STATISTIC, src.getTStatistic(rhoValue, n));
    return measurements;
}
Also used : HashMap(java.util.HashMap) TwinCursor(net.imglib2.TwinCursor) SpearmanRankCorrelation(sc.fiji.coloc.algorithms.SpearmanRankCorrelation) BitType(net.imglib2.type.logic.BitType)

Example 3 with NativeType

use of net.imglib2.type.NativeType in project mia by mianalysis.

the class ManualThreshold method measureManders.

public static <T extends RealType<T> & NativeType<T>> HashMap<String, Double> measureManders(DataContainer<T> data) {
    // Based on code from
    // https://github.com/fiji/Colocalisation_Analysis/blob/master/src/main/java/sc/fiji/coloc/algorithms/MandersColocalization.java
    // (Accessed 2021-08-10)
    HashMap<String, Double> measurements = new HashMap<>();
    MandersColocalization<T> mc = new MandersColocalization<>();
    // get the two images for the calculation of Manders' split coefficients
    RandomAccessible<T> img1 = data.getSourceImage1();
    RandomAccessible<T> img2 = data.getSourceImage2();
    RandomAccessibleInterval<BitType> mask = data.getMask();
    TwinCursor<T> cursor = new TwinCursor<T>(img1.randomAccess(), img2.randomAccess(), Views.iterable(mask).localizingCursor());
    // calculate Manders' split coefficients without threshold, M1 and M2.
    MandersResults results = mc.calculateMandersCorrelation(cursor, img1.randomAccess().get().createVariable());
    measurements.put(Measurements.M1_ABOVE_ZERO, results.m1);
    measurements.put(Measurements.M2_ABOVE_ZERO, results.m2);
    // Calculate the thresholded Manders' split coefficients, tM1 and tM2, if
    // possible
    AutoThresholdRegression<T> autoThreshold = data.getAutoThreshold();
    if (autoThreshold != null) {
        // thresholded Manders' split coefficients, tM1 and tM2
        cursor.reset();
        results = mc.calculateMandersCorrelation(cursor, autoThreshold.getCh1MaxThreshold(), autoThreshold.getCh2MaxThreshold(), ThresholdMode.Above);
        measurements.put(Measurements.M1_ABOVE_THRESHOLD, results.m1);
        measurements.put(Measurements.M2_ABOVE_THRESHOLD, results.m2);
    }
    return measurements;
}
Also used : MandersColocalization(sc.fiji.coloc.algorithms.MandersColocalization) HashMap(java.util.HashMap) TwinCursor(net.imglib2.TwinCursor) BitType(net.imglib2.type.logic.BitType) MandersResults(sc.fiji.coloc.algorithms.MandersColocalization.MandersResults)

Example 4 with NativeType

use of net.imglib2.type.NativeType in project mia by mianalysis.

the class MaskObjects method maskObject.

public static <T extends RealType<T> & NativeType<T>> Obj maskObject(Obj inputObject, Image maskImage) {
    Objs tempObjects = new Objs("Mask objects", inputObject.getObjectCollection());
    // Creating the mask object
    Obj maskObject = tempObjects.createAndAddNewObject(inputObject.getVolumeType(), inputObject.getID());
    maskObject.setT(inputObject.getT());
    ImgPlus<T> maskImg = maskImage.getImgPlus();
    RandomAccess<T> randomAccess = maskImg.randomAccess();
    int xAx = maskImg.dimensionIndex(Axes.X);
    int yAx = maskImg.dimensionIndex(Axes.Y);
    int cAx = maskImg.dimensionIndex(Axes.CHANNEL);
    int zAx = maskImg.dimensionIndex(Axes.Z);
    int tAx = maskImg.dimensionIndex(Axes.TIME);
    // intensity
    for (Point<Integer> point : inputObject.getCoordinateSet()) {
        long[] location = new long[maskImg.numDimensions()];
        if (xAx != -1)
            location[xAx] = point.getX();
        if (yAx != -1)
            location[yAx] = point.getY();
        if (cAx != -1)
            location[cAx] = 0;
        if (zAx != -1)
            location[zAx] = point.getZ();
        if (tAx != -1)
            location[tAx] = inputObject.getT();
        randomAccess.setPosition(location);
        int value = ((UnsignedByteType) randomAccess.get()).get();
        if (value != 0) {
            try {
                maskObject.add(point);
            } catch (PointOutOfRangeException e) {
            }
        }
    }
    return maskObject;
}
Also used : PointOutOfRangeException(io.github.sjcross.common.object.volume.PointOutOfRangeException) Obj(io.github.mianalysis.mia.object.Obj) UnsignedByteType(net.imglib2.type.numeric.integer.UnsignedByteType) Objs(io.github.mianalysis.mia.object.Objs) Point(io.github.sjcross.common.object.Point)

Example 5 with NativeType

use of net.imglib2.type.NativeType in project clij2-fft by clij.

the class ImageUtility method cropSymmetric.

public static <T extends ComplexType<T> & NativeType<T>> Img<T> cropSymmetric(RandomAccessibleInterval<T> in, long[] cropSize, OpService ops) {
    long[] min = new long[cropSize.length];
    long[] max = new long[cropSize.length];
    for (int d = 0; d < cropSize.length; d++) {
        min[d] = in.dimension(d) / 2 - cropSize[d] / 2;
        max[d] = min[d] + cropSize[d] - 1;
    }
    Interval interval = new FinalInterval(min, max);
    RandomAccessibleInterval<T> cropped = Views.interval(in, interval);
    Img<T> out = ops.create().img(cropped, Util.getTypeFromInterval(cropped));
    ops.copy().rai(out, cropped);
    return out;
}
Also used : FinalInterval(net.imglib2.FinalInterval) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Interval(net.imglib2.Interval) FinalInterval(net.imglib2.FinalInterval)

Aggregations

ImgPlus (net.imagej.ImgPlus)8 ArrayImgFactory (net.imglib2.img.array.ArrayImgFactory)8 LabelRegions (net.imglib2.roi.labeling.LabelRegions)8 HyperSphereShape (net.imglib2.algorithm.neighborhood.HyperSphereShape)7 LabelRegion (net.imglib2.roi.labeling.LabelRegion)7 ArrayList (java.util.ArrayList)6 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)6 BitType (net.imglib2.type.logic.BitType)6 Shape (net.imglib2.algorithm.neighborhood.Shape)5 IntType (net.imglib2.type.numeric.integer.IntType)5 FloatType (net.imglib2.type.numeric.real.FloatType)5 ImagePlus (ij.ImagePlus)4 LUT (ij.process.LUT)4 IOException (java.io.IOException)4 Interval (net.imglib2.Interval)4 LabelRegionCursor (net.imglib2.roi.labeling.LabelRegionCursor)4 Peak (de.mpg.biochem.mars.image.Peak)3 List (java.util.List)3 ClearCLBuffer (net.haesleinhuepf.clij.clearcl.ClearCLBuffer)3 CLIJ2 (net.haesleinhuepf.clij2.CLIJ2)3