Search in sources :

Example 31 with ArrayUtil

use of mcib3d.utils.ArrayUtil in project mcib3d-core by mcib3d.

the class Watershed3D_old method updateAssociationBorder.

private void updateAssociationBorder(int px, int py, int pz, int label) {
    // int label = watershedImage.getPixelInt(px, py, pz);
    // get neighboring borders
    int sx = Math.max(0, px - 1);
    int sy = Math.max(0, py - 1);
    int sz = Math.max(0, pz - 1);
    int ex = Math.min(watershedImage.sizeX - 1, px + 1);
    int ey = Math.min(watershedImage.sizeY - 1, py + 1);
    int ez = Math.min(watershedImage.sizeZ - 1, pz + 1);
    for (int x = sx; x <= ex; x++) {
        for (int y = sy; y <= ey; y++) {
            for (int z = sz; z <= ez; z++) {
                if (watershedImage.getPixel(x, y, z) == BORDER) {
                    ArrayUtil neigh = watershedImage.getNeighborhood3x3x3(x, y, z);
                    // update all associations
                    for (int i = 0; i < neigh.getSize(); i++) {
                        int val = (int) neigh.getValue(i);
                        if ((val != BORDER) && (val != NO_LABEL) && (val != label)) {
                            // always max value first
                            String asso;
                            AssociationRegion assoR = new AssociationRegion();
                            assoR.addRegion(label);
                            assoR.addRegion(val);
                            if (label > val) {
                                asso = label + "_" + val;
                            } else {
                                asso = val + "_" + label;
                            }
                            // test if association exists already
                            boolean ok = true;
                            for (String S : associations) {
                                if ((S.compareTo(asso)) == 0) {
                                    ok = false;
                                    break;
                                }
                            }
                            if (ok) {
                                associations.add(asso);
                            }
                            assoRegions.addAssoRegion(assoR);
                        }
                    }
                }
            }
        }
    }
}
Also used : ArrayUtil(mcib3d.utils.ArrayUtil)

Example 32 with ArrayUtil

use of mcib3d.utils.ArrayUtil in project mcib3d-core by mcib3d.

the class Watershed3D_old method assignWatershedFastLessAccurate.

