Search in sources :

Example 1 with IntCoord3D

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

the class Flood3D method flood3DNoiseShort6.

public static void flood3DNoiseShort6(ImageShort img, IntCoord3D seed, short limit, short newVal) {
    short[][] pixels = img.pixels;
    int sizeX = img.sizeX;
    int sizeY = img.sizeY;
    int sizeZ = img.sizeZ;
    int oldVal = pixels[seed.z][seed.x + seed.y * sizeX];
    // int limit=oldVal-noise;
    ArrayList<IntCoord3D> queue = new ArrayList<IntCoord3D>();
    queue.add(seed);
    while (!queue.isEmpty()) {
        // FIXME last element?
        IntCoord3D curCoord = queue.remove(0);
        IJ.log("processing " + curCoord.x + " " + curCoord.y + " " + curCoord.z + " " + oldVal + " " + limit);
        int xy = curCoord.x + curCoord.y * sizeX;
        if (pixels[curCoord.z][xy] >= limit) {
            pixels[curCoord.z][xy] = newVal;
            if (curCoord.x > 0 && pixels[curCoord.z][xy - 1] >= limit) {
                queue.add(new IntCoord3D(curCoord.x - 1, curCoord.y, curCoord.z));
            }
            if (curCoord.x < (sizeX - 1) && pixels[curCoord.z][xy + 1] >= limit) {
                queue.add(new IntCoord3D(curCoord.x + 1, curCoord.y, curCoord.z));
            }
            if (curCoord.y > 0 && pixels[curCoord.z][xy - sizeX] >= limit) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y - 1, curCoord.z));
            }
            if (curCoord.y < (sizeY - 1) && pixels[curCoord.z][xy + sizeX] >= limit) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y + 1, curCoord.z));
            }
            if (curCoord.z > 0 && pixels[curCoord.z - 1][xy] >= limit) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y, curCoord.z - 1));
            }
            if (curCoord.z < (sizeZ - 1) && pixels[curCoord.z + 1][xy] >= limit) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y, curCoord.z + 1));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IntCoord3D(mcib3d.geom.IntCoord3D)

Example 2 with IntCoord3D

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

the class Flood3D method flood3DShort6.

private static void flood3DShort6(ImageShort img, IntCoord3D seed, short newVal) {
    short[][] pixels = img.pixels;
    int sizeX = img.sizeX;
    int sizeY = img.sizeY;
    int sizeZ = img.sizeZ;
    short oldVal = pixels[seed.z][seed.x + seed.y * sizeX];
    ArrayList<IntCoord3D> queue = new ArrayList<IntCoord3D>();
    queue.add(seed);
    while (!queue.isEmpty()) {
        // FIXME last element?
        IntCoord3D curCoord = queue.remove(0);
        int xy = curCoord.x + curCoord.y * sizeX;
        if (pixels[curCoord.z][xy] == oldVal) {
            pixels[curCoord.z][xy] = newVal;
            if (curCoord.x > 0 && pixels[curCoord.z][xy - 1] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x - 1, curCoord.y, curCoord.z));
            }
            if (curCoord.x < (sizeX - 1) && pixels[curCoord.z][xy + 1] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x + 1, curCoord.y, curCoord.z));
            }
            if (curCoord.y > 0 && pixels[curCoord.z][xy - sizeX] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y - 1, curCoord.z));
            }
            if (curCoord.y < (sizeY - 1) && pixels[curCoord.z][xy + sizeX] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y + 1, curCoord.z));
            }
            if (curCoord.z > 0 && pixels[curCoord.z - 1][xy] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y, curCoord.z - 1));
            }
            if (curCoord.z < (sizeZ - 1) && pixels[curCoord.z + 1][xy] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y, curCoord.z + 1));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IntCoord3D(mcib3d.geom.IntCoord3D)

Example 3 with IntCoord3D

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

the class FillHoles3D method process.

