Search in sources :

Example 6 with IJStatus

use of mcib3d.utils.Logger.IJStatus in project mcib3d-core by mcib3d.

the class Object3DSurface method computeVoxelsMultithread.

// private void computeVoxelsFill() {
// voxels = new ArrayList<Voxel3D>();
// // start at 1 1 1 to let 0 0 0 as background for fill holes
// int w = getXmax() - getXmin() + 2;
// int h = getYmax() - getYmin() + 2;
// int d = getZmax() - getZmin() + 2;
// ImageByte mask = new ImageByte("mask",w, h, d);
// 
// Point3f vox;
// int x, y, z;
// int val = 1;
// 
// Iterator it = unique_vertices.iterator();
// while (it.hasNext()) {
// vox = (Point3f) it.next();
// x = (int) (vox.getX() - getXmin() + 1);
// y = (int) (vox.getY() - getYmin() + 1);
// z = (int) (vox.getZ() - getZmin() + 1);
// mask.putPixel(x, y, z, val);
// }
// // fill holes 3D
// mask.fillHoles3D(0, val);
// 
// // create voxels lists
// double xx, yy, zz;
// for (z = 0; z < mask.getSizez(); z++) {
// for (y = 0; y < mask.getSizey(); y++) {
// for (x = 0; x < mask.getSizex(); x++) {
// if (mask.getPixel(x, y, z) == val) {
// xx = x + getXmin() - 1;
// yy = y + getYmin() - 1;
// zz = z + getZmin() - 1;
// voxels.add(new Voxel3D(xx, yy, zz, val));
// }
// }
// }
// }
// 
// }
// Voxellisation ; coordinates should be backed to pixels coordinates
private ArrayList<Voxel3D> computeVoxelsMultithread() {
    final int zminv = (int) Math.floor(getZmin()) - 1;
    final int yminv = (int) Math.floor(getYmin()) - 1;
    final int xminv = (int) Math.floor(getXmin()) - 1;
    final int zmaxv = (int) Math.ceil(getZmax()) + 1;
    final int ymaxv = (int) Math.ceil(getYmax()) + 1;
    final int xmaxv = (int) Math.ceil(getXmax()) + 1;
    final int n_cpus = ThreadUtil.getNbCpus();
    final int val = this.getValue();
    final ArrayList[] voxelss = new ArrayList[n_cpus];
    for (int i = 0; i < voxelss.length; i++) {
        voxelss[i] = new ArrayList();
    }
    final Vector3D dir0 = new Vector3D(1, 0, 0);
    final Vector3D dir1 = new Vector3D(-1, 0, 0);
    final int dec = (int) Math.ceil((double) (zmaxv - zminv + 1) / (double) n_cpus);
    // IJ.log("dec " + dec + " " + zminv + " " + zmaxv + " " + n_cpus);
    // Timer
    final Chrono time = new Chrono(zmaxv - zminv + 1);
    time.start();
    final AbstractLog show = new IJStatus();
    final AtomicInteger ai = new AtomicInteger(0);
    Thread[] threads = ThreadUtil.createThreadArray(n_cpus);
    for (int ithread = 0; ithread < threads.length; ithread++) {
        threads[ithread] = new Thread() {

            @Override
            public void run() {
                boolean in;
                Point3D origin;
                for (int k = ai.getAndIncrement(); k < n_cpus; k = ai.getAndIncrement()) {
                    int zmaxx = Math.min(zminv + (k + 1) * dec, zmaxv);
                    for (float zi = zminv + dec * k; zi < zmaxx; zi += 1) {
                        // IJ.showStatus(k + " : Voxellisation " + zi + "/" + (zmaxx - 1));
                        for (float yi = yminv; yi <= ymaxv; yi += 1) {
                            in = false;
                            float xi = xminv;
                            while (xi <= xmaxv) {
                                if (!in) {
                                    origin = new Point3D(xi, yi + 0.5, zi + 0.5);
                                    double distMin = minDistSquareTriangles(origin, dir0, 1.0);
                                    if (distMin <= 1) {
                                        in = true;
                                    } else if ((distMin >= 9.0) && (distMin < Double.MAX_VALUE)) {
                                        // IJ.log(" " + distMin + " " + (Math.floor(Math.sqrt(distMin)) - 1));
                                        xi += Math.floor(Math.sqrt(distMin)) - 1;
                                    } else if (distMin == Double.MAX_VALUE) {
                                        xi += xmaxv;
                                    }
                                } else {
                                    // voxels are in pixels not calibrated units
                                    voxelss[k].add(new Voxel3D(xi, yi, zi, val));
                                    origin = new Point3D(xi, yi + 0.5, zi + 0.5);
                                    double distMin = minDistSquareTriangles(origin, dir1, 1);
                                    if (distMin <= 1) {
                                        in = false;
                                    } else if ((distMin >= 9.0) && (distMin < Double.MAX_VALUE)) {
                                        // IJ.log(" " + distMin + " " + (Math.floor(Math.sqrt(distMin)) - 1));
                                        int nb = (int) Math.floor(Math.sqrt(distMin));
                                        if (xi + nb < xmaxv) {
                                            addLineXVoxels(voxelss[k], val, xi + 1, yi, zi, nb);
                                            xi += nb;
                                            in = false;
                                        }
                                    }
                                }
                                xi += 1;
                            }
                        }
                        String ti = time.getFullInfo(1);
                        if (ti != null)
                            show.log("3D voxellisation : " + ti);
                    }
                }
            }
        };
    }
    ThreadUtil.startAndJoin(threads);
    // put all arrays in one
    ArrayList<Voxel3D> newVox = new ArrayList<Voxel3D>();
    for (ArrayList voxels1 : voxelss) {
        newVox.addAll(voxels1);
    }
    return newVox;
}
Also used : Chrono(mcib3d.utils.Chrono) IJStatus(mcib3d.utils.Logger.IJStatus) AbstractLog(mcib3d.utils.Logger.AbstractLog) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 7 with IJStatus

use of mcib3d.utils.Logger.IJStatus 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)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Chrono (mcib3d.utils.Chrono)7 AbstractLog (mcib3d.utils.Logger.AbstractLog)7 IJStatus (mcib3d.utils.Logger.IJStatus)7 ArrayList (java.util.ArrayList)2 Object3DVoxels (mcib3d.geom.Object3DVoxels)2 Voxel3DComparable (mcib3d.geom.Voxel3DComparable)2