Search in sources :

Example 6 with Object3DVoxels

use of mcib3d.geom.Object3DVoxels in project mcib3d-core by mcib3d.

the class FastFilters3D method filterIntImage.

public static ImageInt filterIntImage(ImageInt stackorig, int filter, Object3DVoxels obj, int nbcpus, boolean showstatus) {
    int nbToProcess = stackorig.sizeZ;
    if ((filter == TOPHAT) || (filter == CLOSEGRAY) || (filter == OPENGRAY))
        nbToProcess *= 2;
    // Timer
    final Chrono time = new Chrono(nbToProcess);
    time.start();
    final AbstractLog show = new IJStatus();
    final Object3DVoxels object = obj;
    // IJ.log("Using java filtering " + voisx + " " + voisy + " " + voisz + " " + filter + " " + nbcpus);
    final ImageInt ima = stackorig;
    ImageInt res = (ImageInt) ima.createSameDimensions();
    if ((filter == MEAN) || (filter == MEDIAN) || (filter == MIN) || (filter == MAX) || (filter == MAXLOCAL) || (filter == TOPHAT) || (filter == VARIANCE) || (filter == CLOSEGRAY) || (filter == OPENGRAY)) {
        // PARALLEL
        final ImageInt out = res;
        final AtomicInteger ai = new AtomicInteger(0);
        final int n_cpus = nbcpus == 0 ? ThreadUtil.getNbCpus() : nbcpus;
        int fi = filter;
        if ((fi == TOPHAT) || (fi == OPENGRAY)) {
            fi = MIN;
        }
        if (fi == CLOSEGRAY) {
            fi = MAX;
        }
        final int f = fi;
        final int dec = (int) Math.ceil((double) ima.sizeZ / (double) n_cpus);
        Thread[] threads = ThreadUtil.createThreadArray(n_cpus);
        for (int ithread = 0; ithread < threads.length; ithread++) {
            threads[ithread] = new Thread() {

                @Override
                public void run() {
                    for (int k = ai.getAndIncrement(); k < n_cpus; k = ai.getAndIncrement()) {
                        ima.filterGeneric(out, object, dec * k, dec * (k + 1), f, time, show);
                    }
                }
            };
        }
        ThreadUtil.startAndJoin(threads);
        // TOPHAT MAX
        if ((filter == TOPHAT) || (filter == OPENGRAY)) {
            final int f2 = MAX;
            final ImageInt res2 = (ImageInt) ima.createSameDimensions();
            ai.set(0);
            for (int ithread = 0; ithread < threads.length; ithread++) {
                threads[ithread] = new Thread() {

                    @Override
                    public void run() {
                        for (int k = ai.getAndIncrement(); k < n_cpus; k = ai.getAndIncrement()) {
                            out.filterGeneric(res2, object, dec * k, dec * (k + 1), f2, time, show);
                        }
                    }
                };
            }
            ThreadUtil.startAndJoin(threads);
            // TOPHAT DIFFERENCE
            if (filter == TOPHAT) {
                res = ima.substractImage(res2);
            } else {
                res = res2;
            }
        }
        // CLOSING 2nd Step
        if (filter == CLOSEGRAY) {
            final int f2 = MIN;
            final ImageInt res2 = (ImageInt) ima.createSameDimensions();
            ai.set(0);
            for (int ithread = 0; ithread < threads.length; ithread++) {
                threads[ithread] = new Thread() {

                    @Override
                    public void run() {
                        for (int k = ai.getAndIncrement(); k < n_cpus; k = ai.getAndIncrement()) {
                            out.filterGeneric(res2, object, dec * k, dec * (k + 1), f2, time, show);
                        }
                    }
                };
            }
            ThreadUtil.startAndJoin(threads);
            res = res2;
        }
    } else if (filter == SOBEL) {
        res = ima.sobelFilter();
    } else if (filter == ADAPTIVE) {
    // res = ima.adaptiveFilter(voisx, voisy, voisz, nbcpus);
    }
    return res;
}
Also used : Chrono(mcib3d.utils.Chrono) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IJStatus(mcib3d.utils.Logger.IJStatus) AbstractLog(mcib3d.utils.Logger.AbstractLog) Object3DVoxels(mcib3d.geom.Object3DVoxels)

Example 7 with Object3DVoxels

use of mcib3d.geom.Object3DVoxels in project mcib3d-core by mcib3d.