public static void process(ImageByte mask, int foregroundValue, int nbCPUs, boolean verbose) {
    final byte[][] pixels = mask.pixels;
    final int sizeX = mask.sizeX;
    final int sizeZ = mask.sizeZ;
    final int sizeY = mask.sizeY;
    final int sizeXY = mask.sizeXY;
    final byte fgValue = (byte) foregroundValue;
    int mid;
    if (foregroundValue > 1) {
        mid = foregroundValue - 1;
    } else {
        mid = foregroundValue + 1;
    }
    final byte midValue = (byte) mid;
    // ImageByte im1 = new ImageByte(pixels, "", sizeX);
    // Z axis
    final mcib3d.utils.ThreadRunner tr = new mcib3d.utils.ThreadRunner(0, sizeY, nbCPUs);
    for (int i = 0; i < tr.threads.length; i++) {
        tr.threads[i] = new Thread(new Runnable() {

            public void run() {
                for (int idx = tr.ai.getAndIncrement(); idx < tr.end; idx = tr.ai.getAndIncrement()) {
                    try {
                        int offsetY = sizeX * idx;
                        for (int x = 0; x < sizeX; x++) {
                            int z = 0;
                            while (z < sizeZ && pixels[z][x + offsetY] == 0) {
                                pixels[z][x + offsetY] = midValue;
                                z++;
                            }
                            if (z < (sizeZ - 1)) {
                                z = sizeZ - 1;
                                while (z >= 0 && pixels[z][x + offsetY] == 0) {
                                    pixels[z][x + offsetY] = midValue;
                                    z--;
                                }
                            }
                        }
                    } catch (Exception e) {
                        exceptionPrinter.print(e, "fillHoles 3D Z:" + idx + "::error::", true);
                    }
                }
            }
        });
    }
    tr.startAndJoin();
    // Y axis
    final mcib3d.utils.ThreadRunner tr2 = new mcib3d.utils.ThreadRunner(0, sizeZ, nbCPUs);
    for (int i = 0; i < tr2.threads.length; i++) {
        tr2.threads[i] = new Thread(new Runnable() {

            public void run() {
                for (int idx = tr2.ai.getAndIncrement(); idx < tr2.end; idx = tr2.ai.getAndIncrement()) {
                    try {
                        for (int y = 0; y < sizeY; y++) {
                            int offsetY = sizeX * y;
                            boolean bcg = pixels[idx][offsetY] == 0;
                            for (int x = 0; x < sizeX; x++) {
                                byte value = pixels[idx][offsetY + x];
                                if (value == fgValue) {
                                    bcg = false;
                                } else if (value == 0 && bcg) {
                                    pixels[idx][offsetY + x] = midValue;
                                } else if (value == midValue) {
                                    bcg = true;
                                }
                            }
                            bcg = pixels[idx][offsetY + sizeX - 1] == 0;
                            for (int x = sizeX - 1; x >= 0; x--) {
                                byte value = pixels[idx][offsetY + x];
                                if (value == fgValue) {
                                    bcg = false;
                                } else if (value == 0 && bcg) {
                                    pixels[idx][offsetY + x] = midValue;
                                } else if (value == midValue) {
                                    bcg = true;
                                }
                            }
                        }
                    } catch (Exception e) {
                        exceptionPrinter.print(e, "fillHoles 3D Y:" + idx + "::error::", true);
                    }
                }
            }
        });
    }
    tr2.startAndJoin();
    // X axis
    final mcib3d.utils.ThreadRunner tr3 = new mcib3d.utils.ThreadRunner(0, sizeZ, nbCPUs);
    for (int i = 0; i < tr3.threads.length; i++) {
        tr3.threads[i] = new Thread(new Runnable() {

            public void run() {
                for (int idx = tr3.ai.getAndIncrement(); idx < tr3.end; idx = tr3.ai.getAndIncrement()) {
                    try {
                        for (int x = 0; x < sizeX; x++) {
                            boolean bcg = pixels[idx][x] == 0;
                            for (int y = 0; y < sizeY; y++) {
                                byte value = pixels[idx][y * sizeX + x];
                                if (value == fgValue) {
                                    bcg = false;
                                } else if (value == 0 && bcg) {
                                    pixels[idx][y * sizeX + x] = midValue;
                                } else if (value == midValue) {
                                    bcg = true;
                                }
                            }
                            bcg = pixels[idx][(sizeY - 1) * sizeX + x] == 0;
                            for (int y = sizeY - 1; y >= 0; y--) {
                                byte value = pixels[idx][y * sizeX + x];
                                if (value == fgValue) {
                                    bcg = false;
                                } else if (value == 0 && bcg) {
                                    pixels[idx][y * sizeX + x] = midValue;
                                } else if (value == midValue) {
                                    bcg = true;
                                }
                            }
                        }
                    } catch (Exception e) {
                        exceptionPrinter.print(e, "fillHoles 3D X:" + idx + "::error::", true);
                    }
                }
            }
        });
    }
    tr3.startAndJoin();
    // im1.showDuplicate("3eme passage");
    // remove artefacts
    Queue<IntCoord3D> heap = new LinkedList<IntCoord3D>();
    for (int z = 0; z < sizeZ; z++) {
        for (int y = 0; y < sizeY; y++) {
            for (int x = 0; x < sizeX; x++) {
                if (pixels[z][x + sizeX * y] == 0) {
                    if (checkVoxel(pixels, x, y, z, sizeX, sizeY, sizeZ, midValue)) {
                        // heap.add((x + y * sizeX + z * sizeXY));
                        heap.add(new IntCoord3D(x, y, z));
                    }
                }
            }
        }
    }
    // Coordinate3D c = new Coordinate3D(0, sizeX, sizeY, sizeZ);
    while (!heap.isEmpty()) {
        // int coord = heap.remove();
        IntCoord3D c = heap.remove();
        // c.setCoord(coord);
        if ((c.z < 0) || (c.z >= sizeZ) || (c.x + c.y * sizeX < 0) || (c.x + c.y * sizeX >= sizeXY)) {
            IJ.log("souci " + c);
        }
        if (pixels[c.z][c.x + c.y * sizeX] == 0) {
            pixels[c.z][c.x + c.y * sizeX] = midValue;
            if (c.z > 0 && pixels[c.z - 1][c.x + c.y * sizeX] == 0) {
                heap.add(new IntCoord3D(c.x, c.y, c.z - 1));
            }
            if (c.z < (sizeZ - 1) && pixels[c.z + 1][c.x + c.y * sizeX] == 0) {
                heap.add(new IntCoord3D(c.x, c.y, c.z + 1));
            }
            if (c.x > 0 && pixels[c.z][c.x - 1 + c.y * sizeX] == 0) {
                heap.add(new IntCoord3D(c.x - 1, c.y, c.z));
            }
            if (c.x < (sizeX - 1) && pixels[c.z][c.x + 1 + c.y * sizeX] == 0) {
                heap.add(new IntCoord3D(c.x + 1, c.y, c.z));
            }
            if (c.y > 0 && pixels[c.z][c.x + (c.y - 1) * sizeX] == 0) {
                heap.add(new IntCoord3D(c.x, c.y - 1, c.z));
            }
            if (c.y < (sizeY - 1) && pixels[c.z][c.x + (c.y + 1) * sizeX] == 0) {
                heap.add(new IntCoord3D(c.x, c.y + 1, c.z));
            }
        }
    }
    // im1.showDuplicate("arte");
    // final step
    final mcib3d.utils.ThreadRunner tr4 = new mcib3d.utils.ThreadRunner(0, sizeZ, nbCPUs);
    for (int i = 0; i < tr4.threads.length; i++) {
        tr4.threads[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int idx = tr4.ai.getAndIncrement(); idx < tr4.end; idx = tr4.ai.getAndIncrement()) {
                    try {
                        for (int xy = 0; xy < sizeX * sizeY; xy++) {
                            if (pixels[idx][xy] == midValue) {
                                pixels[idx][xy] = 0;
                            } else if (pixels[idx][xy] == 0) {
                                pixels[idx][xy] = fgValue;
                            }
                        }
                    } catch (Exception e) {
                        exceptionPrinter.print(e, "fillHoles 3D final step:" + idx + "::error::", true);
                    }
                }
            }
        });
    }
    tr4.startAndJoin();
}
Also used : IntCoord3D(mcib3d.geom.IntCoord3D) LinkedList(java.util.LinkedList)

