Search in sources :

Example 1 with EllipsoidalNeighborhood

use of bacmman.processing.neighborhood.EllipsoidalNeighborhood 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 2 with EllipsoidalNeighborhood

use of bacmman.processing.neighborhood.EllipsoidalNeighborhood in project bacmman by jeanollion.

the class Region method getContour.

/**
 * @return subset of object's voxels that are in contact with background, edge or other object
 */
public Set<Voxel> getContour() {
    // 1 and not 1.5 -> diagonal
    EllipsoidalNeighborhood neigh = !is2D() ? new EllipsoidalNeighborhood(1, 1, true) : new EllipsoidalNeighborhood(1, true);
    Set<Voxel> res = new HashSet<>();
    if (voxels != null) {
        /*for (int i = 0; i<neigh.dx.length; ++i) {
                neigh.dx[i]-=mask.xMin();
                neigh.dy[i]-=mask.yMin();
                if (!is2D()) neigh.dz[i]-=mask.zMin();
            }
            for (Voxel v: getVoxels()) if (touchBorder(v.x, v.y, v.z, neigh, mask)) res.add(v);
            */
        for (Voxel v : getVoxels()) if (touchBorderVox(v, neigh))
            res.add(v);
    } else if (false && roi != null && is2D()) {
        // contour do not correspond: it is shifted 1 pixel towards high y & x values.
        return roi.getContour(getBounds());
    } else {
        getMask();
        ImageMask.loop(mask, (x, y, z) -> {
            if (touchBorder(x, y, z, neigh, mask))
                res.add(new Voxel(x + mask.xMin(), y + mask.yMin(), z + mask.zMin()));
        });
    }
    return res;
}
Also used : java.util(java.util) Roi3D(bacmman.data_structure.region_container.roi.Roi3D) RegionContainerBlankMask(bacmman.data_structure.region_container.RegionContainerBlankMask) BlankMask(bacmman.image.BlankMask) ImageInteger(bacmman.image.ImageInteger) LoopFunction(bacmman.image.BoundingBox.LoopFunction) ImageMask2D(bacmman.image.ImageMask2D) LoggerFactory(org.slf4j.LoggerFactory) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood) Neighborhood(bacmman.processing.neighborhood.Neighborhood) EDT(bacmman.processing.EDT) Utils(bacmman.utils.Utils) RegionContainer(bacmman.data_structure.region_container.RegionContainer) MAX_VOX_2D(bacmman.data_structure.region_container.RegionContainer.MAX_VOX_2D) RegionContainerIjRoi(bacmman.data_structure.region_container.RegionContainerIjRoi) ImageMask(bacmman.image.ImageMask) BoundingBox.getIntersection2D(bacmman.image.BoundingBox.getIntersection2D) ImageProperties(bacmman.image.ImageProperties) Filters(bacmman.processing.Filters) ImageLabeller(bacmman.image.ImageLabeller) Offset(bacmman.image.Offset) SimpleBoundingBox(bacmman.image.SimpleBoundingBox) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) SimpleOffset(bacmman.image.SimpleOffset) TypeConverter(bacmman.image.TypeConverter) Utils.comparator(bacmman.utils.Utils.comparator) Point(bacmman.utils.geom.Point) BoundingBox.intersect2D(bacmman.image.BoundingBox.intersect2D) Image(bacmman.image.Image) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) DoubleStream(java.util.stream.DoubleStream) BoundingBox(bacmman.image.BoundingBox) MutableBoundingBox(bacmman.image.MutableBoundingBox) SimpleImageProperties(bacmman.image.SimpleImageProperties) MAX_VOX_3D(bacmman.data_structure.region_container.RegionContainer.MAX_VOX_3D) RegionFactory(bacmman.processing.RegionFactory) Vector(bacmman.utils.geom.Vector) RegionContainerVoxels(bacmman.data_structure.region_container.RegionContainerVoxels) ImageByte(bacmman.image.ImageByte) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood)

Example 3 with EllipsoidalNeighborhood

use of bacmman.processing.neighborhood.EllipsoidalNeighborhood in project bacmman by jeanollion.

the class RemoveHotPixels method computeConfigurationData.

