Search in sources :

Example 1 with GaussianFit_PlugIn

use of uk.ac.sussex.gdsc.ij.utils.GaussianFit_PlugIn in project gdsc by aherbert.

the class FindFociLegacy method findCentreGaussianFit.

/**
 * Finds the centre of the image using a 2D Gaussian fit to projection along the Z-axis.
 *
 * @param subImage the sub image
 * @param dimensions the dimensions
 * @param projectionMethod (0) Average value; (1) Maximum value
 * @return the int[]
 */
private static int[] findCentreGaussianFit(int[] subImage, int[] dimensions, int projectionMethod) {
    if (isGaussianFitEnabled < 1) {
        return null;
    }
    final int blockSize = dimensions[0] * dimensions[1];
    final float[] projection = new float[blockSize];
    if (projectionMethod == 1) {
        // Maximum value
        for (int z = dimensions[2]; z-- > 0; ) {
            int index = blockSize * z;
            for (int i = 0; i < blockSize; i++, index++) {
                if (projection[i] < subImage[index]) {
                    projection[i] = subImage[index];
                }
            }
        }
    } else {
        // Average value
        for (int z = dimensions[2]; z-- > 0; ) {
            int index = blockSize * z;
            for (int i = 0; i < blockSize; i++, index++) {
                projection[i] += subImage[index];
            }
        }
        for (int i = blockSize; i-- > 0; ) {
            projection[i] /= dimensions[2];
        }
    }
    final GaussianFit_PlugIn gf = new GaussianFit_PlugIn();
    final double[] fitParams = gf.fit(projection, dimensions[0], dimensions[1]);
    int[] centre = null;
    if (fitParams != null) {
        // Find the centre of mass along the z-axis
        centre = convertCentre(new double[] { fitParams[2], fitParams[3] });
        // Use the centre of mass along the projection axis
        double com = 0;
        long sum = 0;
        for (int z = dimensions[2]; z-- > 0; ) {
            final int index = blockSize * z;
            final int value = subImage[index];
            if (value > 0) {
                com += z * value;
                sum += value;
            }
        }
        centre[2] = round(com / sum);
        // Avoid clipping
        if (centre[2] >= dimensions[2]) {
            centre[2] = dimensions[2] - 1;
        }
    }
    return centre;
}
Also used : GaussianFit_PlugIn(uk.ac.sussex.gdsc.ij.utils.GaussianFit_PlugIn)

Example 2 with GaussianFit_PlugIn

use of uk.ac.sussex.gdsc.ij.utils.GaussianFit_PlugIn in project gdsc by aherbert.

the class FindFociBaseProcessor method findCentreGaussianFit.

/**
 * Finds the centre of the image using a 2D Gaussian fit to projection along the Z-axis.
 *
 * @param subImage the sub image
 * @param dimensions the dimensions
 * @param projectionMethod (0) Average value; (1) Maximum value
 * @return the centre of the image
 */
@Nullable
private int[] findCentreGaussianFit(float[] subImage, int[] dimensions, int projectionMethod) {
    if (FindFoci_PlugIn.IS_GAUSSIAN_FIT_ENABLED < 1) {
        return null;
    }
    final int blockSize = dimensions[0] * dimensions[1];
    final float[] projection = new float[blockSize];
    if (projectionMethod == 1) {
        // Maximum value
        for (int z = dimensions[2]; z-- > 0; ) {
            int index = blockSize * z;
            for (int i = 0; i < blockSize; i++, index++) {
                if (projection[i] < subImage[index]) {
                    projection[i] = subImage[index];
                }
            }
        }
    } else {
        // Average value
        for (int z = dimensions[2]; z-- > 0; ) {
            int index = blockSize * z;
            for (int i = 0; i < blockSize; i++, index++) {
                projection[i] += subImage[index];
            }
        }
        for (int i = blockSize; i-- > 0; ) {
            projection[i] /= dimensions[2];
        }
    }
    final GaussianFit_PlugIn gf = new GaussianFit_PlugIn();
    final double[] fitParams = gf.fit(projection, dimensions[0], dimensions[1]);
    int[] centre = null;
    if (fitParams != null) {
        // Find the centre of mass along the z-axis
        centre = convertCentre(new double[] { fitParams[2], fitParams[3] });
        // Use the centre of mass along the projection axis
        double com = 0;
        long sum = 0;
        for (int z = dimensions[2]; z-- > 0; ) {
            final int index = blockSize * z;
            final float value = subImage[index];
            if (value > 0) {
                com += z * value;
                sum += value;
            }
        }
        if (sum == 0) {
            centre[2] = dimensions[2] / 2;
        } else {
            // Avoid clipping
            centre[2] = MathUtils.clip(0, dimensions[2] - 1, round(com / sum));
        }
    }
    return centre;
}
Also used : GaussianFit_PlugIn(uk.ac.sussex.gdsc.ij.utils.GaussianFit_PlugIn) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Aggregations

GaussianFit_PlugIn (uk.ac.sussex.gdsc.ij.utils.GaussianFit_PlugIn)2 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)1