Example 4 with IntCoord3D

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

the class Flood3D method flood3DByte26.

private static void flood3DByte26(ImageByte img, IntCoord3D seed, byte newVal) {
    byte[][] pixels = img.pixels;
    int sizeX = img.sizeX;
    int sizeY = img.sizeY;
    int sizeZ = img.sizeZ;
    short oldVal = pixels[seed.z][seed.x + seed.y * sizeX];
    ArrayList<IntCoord3D> queue = new ArrayList<IntCoord3D>();
    queue.add(seed);
    while (!queue.isEmpty()) {
        // FIXME last element?
        IntCoord3D curCoord = queue.remove(0);
        int xy = curCoord.x + curCoord.y * sizeX;
        if (pixels[curCoord.z][xy] == oldVal) {
            pixels[curCoord.z][xy] = newVal;
            int curZ, curY, curX;
            for (int zz = -1; zz < 2; zz++) {
                curZ = curCoord.z + zz;
                if (curZ > 0 && curZ < (sizeZ - 1)) {
                    for (int yy = -1; yy < 2; yy++) {
                        curY = curCoord.y + yy;
                        if (curY > 0 && curY < (sizeY - 1)) {
                            for (int xx = -1; xx < 2; xx++) {
                                curX = curCoord.x + xx;
                                if (curX > 0 && curX < (sizeX - 1) && (xx != 0 || yy != 0 || zz != 0)) {
                                    if (pixels[curZ][curX + curY * sizeX] == oldVal) {
                                        queue.add(new IntCoord3D(curX, curY, curZ));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IntCoord3D(mcib3d.geom.IntCoord3D)

Example 5 with IntCoord3D

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

the class Flood3D method flood3DByte6.

private static void flood3DByte6(ImageByte img, IntCoord3D seed, byte newVal) {
    byte[][] pixels = img.pixels;
    int sizeX = img.sizeX;
    int sizeY = img.sizeY;
    int sizeZ = img.sizeZ;
    short oldVal = pixels[seed.z][seed.x + seed.y * sizeX];
    ArrayList<IntCoord3D> queue = new ArrayList<IntCoord3D>();
    queue.add(seed);
    while (!queue.isEmpty()) {
        // FIXME last element?
        IntCoord3D curCoord = queue.remove(0);
        int xy = curCoord.x + curCoord.y * sizeX;
        if (pixels[curCoord.z][xy] == oldVal) {
            pixels[curCoord.z][xy] = newVal;
            if (curCoord.x > 0 && pixels[curCoord.z][xy - 1] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x - 1, curCoord.y, curCoord.z));
            }
            if (curCoord.x < (sizeX - 1) && pixels[curCoord.z][xy + 1] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x + 1, curCoord.y, curCoord.z));
            }
            if (curCoord.y > 0 && pixels[curCoord.z][xy - sizeX] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y - 1, curCoord.z));
            }
            if (curCoord.y < (sizeY - 1) && pixels[curCoord.z][xy + sizeX] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y + 1, curCoord.z));
            }
            if (curCoord.z > 0 && pixels[curCoord.z - 1][xy] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y, curCoord.z - 1));
            }
            if (curCoord.z < (sizeZ - 1) && pixels[curCoord.z + 1][xy] == oldVal) {
                queue.add(new IntCoord3D(curCoord.x, curCoord.y, curCoord.z + 1));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IntCoord3D(mcib3d.geom.IntCoord3D)

Aggregations

IntCoord3D (mcib3d.geom.IntCoord3D)8 ArrayList (java.util.ArrayList)6 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Voxel3D (mcib3d.geom.Voxel3D)1 Voxel3DComparable (mcib3d.geom.Voxel3DComparable)1 ImageShort (mcib3d.image3d.ImageShort)1