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.");
}
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;
}
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;
}
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;
}
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;
}
Aggregations