Search in sources :

Example 1 with Region

use of bacmman.data_structure.Region in project bacmman by jeanollion.

the class GaussianFit method runOnRegions.

/**
 * Calls {@link #run(Image, List, double, int, double, boolean, boolean, boolean, Map, boolean, boolean, int, double, double)}, on the centers of {@param peaks}
 * @param image
 * @param peaks
 * @param typicalRadius
 * @param minDistance
 * @param maxIter
 * @param lambda
 * @param termEpsilon
 * @return
 */
public static Map<Region, double[]> runOnRegions(Image image, List<Region> peaks, double typicalRadius, int fittingBoxRadius, double minDistance, boolean fitEllipse, boolean backgroundPlane, boolean fitBackground, boolean fitCenter, boolean fitAxis, boolean useRegionToEstimateRadius, int maxIter, double lambda, double termEpsilon) {
    int nDims = image.sizeZ() > 1 ? 3 : 2;
    Map<Point, Region> locObj = new HashMap<>(peaks.size());
    List<Point> peaksLoc = new ArrayList<>(peaks.size());
    for (Region o : peaks) {
        Point center = o.getCenter();
        if (center == null)
            center = o.getMassCenter(image, false);
        if (o.isAbsoluteLandMark())
            center = center.duplicate().translateRev(image);
        peaksLoc.add(center);
        locObj.put(center, o);
    }
    Map<Point, double[]> startParams = null;
    if (useRegionToEstimateRadius) {
        startParams = peaksLoc.stream().collect(Collectors.toMap(Function.identity(), p -> {
            Region region = locObj.get(p);
            // estimation of radius for circle / sphere
            double radius = nDims == 2 ? Math.sqrt(region.size() / (Math.PI)) : Math.pow(3 * region.size() / (4 * Math.PI), 1 / 3d);
            if (region instanceof Spot)
                radius = ((Spot) region).getRadius();
            if (fitEllipse) {
                double[] params = new double[6];
                for (int i = 0; i < 2; ++i) params[i] = p.get(i);
                if (false && region instanceof Ellipse2D) {
                    Ellipse2D e = (Ellipse2D) region;
                    double cos = Math.cos(e.getTheta());
                    double sin = Math.sin(e.getTheta());
                // TODO compute a, b & c
                } else {
                    params[2] = 1 / radius * radius;
                    params[3] = 1 / radius * radius;
                }
                return params;
            } else {
                double[] params = new double[nDims + 3];
                for (int i = 0; i < nDims; ++i) params[i] = p.get(i);
                params[nDims + 1] = 1 / radius * radius;
                return params;
            }
        }));
    }
    Map<Point, double[]> results = run(image, peaksLoc, typicalRadius, fittingBoxRadius, minDistance, fitEllipse, backgroundPlane, fitBackground, startParams, fitCenter, fitAxis, maxIter, lambda, termEpsilon);
    return results.entrySet().stream().peek(e -> {
        Region region = locObj.get(e.getKey());
        if (region.isAbsoluteLandMark()) {
            // translate coords
            e.getValue()[0] += image.xMin();
            e.getValue()[1] += image.yMin();
            if (nDims == 3)
                e.getValue()[2] += image.zMin();
        }
    }).collect(Collectors.toMap(e -> locObj.get(e.getKey()), Entry::getValue));
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) Logger(org.slf4j.Logger) Spot(bacmman.data_structure.Spot) LoggerFactory(org.slf4j.LoggerFactory) Ellipse2D(bacmman.data_structure.Ellipse2D) Point(bacmman.utils.geom.Point) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Utils(bacmman.utils.Utils) bacmman.image(bacmman.image) net.imglib2.algorithm.localization(net.imglib2.algorithm.localization) TriFunction(bacmman.utils.TriFunction) Entry(java.util.Map.Entry) Region(bacmman.data_structure.Region) ImgLib2ImageWrapper(bacmman.image.wrappers.ImgLib2ImageWrapper) Img(net.imglib2.img.Img) Localizable(net.imglib2.Localizable) Spot(bacmman.data_structure.Spot) Point(bacmman.utils.geom.Point) Point(bacmman.utils.geom.Point) Ellipse2D(bacmman.data_structure.Ellipse2D) Region(bacmman.data_structure.Region)

