Search in sources :

Example 1 with IntImage3D

use of mcib3d.image3d.legacy.IntImage3D 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

ImagePlus (ij.ImagePlus)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Object3DVoxels (mcib3d.geom.Object3DVoxels)1 Voxel3D (mcib3d.geom.Voxel3D)1 IntImage3D (mcib3d.image3d.legacy.IntImage3D)1