private boolean assignWatershedFastLessAccurate(int threshold) {
    Voxel3DComparable voxel;
    int px, py, pz;
    ArrayUtil neigh;
    int max, max2;
    boolean loop = false;
    ArrayList voxelsNextRound = new ArrayList();
    for (Iterator it = voxels.iterator(); it.hasNext(); ) {
        voxel = (Voxel3DComparable) it.next();
        if (voxel.getValue() < threshold) {
            // voxelsNextRound.addAll(voxels.subList(voxels.indexOf(voxel), voxels.size()));
            break;
        // voxelsNextRound.add(voxel);
        // continue;
        }
        px = voxel.getRoundX();
        py = voxel.getRoundY();
        pz = voxel.getRoundZ();
        if (watershedImage.getPixel(px, py, pz) == NO_LABEL) {
            // 6-neighbor
            // neigh = watershedImage.getNeighborhoodCross3D(px, py, pz);
            // 26-neighbor
            neigh = watershedImage.getNeighborhood3x3x3(px, py, pz);
            max = (int) neigh.getMaximum();
            if ((max == NO_LABEL) || (max == BORDER)) {
                voxelsNextRound.add(voxel);
            } else {
                // has a neighbor already labeled // test if two differents labels around
                max2 = (int) neigh.getMaximumBelow(max);
                // only one label around
                if ((max2 == NO_LABEL) || (max2 == BORDER)) {
                    watershedImage.setPixel(px, py, pz, max);
                    loop = true;
                    // compute volumes
                    if (computeVolumes) {
                        // volumeLabels.get(max)[1]++;
                        volumeLabels.set(max, volumeLabels.get(max) + 1);
                    }
                    // get active label
                    if (computeUpdatedLabels) {
                        String la = "" + max;
                        // test if exists already
                        boolean ok = true;
                        for (String S : updatedLabels) {
                            if ((S.compareTo(la)) == 0) {
                                ok = false;
                                break;
                            }
                        }
                        if (ok) {
                            updatedLabels.add(la);
                        }
                    }
                    // add the new labels sharing a border
                    if ((max2 == BORDER) && (computeAssociation)) {
                        updateAssociationBorder(px, py, pz, max);
                    }
                // two or more labels around
                } else {
                    watershedImage.setPixel(px, py, pz, BORDER);
                    // get association
                    if (computeAssociation) {
                        String asso = max + "_" + max2;
                        AssociationRegion assoR = new AssociationRegion();
                        assoR.addRegion(max);
                        assoR.addRegion(max2);
                        max2 = (int) neigh.getMaximumBelow(max2);
                        while ((max2 != NO_LABEL) && (max2 != BORDER)) {
                            asso = asso.concat("_" + max2);
                            assoR.addRegion(max2);
                            max2 = (int) neigh.getMaximumBelow(max2);
                        }
                        // if next to border update
                        if ((max2 == BORDER) && (computeAssociation)) {
                            for (String S : asso.split("_")) {
                                updateAssociationBorder(px, py, pz, Integer.parseInt(S));
                            }
                        }
                        // test if association exists already
                        boolean ok = true;
                        for (String S : associations) {
                            if ((S.compareTo(asso)) == 0) {
                                ok = false;
                                break;
                            }
                        }
                        if (ok) {
                            associations.add(asso);
                        }
                        assoRegions.addAssoRegion(assoR);
                    }
                }
            }
        }
    }
    voxels = voxelsNextRound;
    return loop;
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Voxel3DComparable(mcib3d.geom.Voxel3DComparable) ArrayUtil(mcib3d.utils.ArrayUtil)

Example 33 with ArrayUtil

use of mcib3d.utils.ArrayUtil in project mcib3d-core by mcib3d.

the class SpatialStatistics method compute.

private void compute() {
    // Observed
    if (verbose) {
        IJ.log("Computing " + descriptor.getName() + " for observed data");
    }
    obsDesc = descriptor.compute(observed);
    int nbDesc = obsDesc.getSize();
    obsDesc.sort();
    obsCD = CDFTools.cdf(obsDesc);
    // Samples for average
    if (verbose) {
        IJ.log("Average : Computing " + descriptor.getName() + " for " + nbSamples + " " + model.getName() + " data");
    }
    // model.getSampleImage().show();
    ArrayUtil[] samplesDesc = getSamples();
    // x values for computation
    xEvals = new ArrayUtil(nbSamples * nbDesc);
    for (int i = 0; i < nbSamples; i++) {
        xEvals.insertValues(i * nbDesc, samplesDesc[i]);
    }
    xEvals.sort();
    // compute average
    averageCD = CDFTools.cdfAverage(samplesDesc, xEvals);
    // New samples for envelope and ranking
    if (verbose) {
        IJ.log("Envelope : Computing " + descriptor.getName() + " for " + nbSamples + " " + model.getName() + " data");
    }
    samplesDesc = null;
    System.gc();
    samplesDesc = getSamples();
    // uniform spaced
    double max = xEvals.getMaximum();
    int nbBins = 1000;
    xEvalsEnv = new ArrayUtil(nbBins);
    for (int i = 0; i < nbBins; i++) {
        xEvalsEnv.addValue(i, ((double) i) * max / ((double) nbBins));
    }
    samplesEnvLow = CDFTools.cdfPercentage(samplesDesc, xEvalsEnv, env / 2.0);
    samplesEnvHigh = CDFTools.cdfPercentage(samplesDesc, xEvalsEnv, 1.0 - env / 2.0);
    if (verbose) {
        IJ.log("Computing " + descriptor.getName() + " sdi");
    }
    sdi = CDFTools.SDI(obsDesc, samplesDesc, averageCD, xEvals);
}
Also used : ArrayUtil(mcib3d.utils.ArrayUtil)

Example 34 with ArrayUtil

use of mcib3d.utils.ArrayUtil in project mcib3d-core by mcib3d.

the class SpatialStatistics method getSamples.

private ArrayUtil[] getSamples() {
    final ArrayUtil[] samplesDesc = new ArrayUtil[nbSamples];
    for (int i = 0; i < nbSamples; i++) {
        if (verbose) {
            IJ.showStatus("Random population " + (i + 1));
        }
        ArrayUtil tmp = descriptor.compute(model.getSample());
        tmp.sort();
        samplesDesc[i] = tmp;
    }
    return samplesDesc;
}
Also used : ArrayUtil(mcib3d.utils.ArrayUtil)

Example 35 with ArrayUtil

use of mcib3d.utils.ArrayUtil in project mcib3d-core by mcib3d.

the class Segment3DSpots method localMean.

/**
 * Local mean for segmentation
 *
 * @param xc x coordinate of pixel
 * @param yc y coordinate of pixel
 * @param zc z coordinate of pixel
 * @return the local mean at this position
 */
public double localMean(int xc, int yc, int zc) {
    double mspot, mback;
    int sx = rawImage.sizeX;
    int sy = rawImage.sizeY;
    int sz = rawImage.sizeZ;
    float rad0 = radius0;
    float rad1 = radius1;
    float rad2 = radius2;
    double weight = localWeight;
    if (!WATERSHED) {
        // no watershed image
        // central value
        ArrayUtil neigh = rawImage.getNeighborhood(xc, yc, zc, rad0, rad0, rad0);
        mspot = neigh.getMean();
        // background value
        neigh = rawImage.getNeighborhoodLayer(xc, yc, zc, rad1, rad2);
        mback = neigh.getMean();
    } else {
        // take into account watershed image
        double rad02 = rad0 * rad0;
        double rad12 = rad1 * rad1;
        double rad22 = rad2 * rad2;
        int zd, zf, yd, yf, xd, xf;
        int nbspot = 0;
        int nbback = 0;
        double sumspot = 0;
        double sumback = 0;
        double dist;
        float pix;
        int water;
        int waterc = watershedImage.getPixelInt(xc, yc, zc);
        // compute bounding
        zd = (int) (zc - rad2);
        if (zd < 0) {
            zd = 0;
        }
        zf = (int) (zc + rad2);
        if (zf >= sz) {
            zf = sz;
        }
        yd = (int) (yc - rad2);
        if (yd < 0) {
            yd = 0;
        }
        yf = (int) (yc + rad2);
        if (yf >= sy) {
            yf = sy;
        }
        xd = (int) (xc - rad2);
        if (xd < 0) {
            xd = 0;
        }
        xf = (int) (xc + rad2);
        if (xf >= sx) {
            xf = sx;
        }
        for (int z = zd; z < zf; z++) {
            for (int y = yd; y < yf; y++) {
                for (int x = xd; x < xf; x++) {
                    dist = (x - xc) * (x - xc) + (y - yc) * (y - yc) + (z - zc) * (z - zc);
                    if (dist <= rad02) {
                        water = watershedImage.getPixelInt(x, y, z);
                        if (water == waterc) {
                            pix = rawImage.getPixel(x, y, z);
                            sumspot += pix;
                            nbspot++;
                        }
                    } else if ((dist >= rad12) && (dist <= rad22)) {
                        water = watershedImage.getPixelInt(x, y, z);
                        if (water == waterc) {
                            pix = rawImage.getPixel(x, y, z);
                            sumback += pix;
                            nbback++;
                        }
                    }
                }
            }
        }
        // test nb minimum of pixels in spot and background
        if (nbspot > 1) {
            mspot = sumspot / nbspot;
        } else {
            return -1;
        }
        if (nbback > 1) {
            mback = sumback / nbback;
        } else {
            return -1;
        }
    }
    return (mspot * weight + (1.0 - weight) * mback);
}
Also used : ArrayUtil(mcib3d.utils.ArrayUtil)

Aggregations

ArrayUtil (mcib3d.utils.ArrayUtil)66 ArrayList (java.util.ArrayList)9 Iterator (java.util.Iterator)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Voxel3D (mcib3d.geom.Voxel3D)2 Voxel3DComparable (mcib3d.geom.Voxel3DComparable)2 RoiManager (ij.plugin.frame.RoiManager)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 Point3D (mcib3d.geom.Point3D)1 Vector3D (mcib3d.geom.Vector3D)1 ImageInt (mcib3d.image3d.ImageInt)1