@Override
public void computeConfigurationData(int channelIdx, InputImages inputImages) {
    configMapF = new HashMapGetCreate<>(new HashMapGetCreate.SetFactory<>());
    Image median = new ImageFloat("", inputImages.getImage(channelIdx, 0));
    // excludes center pixel // only on same plane
    Neighborhood n = new EllipsoidalNeighborhood(1.5, true);
    double thld = threshold.getValue().doubleValue();
    int frameRadius = this.frameRadius.getValue().intValue();
    double fRd = (double) frameRadius;
    final Image[][] testMeanTC = testMode.testSimple() ? new Image[inputImages.getFrameNumber()][1] : null;
    final Image[][] testMedianTC = testMode.testSimple() ? new Image[inputImages.getFrameNumber()][1] : null;
    // perform sliding mean of image
    SlidingOperator<Image, Pair<Integer, Image>, Void> operator = new SlidingOperator<Image, Pair<Integer, Image>, Void>() {

        @Override
        public Pair<Integer, Image> instanciateAccumulator() {
            return new Pair(-1, new ImageFloat("", median));
        }

        @Override
        public void slide(Image removeElement, Image addElement, Pair<Integer, Image> accumulator) {
            if (frameRadius <= 1) {
                // no averaging in time
                accumulator.value = addElement;
            } else {
                if (removeElement != null && addElement != null) {
                    loop(accumulator.value.getBoundingBox().resetOffset(), (x, y, z) -> {
                        accumulator.value.setPixel(x, y, z, accumulator.value.getPixel(x, y, z) + (addElement.getPixel(x, y, z) - removeElement.getPixel(x, y, z)) / fRd);
                    }, true);
                } else if (addElement != null) {
                    loop(accumulator.value.getBoundingBox().resetOffset(), (x, y, z) -> {
                        accumulator.value.setPixel(x, y, z, accumulator.value.getPixel(x, y, z) + addElement.getPixel(x, y, z) / fRd);
                    }, true);
                } else if (removeElement != null) {
                    loop(accumulator.value.getBoundingBox().resetOffset(), (x, y, z) -> {
                        accumulator.value.setPixel(x, y, z, accumulator.value.getPixel(x, y, z) - removeElement.getPixel(x, y, z) / fRd);
                    }, true);
                }
            }
            // / keep track of current frame
            accumulator.key = accumulator.key + 1;
        }

        @Override
        public Void compute(Pair<Integer, Image> accumulator) {
            Filters.median(accumulator.value, median, n, true);
            // Filters.median(inputImages.getImage(channelIdx, accumulator.key), median, n);
            if (testMode.testSimple()) {
                testMeanTC[accumulator.key][0] = accumulator.value.duplicate();
                testMedianTC[accumulator.key][0] = median.duplicate();
            }
            loop(median.getBoundingBox().resetOffset(), (x, y, z) -> {
                float med = median.getPixel(x, y, z);
                if (accumulator.value.getPixel(x, y, z) - med >= thld) {
                    Voxel v = new Voxel(x, y, z, med);
                    for (int f = Math.max(0, accumulator.key - frameRadius); f <= accumulator.key; ++f) {
                        configMapF.getAndCreateIfNecessary(f).add(v);
                    // Set<Voxel> set = configMapF.getAndCreateIfNecessarySync(f);
                    // synchronized (set) {set.add(v);}
                    }
                }
            });
            // not parallele
            return null;
        }
    };
    List<Image> imList = Arrays.asList(InputImages.getImageForChannel(inputImages, channelIdx, false));
    if (frameRadius >= 1)
        SlidingOperator.performSlideLeft(imList, frameRadius, operator);
    else
        ThreadRunner.parallelExecutionBySegments(i -> operator.compute(new Pair<>(i, imList.get(i))), 0, imList.size(), 100);
    if (testMode.testSimple()) {
        // first frames are not computed
        for (int f = 0; f < frameRadius - 1; ++f) testMeanTC[f][0] = testMeanTC[frameRadius - 1][0];
        for (int f = 0; f < frameRadius - 1; ++f) testMedianTC[f][0] = testMedianTC[frameRadius - 1][0];
        Core.showImage5D("Sliding median", testMedianTC);
        Core.showImage5D("Sliding mean", testMeanTC);
        logger.debug("number of dead voxels detected: {}", configMapF.size());
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood) Neighborhood(bacmman.processing.neighborhood.Neighborhood) HashMapGetCreate(bacmman.utils.HashMapGetCreate) Voxel(bacmman.data_structure.Voxel) Pair(bacmman.utils.Pair) ConfigurableTransformation(bacmman.plugins.ConfigurableTransformation) Core(bacmman.core.Core) ImageFloat(bacmman.image.ImageFloat) Map(java.util.Map) Median(bacmman.processing.Filters.Median) Filters(bacmman.processing.Filters) BoundingBox.loop(bacmman.image.BoundingBox.loop) TestableOperation(bacmman.plugins.TestableOperation) SlidingOperator(bacmman.utils.SlidingOperator) ThreadRunner(bacmman.utils.ThreadRunner) Hint(bacmman.plugins.Hint) Set(java.util.Set) Parameter(bacmman.configuration.parameters.Parameter) Image(bacmman.image.Image) List(java.util.List) InputImages(bacmman.data_structure.input_image.InputImages) NumberParameter(bacmman.configuration.parameters.NumberParameter) BoundedNumberParameter(bacmman.configuration.parameters.BoundedNumberParameter) Image(bacmman.image.Image) SlidingOperator(bacmman.utils.SlidingOperator) ImageFloat(bacmman.image.ImageFloat) Hint(bacmman.plugins.Hint) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood) Neighborhood(bacmman.processing.neighborhood.Neighborhood) Voxel(bacmman.data_structure.Voxel) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood) Pair(bacmman.utils.Pair)

