Search in sources :

Example 81 with FloatProcessor

use of ij.process.FloatProcessor in project mcib3d-core by mcib3d.

the class EdtByte method run.

public ImageFloat run(ImageByte imp, int thresh, float scaleXY, float scaleZ, int nbCPUs) throws Exception {
    int sizeX = imp.sizeX;
    int sizeY = imp.sizeY;
    int sizeZ = imp.sizeZ;
    float scale = scaleZ / scaleXY;
    byte[][] data = imp.pixels;
    // Create 32 bit floating point stack for output, s.  Will also use it for g in Transformation 1.
    ImageStack sStack = new ImageStack(sizeX, sizeY);
    float[][] res = new float[sizeZ][];
    for (int k = 0; k < sizeZ; k++) {
        ImageProcessor ipk = new FloatProcessor(sizeX, sizeY);
        sStack.addSlice(null, ipk);
        res[k] = (float[]) ipk.getPixels();
    }
    float[] sk;
    // Transformation 1.  Use s to store g.
    Step1Thread[] s1t = new Step1Thread[nbCPUs];
    for (int thread = 0; thread < nbCPUs; thread++) {
        s1t[thread] = new Step1Thread(thread, nbCPUs, sizeX, sizeY, sizeZ, thresh, res, data, scale);
        s1t[thread].start();
    }
    try {
        for (int thread = 0; thread < nbCPUs; thread++) {
            s1t[thread].join();
        }
    } catch (InterruptedException ie) {
        IJ.error("A thread was interrupted in step 1 .");
    }
    // Transformation 2.  g (in s) -> h (in s)
    Step2Thread[] s2t = new Step2Thread[nbCPUs];
    for (int thread = 0; thread < nbCPUs; thread++) {
        s2t[thread] = new Step2Thread(thread, nbCPUs, sizeX, sizeY, sizeZ, res);
        s2t[thread].start();
    }
    try {
        for (int thread = 0; thread < nbCPUs; thread++) {
            s2t[thread].join();
        }
    } catch (InterruptedException ie) {
        IJ.error("A thread was interrupted in step 2 .");
    }
    // Transformation 3. h (in s) -> s
    if (imp.sizeZ > 1) {
        Step3Thread[] s3t = new Step3Thread[nbCPUs];
        for (int thread = 0; thread < nbCPUs; thread++) {
            s3t[thread] = new Step3Thread(thread, nbCPUs, sizeX, sizeY, sizeZ, res, data, thresh, scale);
            s3t[thread].start();
        }
        try {
            for (int thread = 0; thread < nbCPUs; thread++) {
                s3t[thread].join();
            }
        } catch (InterruptedException ie) {
            IJ.error("A thread was interrupted in step 3 .");
        }
    }
    // Find the largest distance for scaling
    // Also fill in the background values.
    float distMax = 0;
    int wh = sizeX * sizeY;
    float dist;
    for (int k = 0; k < sizeZ; k++) {
        sk = res[k];
        for (int ind = 0; ind < wh; ind++) {
            if (((data[k][ind] & 255) <= thresh)) {
                sk[ind] = 0;
            } else {
                dist = (float) Math.sqrt(sk[ind]) * scaleXY;
                sk[ind] = dist;
                distMax = (dist > distMax) ? dist : distMax;
            }
        }
    }
    ImageFloat map = (ImageFloat) ImageFloat.wrap(sStack);
    map.setScale(imp);
    map.setOffset(imp);
    map.setMinAndMax(0, distMax);
    return map;
}
Also used : ImageStack(ij.ImageStack) FloatProcessor(ij.process.FloatProcessor) ImageFloat(mcib3d.image3d.ImageFloat) ImageProcessor(ij.process.ImageProcessor)

Example 82 with FloatProcessor

use of ij.process.FloatProcessor in project mcib3d-core by mcib3d.

the class FHTImage3D method getPowerSpectrum.

/**
 * Gets the powerStack attribute of the FHTImage3D object
 *
 * @param log Description of the Parameter
 * @return The powerStack value
 */
public ImageStack getPowerSpectrum(boolean log) {
    FloatProcessor tmp;
    double n;
    double value;
    ImageStack stack = new ImageStack(sizex, sizey);
    for (int k = 0; k < sizez; k++) {
        tmp = new FloatProcessor(sizex, sizey);
        for (int x = 0; x < sizex; x++) {
            for (int y = 0; y < sizey; y++) {
                n = getNorm(x, y, k);
                value = n * n;
                if (log) {
                    value = Math.log(value);
                }
                tmp.putPixelValue(x, y, (float) value);
            }
        }
        stack.addSlice("" + (k + 1), tmp);
    }
    return stack;
}
Also used : FloatProcessor(ij.process.FloatProcessor) ImageStack(ij.ImageStack)

Example 83 with FloatProcessor

use of ij.process.FloatProcessor in project mcib3d-core by mcib3d.

the class ImageFloat method resize.

@Override
public ImageHandler resize(int dX, int dY, int dZ) {
    int newX = Math.max(1, sizeX + 2 * dX);
    int newY = Math.max(1, sizeY + 2 * dY);
    boolean bck = Prefs.get("resizer.zero", true);
    Prefs.set("resizer.zero", true);
    ij.plugin.CanvasResizer cr = new ij.plugin.CanvasResizer();
    ImageStack res = cr.expandStack(img.getStack(), newX, newY, dX, dY);
    if (!bck) {
    // Prefs.set("resizer.zero", false);
    }
    if (dZ > 0) {
        for (int i = 0; i < dZ; i++) {
            res.addSlice("", new FloatProcessor(newX, newY), 0);
            res.addSlice("", new FloatProcessor(newX, newY));
        }
    } else {
        for (int i = 0; i < -dZ; i++) {
            if (res.getSize() <= 2) {
                break;
            }
            res.deleteLastSlice();
            res.deleteSlice(1);
        }
    }
    ImageFloat r = new ImageFloat(new ImagePlus(title + "::resized", res));
    r.offsetX = offsetX - dX;
    r.offsetY = offsetY - dY;
    r.offsetZ = offsetZ - dZ;
    return r;
}
Also used : ImageStack(ij.ImageStack) FloatProcessor(ij.process.FloatProcessor) ImagePlus(ij.ImagePlus)