the class ManualSpot method segmentSpots.

// End of variables declaration//GEN-END:variables
private void segmentSpots() {
    Roi roi;
    RoiManager manager = RoiManager.getInstance();
    Roi[] rois = manager.getRoisAsArray();
    if ((manager == null) || (rois.length == 0)) {
        IJ.showMessage("Put rois in the RoiManager");
        return;
    }
    if (objs == null) {
        objs = new Object3D[rois.length];
    }
    String text = "";
    for (int r = 0; r < rois.length; r++) {
        roi = rois[r];
        Rectangle rect = roi.getBounds();
        int x = rect.x;
        int y = rect.y;
        int z = manager.getSliceNumber(manager.getName(r));
        text = text.concat("\nx=" + x + " " + y + " " + z);
        jTextFieldLog.setText(text);
        Segment3DSpots segSpots = new Segment3DSpots(spot3DImage, null);
        segSpots.setLabelImage(labelImage);
        segSpots.show = true;
        // find local maximum
        Voxel3D v = segSpots.getLocalMaximum(x, y, z, 2, 2, 2);
        if (v != null) {
            text = text.concat("\nMax local " + v.getX() + " " + v.getY() + " " + v.getZ() + " " + v.getValue());
            jTextFieldLog.setText(text);
            x = v.getRoundX();
            y = v.getRoundY();
            z = v.getRoundZ();
        }
        // set parameter
        if (threshold_method == 0) {
            segSpots.setMethodLocal(Segment3DSpots.LOCAL_CONSTANT);
        } else if (threshold_method == 1) {
            segSpots.setMethodLocal(Segment3DSpots.LOCAL_MEAN);
        } else {
            segSpots.setMethodLocal(Segment3DSpots.LOCAL_GAUSS);
        }
        if (spot_method == 0) {
            segSpots.setMethodSeg(Segment3DSpots.SEG_CLASSICAL);
        } else if (spot_method == 0) {
            segSpots.setMethodSeg(Segment3DSpots.SEG_MAX);
        } else {
            segSpots.setMethodSeg(Segment3DSpots.SEG_BLOCK);
        }
        segSpots.setLocalThreshold(threshold_constant);
        segSpots.setGaussMaxr(rad_gauss);
        segSpots.setGaussPc(sd_gauss / 10.0);
        segSpots.setRadiusLocalMean(r0, r1, r2, weight);
        segSpots.setWatershed(false);
        // reset labelimage
        if (objs[r] != null) {
            objs[r].draw(labelImage, 0);
        // IJ.log("deleting old spot " + r);
        }
        // redraw population (in case spot has erased objects)
        popOrig.draw(labelImage);
        segSpots.setLabelImage(labelImage);
        ArrayList<Voxel3D> spot = segSpots.segmentOneSpot(x, y, z, col);
        // IJ.log("spot " + r + " at " + x + " " + y + " " + z + " : " + spot);
        if (spot != null) {
            // IJ.log("spot found, size " + spot.size());
            if (spot.size() > 0) {
                objs[r] = new Object3DVoxels(spot);
                objs[r].draw(labelImage, col);
            }
            if (plusres == null) {
                plusres = labelImage.getImagePlus();
                plusres.show();
                IJ.run(plusres, "3-3-2 RGB", "");
            }
            IJ.setMinAndMax(plusres, 0, col);
            plusres.setSlice((int) objs[r].getCenterZ() + 1);
            plusres.updateAndDraw();
        } else {
            jTextFieldLog.setText(jTextFieldLog.getText() + "\nno spot found !");
        }
    }
    labelImage.updateDisplay();
}
Also used : Rectangle(java.awt.Rectangle) Object3DVoxels(mcib3d.geom.Object3DVoxels) Voxel3D(mcib3d.geom.Voxel3D) Roi(ij.gui.Roi) RoiManager(ij.plugin.frame.RoiManager)

Example 8 with Object3DVoxels

use of mcib3d.geom.Object3DVoxels in project mcib3d-core by mcib3d.

the class F_Function method init.

@Override
public boolean init() {
    Random ra = new Random();
    Object3DVoxels maskV = new Object3DVoxels(mask);
    evaluationPoints = new Point3D[nbEvaluationpoints];
    // FIXME, check if already selected and too many points
    for (int i = 0; i < nbEvaluationpoints; ++i) {
        evaluationPoints[i] = maskV.getRandomvoxel(ra);
    }
    return true;
}
Also used : Random(java.util.Random) Object3DVoxels(mcib3d.geom.Object3DVoxels)