Example 4 with EllipsoidalNeighborhood

use of bacmman.processing.neighborhood.EllipsoidalNeighborhood in project bacmman by jeanollion.

the class Curvature method getCurvatureWatershedMap.

public static ImageFloat getCurvatureWatershedMap(final Image edm, final ImageInteger mask, KDTree<Double> curvature) {
    final ImageFloat res = new ImageFloat("CurvatureWatershedMap", edm);
    final NearestNeighborSearchOnKDTree<Double> search = new NearestNeighborSearchOnKDTree(curvature);
    final TreeSet<Voxel> heap = new TreeSet<>(Voxel.getComparator());
    final EllipsoidalNeighborhood neigh = new EllipsoidalNeighborhood(1.5, true);
    // initialize with the border of objects
    BoundingBox.loop(mask.getBoundingBox().resetOffset(), (int x, int y, int z) -> {
        if (mask.insideMask(x, y, z) && neigh.hasNullValue(x, y, z, mask, true)) {
            double edmValue = edm.getPixel(x, y, z);
            search.search(new Point(x + mask.xMin(), y + mask.yMin()));
            res.setPixel(x, y, z, search.getSampler().get());
            Voxel next;
            for (int i = 0; i < neigh.getSize(); ++i) {
                next = new Voxel(x + neigh.dx[i], y + neigh.dy[i], 0);
                if (!mask.contains(next.x, next.y, next.z) || !mask.insideMask(x, y, z))
                    continue;
                next.value = edm.getPixel(next.x, next.y, 0);
                if (next.value > edmValue)
                    heap.add(next);
            }
        }
    });
    Voxel next;
    while (!heap.isEmpty()) {
        Voxel v = heap.pollFirst();
        double value = 0, count = 0;
        for (int i = 0; i < neigh.getSize(); ++i) {
            next = new Voxel(v.x + neigh.dx[i], v.y + neigh.dy[i], 0);
            next.value = edm.getPixel(next.x, next.y, next.z);
            if (next.value > v.value)
                heap.add(next);
            else {
                value += res.getPixel(next.x, next.y, 0);
                ++count;
                if (count > 0)
                    res.setPixel(v.x, v.y, 0, value / count);
            }
        }
    }
    return res;
}
Also used : NearestNeighborSearchOnKDTree(net.imglib2.neighborsearch.NearestNeighborSearchOnKDTree) Voxel(bacmman.data_structure.Voxel) TreeSet(java.util.TreeSet) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood) Point(net.imglib2.Point) RealPoint(net.imglib2.RealPoint) ImageFloat(bacmman.image.ImageFloat) Point(net.imglib2.Point) RealPoint(net.imglib2.RealPoint)

Example 5 with EllipsoidalNeighborhood

use of bacmman.processing.neighborhood.EllipsoidalNeighborhood in project bacmman by jeanollion.

the class MultiScaleWatershedTransform method run.