Example 2 with Region

use of bacmman.data_structure.Region in project bacmman by jeanollion.

the class RegionFactory method createSeedObjectsFromSeeds.

public static List<Region> createSeedObjectsFromSeeds(List<Point> seedsXYZ, boolean is2D, double scaleXY, double scaleZ) {
    List<Region> seedObjects = new ArrayList<>(seedsXYZ.size());
    int label = 0;
    for (Point seed : seedsXYZ) seedObjects.add(new Region(seed.asVoxel(), ++label, is2D, (float) scaleXY, (float) scaleZ));
    return seedObjects;
}
Also used : ArrayList(java.util.ArrayList) Region(bacmman.data_structure.Region) Point(bacmman.utils.geom.Point) Point(bacmman.utils.geom.Point)

Example 3 with Region

use of bacmman.data_structure.Region in project bacmman by jeanollion.

the class SubPixelLocalizator method getPeaks.

public static List<Point> getPeaks(Image img, List<Region> objects) {
    List<Point> peaks = new ArrayList<>(objects.size());
    for (Region o : objects) {
        // get max value within map
        double max = Double.NEGATIVE_INFINITY;
        Voxel maxV = null;
        for (Voxel v : o.getVoxels()) {
            double value = img.getPixel(v.x, v.y, v.z);
            if (value > max) {
                max = value;
                maxV = v;
            }
        }
        if (img.sizeZ() > 1)
            peaks.add(new Point(maxV.x, maxV.y, maxV.z));
        else
            peaks.add(new Point(maxV.x, maxV.y));
    }
    return peaks;
}
Also used : Voxel(bacmman.data_structure.Voxel) ArrayList(java.util.ArrayList) Region(bacmman.data_structure.Region) Point(net.imglib2.Point)

Example 4 with Region

use of bacmman.data_structure.Region in project bacmman by jeanollion.

the class RegionCluster method getInteface.

public static <I extends InterfaceRegion<I>> I getInteface(Region o1, Region o2, ImageInteger labelImage, InterfaceFactory<Region, I> interfaceFactory) {
    EllipsoidalNeighborhood neigh = labelImage.sizeZ() > 1 ? new EllipsoidalNeighborhood(1, 1, true) : new EllipsoidalNeighborhood(1, true);
    Region min;
    int otherLabel;
    I inter = interfaceFactory.create(o1, o2);
    if (o1.getVoxels().size() <= o2.getVoxels().size()) {
        min = o1;
        otherLabel = o2.getLabel();
    } else {
        min = o2;
        otherLabel = o1.getLabel();
    }
    int xx, yy, zz;
    for (Voxel v : min.getVoxels()) {
        for (int i = 0; i < neigh.dx.length; ++i) {
            xx = v.x + neigh.dx[i];
            yy = v.y + neigh.dy[i];
            zz = v.z + neigh.dz[i];
            if (labelImage.contains(xx, yy, zz) && labelImage.getPixelInt(xx, yy, zz) == otherLabel)
                inter.addPair(v, new Voxel(xx, yy, zz));
        }
    }
    return inter;
}
Also used : Voxel(bacmman.data_structure.Voxel) Region(bacmman.data_structure.Region) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood)

Example 5 with Region

use of bacmman.data_structure.Region in project bacmman by jeanollion.

the class RegionCluster method setInterfaces.

