src/main/java/org/stowers/microscopy/ij1plugins/findpeaks/FindPeaks3DLocalMax.java
private void searchNeighborhood(int nx, int ny, int nz) {

        if ((nx < 1) || (ny < 1) || (nz < 1)) return;
        if ((nx > w - 2) || (ny > h - 2) || (nz > d - 2)) return;

        float[] pixels = stack.getVoxels(nx - 1, ny -1, nz -1 , 3, 3, 3, null);

        //System.out.println("working on: " + nx + " " + ny + " " + nz + " : " + pixels[13]);

        for (int i = 0; i < pixels.length; i++) {
            if (i == pixels.length/2) continue; //do check the center pixel

            int rx = (i % 9) % 3 - 1;
            int ry = (i % 9) / 3 - 1;
            int rz = i / 9 - 1;

            int sx = nx + rx;
            int sy = ny + ry;
            int sz = nz + rz;

            if (((pixels[i] > .5*value) && pixels[i] > threshold) ||
                    (pixels[i] > (value - tolerance)) && (pixels[i] > threshold)) {

                Long index = (long)(sz*w*h + sy*w + sx);
                if (!neighborsList.contains((Long)index)) {
                    neighborsList.add(index);
                    //System.out.println("Added: " + sx + " " + sy + " " + sz + " : " + pixels[i]);
                    float d = (nx - sx)*(nx - sx) + (ny - sy)*(ny - sy) + (nz - sz)*(nz - sz);
                    if (d < 15*15) {
                        searchNeighborhood(sx, sy, sz);
                    }
                }
            }
            else {
                //System.out.println("Rejected: " + sx + " " + sy + " " + sz + " : " + pixels[i]);
            }

        }
    }
/*
    This was causing stack overflow errors due to the recursion,
    so I switched to an iterative version. See searchNeighborhoodIterative
     */
This was causing stack overflow errors due to the recursion,
so I switched to an iterative version.
This was causing stack overflow errors due to the recursion so I switched to an iterative version .
private void searchNeighborhood(int nx, int ny, int nz) {
        if ((nx < 1) || (ny < 1) || (nz < 1)) return;
        if ((nx > w - 2) || (ny > h - 2) || (nz > d - 2)) return;
        float[] pixels = stack.getVoxels(nx - 1, ny -1, nz -1 , 3, 3, 3, null);
        for (int i = 0; i < pixels.length; i++) {
            if (i == pixels.length/2) continue; 
            int rx = (i % 9) % 3 - 1;
            int ry = (i % 9) / 3 - 1;
            int rz = i / 9 - 1;
            int sx = nx + rx;
            int sy = ny + ry;
            int sz = nz + rz;
            if (((pixels[i] > .5*value) && pixels[i] > threshold) ||
                    (pixels[i] > (value - tolerance)) && (pixels[i] > threshold)) {
                Long index = (long)(sz*w*h + sy*w + sx);
                if (!neighborsList.contains((Long)index)) {
                    neighborsList.add(index);
                    float d = (nx - sx)*(nx - sx) + (ny - sy)*(ny - sy) + (nz - sz)*(nz - sz);
                    if (d < 15*15) {
                        searchNeighborhood(sx, sy, sz);
                    }
                }
            }
            else {
            }
        }
    }
private void searchNeighborhood ( int nx , int ny , int nz ) { if ( ( nx < 1 ) || ( ny < 1 ) || ( nz < 1 ) ) return ; if ( ( nx > w - 2 ) || ( ny > h - 2 ) || ( nz > d - 2 ) ) return ; float [ ] pixels = stack . getVoxels ( nx - 1 , ny - 1 , nz - 1 , 3 , 3 , 3 , null ) ; for ( int i = 0 ; i < pixels . length ; i ++ ) { if ( i == pixels . length / 2 ) continue ; int rx = ( i % 9 ) % 3 - 1 ; int ry = ( i % 9 ) / 3 - 1 ; int rz = i / 9 - 1 ; int sx = nx + rx ; int sy = ny + ry ; int sz = nz + rz ; if ( ( ( pixels [ i ] > .5 * value ) && pixels [ i ] > threshold ) || ( pixels [ i ] > ( value - tolerance ) ) && ( pixels [ i ] > threshold ) ) { Long index = ( long ) ( sz * w * h + sy * w + sx ) ; if ( ! neighborsList . contains ( ( Long ) index ) ) { neighborsList . add ( index ) ; float d = ( nx - sx ) * ( nx - sx ) + ( ny - sy ) * ( ny - sy ) + ( nz - sz ) * ( nz - sz ) ; if ( d < 15 * 15 ) { searchNeighborhood ( sx , sy , sz ) ; } } } else { } } }
This was causing stack overflow errors due to the recursion,
so I switched to an iterative version.
This was causing stack overflow errors due to the recursion so I switched to an iterative version .
//System.out.println("working on: " + nx + " " + ny + " " + nz + " : " + pixels[13]); //do check the center pixel //System.out.println("Added: " + sx + " " + sy + " " + sz + " : " + pixels[i]); //System.out.println("Rejected: " + sx + " " + sy + " " + sz + " : " + pixels[i]);