Beispiel #1
0
    def process(self, image, seeds=None):
        self.objects = None
        self.labelImage = None
        duplicator = Duplicator()
        spots = duplicator.run(image)
        IJ.setMinAndMax(spots, 0, 1)
        IJ.run(spots, "16-bit", "")
        spot3DImage = ImageHandler.wrap(spots)
        if seeds != None:
            seed3DImage = ImageHandler.wrap(seeds)
        else:
            seed3DImage = self.__computeSeeds(spots)

        algorithm = Segment3DSpots(spot3DImage, seed3DImage)
        algorithm.show = False
        algorithm.setSeedsThreshold(self.seedsThreshold)
        algorithm.setLocalThreshold(self.localBackground)
        algorithm.setWatershed(self.watershed)
        algorithm.setVolumeMin(self.volumeMin)
        algorithm.setVolumeMax(self.volumeMax)
        algorithm.setMethodLocal(Segment3DSpots.LOCAL_CONSTANT)
        algorithm.setMethodSeg(Segment3DSpots.SEG_MAX)
        algorithm.segmentAll()
        self.objects = algorithm.getObjects()
        self.labelImage = ImagePlus("OM_" + spots.getTitle(), algorithm.getLabelImage().getImageStack())

        return self.labelImage
Beispiel #2
0
def find_maxima(cs, rad, thresh):
    # type: (CellStack, int, float) -> list
    """
Find maxima in the entire stack with given radius. Exclude peaks below thresh

    :param cs: CellStack

    :param rad: Maxima search radius

    :param thresh: Intensity threshold

    :return: List of maxima 3D coordinates
    """

    imh = ImageHandler.wrap(cs.duplicate())
    radXY = rad
    radZ = rad * cs.scaleZ

    # in MaximaFinder thresh is the noise tolerance value
    mf = MaximaFinder(imh, radXY, radZ, thresh)
    peaks_array = mf.getListPeaks()
    peaks = []
    # check toArray() call functioning
    for p in peaks_array.toArray():
        if p.getValue() >= thresh:
            point = p.getPosition()
            peaks.append([l for l in point.getArray()])

    peaks_list = list(map(lambda x: [int(i) for i in x], peaks))
    peaks_list.append(cs.center)
    return peaks_list
Beispiel #3
0
def neighborhood_mean(cs, r1, r0=0):
    # type: (CellStack, int, int) -> float
    """
Returns the mean of the values inside a sphere (or sphere cap if r0 != 0 provided) around the center
Reference at: https://github.com/mcib3d/mcib3d-core/blob/master/src/main/java/mcib3d/image3d/ImageHandler.java

    :param r1: external radius

    :param r0: internal radius (if sphere cap it must be gt 0)
    """
    imh = ImageHandler.wrap(cs)

    if r0 == 0:
        neigh = imh.getNeighborhoodSphere(cs.center[0], cs.center[1],
                                          cs.center[2], r1, r1, r1 * cs.scaleZ)
        mean = neigh.getMean()
    else:
        neigh = imh.getNeighborhoodLayer(cs.center[0], cs.center[1],
                                         cs.center[2], r0, r1)
        mean = neigh.getMean()

    return mean
Beispiel #4
0
    def __computeSeeds(self, spots):
        seeds = FastFilters3D.filterIntImageStack(
            spots.getImageStack(), FastFilters3D.MAXLOCAL, self.seedRadius, self.seedRadius, self.seedRadius, 0, False
        )

        return ImageHandler.wrap(seeds)
Beispiel #5
0
def mean_shift(cs, radius, peaks, sigma, thresh):
    # type: (CellStack, int, list, float, float) -> list
    """
Perform mean shift algorithm starting from the peaks to determine the centroid of the cell
This implementation is slightly different from the naive algorithm since mean shift values are also weighted by
voxel intensity (mass of the points).
The kernel used is Gaussian Kernel.

    :param cs: CellStack containing voxels

    :param radius: Look-distance for mean shift seeds neighbors selection

    :param peaks:  Seeds of the algorithm

    :param sigma: Gaussian kernel parameter

    :param thresh: Voxel which intensity is below thresh are not considered

    :return: Shifted seeds
    """

    imh = ImageHandler.wrap(cs)

    # copy peaks list
    X = list(peaks)

    past_X = []
    n_iterations = 15
    for it in range(n_iterations):
        for i, x in enumerate(X):
            point_x = Point3D(x[0], x[1], x[2])

            # for each point x in X, find the neighboring points N(x) of x.
            neighbors = imh.getNeighborhoodLayerList(x[0], x[1], x[2], 0,
                                                     radius)

            # for each point x in X, calculate the mean shift m(x).
            numerator = [0] * 3
            denominator = 0
            neigh_arr = neighbors.toArray()

            for neighbor in neigh_arr:
                # discard neighbors below certain thresh
                if neighbor.getValue() >= thresh:

                    # neighbor is a Voxel3D Object, getPosition is a Point3D. For reference,
                    # see https://github.com/mcib3d/mcib3d-core/blob/master/src/main/java/mcib3d/geom/Voxel3D.java

                    neigh_pos = [p for p in neighbor.getPosition().getArray()]
                    distance = point_x.distance(neighbor, 1, cs.scaleZ)
                    weight = gaussian_kernel(distance, sigma)
                    inc = list(
                        map(lambda n: n * (weight * neighbor.getValue()),
                            neigh_pos))
                    numerator = list(map(lambda a, b: a + b, numerator, inc))
                    denominator += weight * neighbor.getValue()

            # print("Denominator: " + str(denominator))
            new_x = list(map(lambda n: int(n / denominator), numerator))

            # for each point x in X, update x = m(x).
            X[i] = new_x

        past_X.append(list(X))

    return X