Example 9 with Object3DVoxels

use of mcib3d.geom.Object3DVoxels in project mcib3d-core by mcib3d.

the class Segment3DSpots method splitSpotProjection.

/**
 * NOT USED, see splitSpotWatershed
 * @param ori
 * @param val
 * @param f
 * @param new1
 * @param new2
 * @param dist
 * @param rad
 * @param dim
 * @return
 */
public static ArrayList<Voxel3D>[] splitSpotProjection(IntImage3D ori, int val, Object3D f, int new1, int new2, double dist, float rad, int dim) {
    // only for voxels object
    if (!(f instanceof Object3DVoxels)) {
        return null;
    }
    boolean debug = false;
    int p1 = f.getZmin();
    int p2 = f.getZmax();
    IntImage3D proj;
    if (dim == 2) {
        proj = ori.projectionZ(-1, p1, p2);
    } else {
        proj = ori;
    }
    // proj = (IntImage3D) proj.medianFilter(1, 1, 0);
    float radz = (dim == 2) ? 0 : rad;
    IntImage3D maxi = proj.createLocalMaximaImage(rad, rad, radz, false);
    // IntImage3D seg = f.getSegImage();
    if (debug) {
        System.out.println("Separe2D " + val);
    }
    // IntImage3D seg2D = seg.projectionZ(val, p1, p2);
    double cx1;
    double cy1;
    double cz1;
    double nb1;
    double cx2;
    double cy2;
    double cz2;
    double nb2;
    double dist1;
    double dist2;
    int nb;
    if (debug) {
        System.out.println("separe spots 2D : " + val);
    }
    // compter nb de max locaux
    /*
         * for (int j = 2; j < maxi.getSizey() - 2; j++) { for (int i = 2; i <
         * maxi.getSizex() - 2; i++) { if ((maxi.getPixel(i, j, 0) > 0) &&
         * (seg2D.getPixel(i, j, 0) > 0)) { maxi.putPixel(i, j, 0, 255); nb++; }
         * else { } } }
         *
         */
    // with ArrayList
    ArrayList<Voxel3D> list = f.getVoxels();
    ArrayList<Voxel3D> maxlo = new ArrayList<Voxel3D>();
    Iterator it = list.iterator();
    int xx, yy, zz;
    Voxel3D vox;
    Voxel3D vox1;
    while (it.hasNext()) {
        vox = (Voxel3D) it.next();
        xx = vox.getRoundX();
        yy = vox.getRoundY();
        zz = vox.getRoundZ();
        if (dim == 2) {
            zz = 0;
        }
        if (maxi.getPix(xx, yy, zz) > 0) {
            // maxi.putPixel(xx, yy, 0, 255);
            vox1 = new Voxel3D(vox);
            vox1.setZ(0);
            maxlo.add(vox1);
        // nb++;
        // } else {
        // maxi.putPixel(xx, yy, 0, 0);
        // }
        }
    }
    nb = maxlo.size();
    IJ.log("max loco " + nb);
    if (debug) {
        new ImagePlus("max loco-" + val, maxi.getStack()).show();
        // new ImagePlus("seg2D-" + val, seg2D.getStack()).show();
        // new ImagePlus("seg-" + val, seg.getStack()).show();
        new ImagePlus("proj-" + val, proj.getStack()).show();
    }
    // si 1 seul pixel on retourne false
    if (maxlo.size() < 2) {
        return null;
    }
    /*
         * Voxel3D tablo[] = new Voxel3D[nb]; nb = 0; // ranger max locaux dans
         * le tablo for (int j = 0; j < maxi.getSizey(); j++) { for (int i = 0;
         * i < maxi.getSizex(); i++) { if ((maxi.getPixel(i, j, 0) > 0)) {
         * tablo[nb] = new Voxel3D(i, j, 0, 0); nb++; } } } /*
         */
    // chercher deux max locaux les plus eloignes
    int i1 = 0;
    int i2 = 0;
    double dmax = 0.0;
    double d;
    for (int i = 0; i < nb; i++) {
        for (int j = i + 1; j < nb; j++) {
            d = maxlo.get(i).distance(maxlo.get(j));
            if (d > dmax) {
                dmax = d;
                i1 = i;
                i2 = j;
            }
        }
    }
    // Ranger les max locaux en deux classes et calculer barycentre des deux classes
    cx1 = 0;
    cy1 = 0;
    cz1 = 0;
    cx2 = 0;
    cy2 = 0;
    cz2 = 0;
    double d1;
    double d2;
    /*
         * if (debug) { System.out.println("max locaux eloignes:");
         * System.out.println("centre1: " + tablo[i1].getX() + " " +
         * tablo[i1].getY() + " " + tablo[i1].getZ());
         * System.out.println("centre2: " + tablo[i2].getX() + " " +
         * tablo[i2].getY() + " " + tablo[i2].getZ());
         * System.out.println("distance : " + tablo[i1].distance(tablo[i2])); }
         *
         */
    Voxel3D PP1 = new Voxel3D(maxlo.get(i1).getX(), maxlo.get(i1).getY(), maxlo.get(i1).getZ(), 0.0);
    Voxel3D PP2 = new Voxel3D(maxlo.get(i2).getX(), maxlo.get(i2).getY(), maxlo.get(i2).getZ(), 0.0);
    Voxel3D P1 = new Voxel3D(cx1, cy1, cz1, 0);
    Voxel3D P2 = new Voxel3D(cx2, cy2, cz2, 0);
    int nbite = 0;
    while (((P1.distance(PP1) > 1) || (P2.distance(PP2) > 1)) && (nbite < 100)) {
        nbite++;
        cx1 = 0;
        cy1 = 0;
        cx2 = 0;
        cy2 = 0;
        cz1 = 0;
        cz2 = 0;
        nb1 = 0;
        nb2 = 0;
        P1.setX(PP1.getX());
        P1.setY(PP1.getY());
        P1.setZ(PP1.getZ());
        P2.setX(PP2.getX());
        P2.setY(PP2.getY());
        P2.setZ(PP2.getZ());
        for (int i = 0; i < nb; i++) {
            d1 = P1.distance(maxlo.get(i));
            d2 = P2.distance(maxlo.get(i));
            if (d1 < d2) {
                cx1 += maxlo.get(i).getX();
                cy1 += maxlo.get(i).getY();
                cz1 += maxlo.get(i).getZ();
                nb1++;
            } else {
                cx2 += maxlo.get(i).getX();
                cy2 += maxlo.get(i).getY();
                cz2 += maxlo.get(i).getZ();
                nb2++;
            }
        }
        cx1 /= nb1;
        cy1 /= nb1;
        cx2 /= nb2;
        cy2 /= nb2;
        cz1 /= nb1;
        cz2 /= nb2;
        PP1.setX(cx1);
        PP1.setY(cy1);
        PP2.setX(cx2);
        PP2.setY(cy2);
        PP1.setZ(cz1);
        PP2.setZ(cz2);
    }
    if (debug) {
        System.out.println("max locaux centres:");
        System.out.println("centre1: " + cx1 + " " + cy1 + " " + cz1);
        System.out.println("centre2: " + cx2 + " " + cy2 + " " + cz2);
        System.out.println("distance: " + PP1.distance(PP2));
    }
    // prendre z milieu
    if (dim == 2) {
        double zmin1 = f.getZmax();
        double zmax1 = f.getZmin();
        double zmin2 = f.getZmax();
        double zmax2 = f.getZmin();
        double di, z;
        // With ArrayList
        it = list.iterator();
        while (it.hasNext()) {
            vox = (Voxel3D) it.next();
            z = vox.getZ();
            // PP1
            PP1.setZ(z);
            di = PP1.distanceSquare(vox);
            if (di < 1) {
                if (z < zmin1) {
                    zmin1 = z;
                }
                if (z > zmax1) {
                    zmax1 = z;
                }
            }
            // PP2
            PP2.setZ(z);
            di = PP2.distanceSquare(vox);
            if (di < 1) {
                if (z < zmin2) {
                    zmin2 = z;
                }
                if (z > zmax2) {
                    zmax2 = z;
                }
            }
        }
        cz1 = 0.5 * (zmin1 + zmax1);
        cz2 = 0.5 * (zmin2 + zmax2);
        PP1.setZ(cz1);
        PP2.setZ(cz2);
    }
    /*
         * // z pour spot 2 k = f.getZmin(); while ((seg.getPixel((int) cx2,
         * (int) cy2, k) != val) && (k < sizez)) { k++; } if (k >= sizez) { k1 =
         * f.getZmin(); k2 = f.getZmax(); } else { k1 = k; while
         * ((seg.getPixel((int) cx2, (int) cy2, k) == val) && (k < sizez)) {
         * k++; } if (k >= sizez) { k2 = f.getZmax(); } else { k2 = k; } } cz2 =
         * (k1 + k2) / 2.0;
         *
         */
    if (debug) {
        System.out.println("max locaux centres en Z:");
        System.out.println("centre1: " + cx1 + " " + cy1 + " " + cz1);
        System.out.println("centre2: " + cx2 + " " + cy2 + " " + cz2);
        System.out.println("distance: " + PP1.distance(PP2));
    }
    // si distance trop petite, on retourne false
    if (PP1.distance(PP2) < dist) {
        return null;
    }
    // hybrid watershed test
    double db1 = f.distPixelBorder(PP1);
    double db2 = f.distPixelBorder(PP2);
    // System.out.println("db " + db1 + " " + db2);
    double db = db1 - db2;
    boolean oneIsLarger = true;
    // object 2 larger than object 1
    if (db2 > db1) {
        db = db2 - db1;
        oneIsLarger = false;
    }
    // //////////////////////////////////////////////////////
    // /// separate the two objects   ///////////////////////
    // //////////////////////////////////////////////////////
    // ArrayUtil tab;
    // IntImage3D seg1 = new IntImage3D(sizex, sizey, sizez);
    // IntImage3D seg2 = new IntImage3D(sizex, sizey, sizez);
    ArrayList<Voxel3D> ob1 = new ArrayList<Voxel3D>();
    ArrayList<Voxel3D> ob2 = new ArrayList<Voxel3D>();
    // IntImage3D tmp;
    // with ArrayList
    it = list.iterator();
    while (it.hasNext()) {
        vox = (Voxel3D) it.next();
        dist1 = PP1.distance(vox);
        dist2 = PP2.distance(vox);
        if (oneIsLarger) {
            dist1 -= db;
        } else {
            dist2 -= db;
        }
        if (dist1 < dist2) {
            vox1 = new Voxel3D(vox);
            vox1.setValue(new1);
            ob1.add(new Voxel3D(vox1));
        } else {
            vox1 = new Voxel3D(vox);
            vox1.setValue(new2);
            ob2.add(new Voxel3D(vox1));
        }
    }
    /*
         * for (k = 0; k < seg.getSizez(); k++) { for (int j = 0; j <
         * seg.getSizey(); j++) { for (int i = 0; i < seg.getSizex(); i++) { if
         * (seg.getPixel(i, j, k) == val) { dist1 = (i - cx1) * (i - cx1) + (j -
         * cy1) * (j - cy1) + (k - cz1) * (k - cz1); dist2 = (i - cx2) * (i -
         * cx2) + (j - cy2) * (j - cy2) + (k - cz2) * (k - cz2); if
         * (oneIsLarger) { dist1 -= db; } else { dist2 -= db; }
         *
         * }
         * }
         * }
         * }
         */
    ArrayList[] tab = new ArrayList[2];
    tab[0] = ob1;
    tab[1] = ob2;
    return tab;
}
Also used : IntImage3D(mcib3d.image3d.legacy.IntImage3D) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Object3DVoxels(mcib3d.geom.Object3DVoxels) Voxel3D(mcib3d.geom.Voxel3D) ImagePlus(ij.ImagePlus)

Aggregations

Object3DVoxels (mcib3d.geom.Object3DVoxels)9 Voxel3D (mcib3d.geom.Voxel3D)6 ImagePlus (ij.ImagePlus)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Chrono (mcib3d.utils.Chrono)2 AbstractLog (mcib3d.utils.Logger.AbstractLog)2 IJStatus (mcib3d.utils.Logger.IJStatus)2 Roi (ij.gui.Roi)1 RoiManager (ij.plugin.frame.RoiManager)1 Rectangle (java.awt.Rectangle)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Random (java.util.Random)1 Object3D (mcib3d.geom.Object3D)1 IntImage3D (mcib3d.image3d.legacy.IntImage3D)1 Watershed3D (mcib3d.image3d.regionGrowing.Watershed3D)1