public void run() {
    double rad = lowConnectivity ? 1 : 1.5;
    EllipsoidalNeighborhood neigh = segmentedMap.sizeZ() > 1 ? new EllipsoidalNeighborhood(rad, rad, true) : new EllipsoidalNeighborhood(rad, true);
    for (Spot s : spots) {
        if (s != null) {
            for (Voxel v : s.voxels) {
                for (int i = 0; i < neigh.getSize(); ++i) {
                    Voxel n = new Voxel(v.x + neigh.dx[i], v.y + neigh.dy[i], v.z + neigh.dz[i]);
                    if (segmentedMap.contains(n.x, n.y, n.z) && mask.insideMask(n.x, n.y, n.z)) {
                        n.value = watershedMaps[s.scale].getPixel(n.x, n.y, n.z);
                        heap.add(n);
                    }
                }
            }
        }
    }
    Score score = generateScore();
    List<Voxel> nextProp = new ArrayList<>(neigh.getSize());
    Set<Integer> surroundingLabels = fusionCriterion == null || fusionCriterion instanceof DefaultFusionCriterion ? null : new HashSet<>(neigh.getSize());
    while (!heap.isEmpty()) {
        // Voxel v = heap.poll();
        Voxel v = heap.pollFirst();
        if (segmentedMap.getPixelInt(v.x, v.y, v.z) > 0)
            continue;
        score.setUp(v);
        for (int i = 0; i < neigh.getSize(); ++i) {
            Voxel n = new Voxel(v.x + neigh.dx[i], v.y + neigh.dy[i], v.z + neigh.dz[i]);
            if (segmentedMap.contains(n.x, n.y, n.z) && mask.insideMask(n.x, n.y, n.z)) {
                int nextLabel = segmentedMap.getPixelInt(n.x, n.y, n.z);
                if (nextLabel > 0) {
                    if (surroundingLabels != null)
                        surroundingLabels.add(nextLabel);
                    score.add(n, nextLabel);
                } else
                    nextProp.add(n);
            }
        }
        int currentLabel = score.getLabel();
        spots[currentLabel].addVox(v);
        // check propagation criterion
        for (Voxel n : nextProp) {
            n.value = watershedMaps[spots[currentLabel].scale].getPixel(n.x, n.y, n.z);
            if (propagationCriterion.continuePropagation(v, n)) {
                // if already present in set -> was accessed from lower value -> priority
                if (!heap.contains(n))
                    heap.add(n);
            }
        }
        nextProp.clear();
        // check fusion criterion
        if (surroundingLabels != null) {
            surroundingLabels.remove(currentLabel);
            if (!surroundingLabels.isEmpty()) {
                Spot currentSpot = spots[currentLabel];
                for (int otherLabel : surroundingLabels) {
                    if (fusionCriterion.checkFusionCriteria(currentSpot, spots[otherLabel], v)) {
                        currentSpot = currentSpot.fusion(spots[otherLabel]);
                    }
                }
                surroundingLabels.clear();
            }
        }
    }
}
Also used : ImageInteger(bacmman.image.ImageInteger) Voxel(bacmman.data_structure.Voxel) ArrayList(java.util.ArrayList) EllipsoidalNeighborhood(bacmman.processing.neighborhood.EllipsoidalNeighborhood)

Aggregations

EllipsoidalNeighborhood (bacmman.processing.neighborhood.EllipsoidalNeighborhood)21 Neighborhood (bacmman.processing.neighborhood.Neighborhood)9 Point (bacmman.utils.geom.Point)7 Voxel (bacmman.data_structure.Voxel)6 Image (bacmman.image.Image)6 ImageInteger (bacmman.image.ImageInteger)6 ImageByte (bacmman.image.ImageByte)5 ImageFloat (bacmman.image.ImageFloat)5 Filters (bacmman.processing.Filters)5 Parameter (bacmman.configuration.parameters.Parameter)3 Region (bacmman.data_structure.Region)3 bacmman.image (bacmman.image)3 ImageMask (bacmman.image.ImageMask)3 ImageProperties (bacmman.image.ImageProperties)3 Offset (bacmman.image.Offset)3 SimpleBoundingBox (bacmman.image.SimpleBoundingBox)3 SimpleImageProperties (bacmman.image.SimpleImageProperties)3 SimpleOffset (bacmman.image.SimpleOffset)3 Utils (bacmman.utils.Utils)3 Vector (bacmman.utils.geom.Vector)3