Example 84 with FloatProcessor

use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.

the class PixelFilter method run.

public void run(ImageProcessor ip) {
    // Compute rolling sums
    FloatProcessor fp = ip.toFloat(0, null);
    float[] data = (float[]) ip.toFloat(0, null).getPixels();
    double[] s = null, ss = null;
    if (preview && cachedS != null) {
        s = cachedS;
        ss = cachedSS;
    }
    if (s == null) {
        s = new double[ip.getPixelCount()];
        ss = new double[s.length];
        calculateRollingSums(fp, s, ss);
    }
    int count = 0;
    final int maxx = ip.getWidth();
    final int maxy = ip.getHeight();
    for (int y = 0, i = 0; y < maxy; y++) {
        for (int x = 0; x < maxx; x++, i++) {
            double sum = 0;
            double sumSquares = 0;
            int minU = x - radius - 1;
            int maxU = FastMath.min(x + radius, maxx - 1);
            int minV = y - radius - 1;
            int maxV = FastMath.min(y + radius, maxy - 1);
            // Compute sum from rolling sum using:
            // sum(u,v) = 
            // + s(u+N,v+N) 
            // - s(u-N-1,v+N)
            // - s(u+N,v-N-1)
            // + s(u-N-1,v-N-1)
            // Note: 
            // s(u,v) = 0 when either u,v < 0
            // s(u,v) = s(umax,v) when u>umax
            // s(u,v) = s(u,vmax) when v>vmax
            // s(u,v) = s(umax,vmax) when u>umax,v>vmax
            // Likewise for ss
            // + s(u+N-1,v+N-1) 
            int index = maxV * maxx + maxU;
            sum += s[index];
            sumSquares += ss[index];
            if (minU >= 0) {
                // - s(u-1,v+N-1)
                index = maxV * maxx + minU;
                sum -= s[index];
                sumSquares -= ss[index];
            }
            if (minV >= 0) {
                // - s(u+N-1,v-1)
                index = minV * maxx + maxU;
                sum -= s[index];
                sumSquares -= ss[index];
                if (minU >= 0) {
                    // + s(u-1,v-1)
                    index = minV * maxx + minU;
                    sum += s[index];
                    sumSquares += ss[index];
                }
            }
            // Reset to bounds to calculate the number of pixels
            if (minU < 0)
                minU = -1;
            if (minV < 0)
                minV = -1;
            int n = (maxU - minU) * (maxV - minV);
            if (n < 2)
                continue;
            // Get the sum of squared differences
            double residuals = sumSquares - (sum * sum) / n;
            if (residuals > 0.0) {
                double stdDev = Math.sqrt(residuals / (n - 1.0));
                double mean = sum / n;
                if (Math.abs(data[i] - mean) / stdDev > error) {
                    ip.setf(i, (float) mean);
                    count++;
                }
            }
        }
    }
    if (preview) {
        cachedS = s;
        cachedSS = ss;
        label.setText("Replaced " + count);
    } else if (pfr != null && count > 0)
        IJ.log(String.format("Slice %d : Replaced %d pixels", pfr.getSliceNumber(), count));
}
Also used : FloatProcessor(ij.process.FloatProcessor)

Example 85 with FloatProcessor

use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.

the class IJImagePeakResults method createNewProcessor.

private ImageProcessor createNewProcessor(int imageWidth, int imageHeight) {
    // Equalised display requires a 16-bit image to allow fast processing of the histogram 
    if ((displayFlags & DISPLAY_EQUALIZED) != 0) {
        pixels = new short[data.length];
        return new ShortProcessor(imageWidth, imageHeight, (short[]) pixels, null);
    } else {
        pixels = new float[data.length];
        // Zero is mapped to 0 in the LUT.
        if ((displayFlags & DISPLAY_MAPPED) != 0) {
            MappedFloatProcessor fp = new MappedFloatProcessor(imageWidth, imageHeight, (float[]) pixels, null);
            fp.setMapZero((displayFlags & DISPLAY_MAP_ZERO) != 0);
            return fp;
        }
        return new FloatProcessor(imageWidth, imageHeight, (float[]) pixels, null);
    }
}
Also used : MappedFloatProcessor(ij.process.MappedFloatProcessor) FloatProcessor(ij.process.FloatProcessor) MappedFloatProcessor(ij.process.MappedFloatProcessor) ShortProcessor(ij.process.ShortProcessor)

Aggregations

FloatProcessor (ij.process.FloatProcessor)174 ImageProcessor (ij.process.ImageProcessor)30 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)26 Rectangle (java.awt.Rectangle)23 Test (org.junit.Test)22 ByteProcessor (ij.process.ByteProcessor)21 ShortProcessor (ij.process.ShortProcessor)20 Point (java.awt.Point)20 Test (org.junit.jupiter.api.Test)20 ImagePlus (ij.ImagePlus)19 ImageStack (ij.ImageStack)19 Future (java.util.concurrent.Future)12 ColorProcessor (ij.process.ColorProcessor)11 ArrayList (java.util.ArrayList)9 LinkedList (java.util.LinkedList)8 Fht (uk.ac.sussex.gdsc.core.ij.process.Fht)7 Point (mpicbg.models.Point)6 FHT2 (ij.process.FHT2)5 File (java.io.File)5 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)5