protected void setInterfaces(boolean background, boolean lowConnectivity) {
    Map<Integer, Region> objects = new HashMap<>();
    for (Region o : population.getRegions()) objects.put(o.getLabel(), o);
    if (background) {
        backgroundRegion = new Region(new HashSet<>(), 0, population.getImageProperties(), population.getImageProperties().sizeZ() == 1, population.getImageProperties().getScaleXY(), population.getImageProperties().getScaleZ());
        objects.put(0, backgroundRegion);
    }
    ImageInteger inputLabels = population.getLabelMap();
    Voxel n = new Voxel(0, 0, 0);
    int otherLabel;
    int[][] neigh = inputLabels.sizeZ() > 1 ? (lowConnectivity ? ImageLabeller.neigh3DLowHalf : ImageLabeller.neigh3DHalf) : (lowConnectivity ? ImageLabeller.neigh2D4Half : ImageLabeller.neigh2D8Half);
    for (Region o : population.getRegions()) {
        for (Voxel vox : o.getVoxels()) {
            for (int i = 0; i < neigh.length; ++i) {
                // only forward for interaction with other spots & background
                n.x = vox.x + neigh[i][0];
                n.y = vox.y + neigh[i][1];
                n.z = vox.z + neigh[i][2];
                if (inputLabels.contains(n.x, n.y, n.z) && foregroundMask.insideMask(n.x, n.y, n.z)) {
                    otherLabel = inputLabels.getPixelInt(n.x, n.y, n.z);
                    if (otherLabel != o.getLabel()) {
                        if (background || otherLabel != 0) {
                            // to avoid having the same instance of voxel as in the region, because voxel can overlap & voxel can be used to store values interface-wise
                            InterfaceRegion inter = getInterface(o, objects.get(otherLabel), true);
                            if (otherLabel > o.getLabel())
                                inter.addPair(vox.duplicate(), n.duplicate());
                            else
                                inter.addPair(n.duplicate(), vox.duplicate());
                        }
                    }
                } else if (background) {
                    InterfaceRegion inter = getInterface(o, objects.get(0), true);
                    inter.addPair(n.duplicate(), vox.duplicate());
                }
                if (background) {
                    // backward for background only
                    n.x = vox.x - neigh[i][0];
                    n.y = vox.y - neigh[i][1];
                    n.z = vox.z - neigh[i][2];
                    if (inputLabels.contains(n.x, n.y, n.z)) {
                        otherLabel = inputLabels.getPixelInt(n.x, n.y, n.z);
                        if (background && otherLabel == 0) {
                            InterfaceRegion inter = getInterface(o, objects.get(otherLabel), true);
                            inter.addPair(n.duplicate(), vox.duplicate());
                        }
                    } else {
                        InterfaceRegion inter = getInterface(o, objects.get(0), true);
                        inter.addPair(n.duplicate(), vox.duplicate());
                    }
                }
            }
        }
    }
    if (verbose)
        logger.debug("Interface collection: nb of interfaces:" + interfaces.size());
}
Also used : ImageInteger(bacmman.image.ImageInteger) HashMap(java.util.HashMap) Voxel(bacmman.data_structure.Voxel) Region(bacmman.data_structure.Region) ImageInteger(bacmman.image.ImageInteger) HashSet(java.util.HashSet)

Aggregations

Region (bacmman.data_structure.Region)86 RegionPopulation (bacmman.data_structure.RegionPopulation)37 SegmentedObject (bacmman.data_structure.SegmentedObject)31 List (java.util.List)23 Voxel (bacmman.data_structure.Voxel)22 Collectors (java.util.stream.Collectors)22 Point (bacmman.utils.geom.Point)20 bacmman.image (bacmman.image)19 bacmman.configuration.parameters (bacmman.configuration.parameters)17 Core (bacmman.core.Core)17 Map (java.util.Map)17 Function (java.util.function.Function)17 Image (bacmman.image.Image)16 ImageInteger (bacmman.image.ImageInteger)15 ImageMask (bacmman.image.ImageMask)15 ArrayList (java.util.ArrayList)15 ImageByte (bacmman.image.ImageByte)14 ObjectIdxTracker (bacmman.plugins.plugins.trackers.ObjectIdxTracker)13 ImageFeatures (bacmman.processing.ImageFeatures)13 Consumer (java.util.function.Consumer)13