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));
}
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);
}
}
use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.
the class ImageConverter method getData.
/**
* Get the data from the image as a float array (include cropping to the ROI). Data is duplicated if the
* input already a float array.
* <p>
* Allows reuse of an existing buffer if provided. This will not be truncated if it is larger than the
* ImageProcessor ROI bounds. If smaller then a new buffer will be created.
*
* @param oPixels
* @param width
* @param height
* @param bounds
* @param buffer
* @return The float array data
*/
public static float[] getData(final Object oPixels, final int width, final int height, final Rectangle bounds, float[] buffer) {
if (oPixels == null)
return null;
if (oPixels instanceof float[]) {
float[] pixels = (float[]) oPixels;
if (bounds != null && (bounds.x != 0 || bounds.y != 0 || bounds.width != width || bounds.height != height)) {
float[] pixels2 = allocate(buffer, bounds.width * bounds.height);
for (int ys = bounds.y; ys < bounds.y + bounds.height; ys++) {
int offset1 = (ys - bounds.y) * bounds.width;
int offset2 = ys * width + bounds.x;
for (int xs = 0; xs < bounds.width; xs++) pixels2[offset1++] = pixels[offset2++];
}
return pixels2;
} else {
float[] pixels2 = allocate(buffer, pixels.length);
System.arraycopy(pixels, 0, pixels2, 0, pixels.length);
return pixels2;
}
} else if (oPixels instanceof short[]) {
short[] pixels = (short[]) oPixels;
if (bounds != null && (bounds.x != 0 || bounds.y != 0 || bounds.width != width || bounds.height != height)) {
float[] pixels2 = allocate(buffer, bounds.width * bounds.height);
for (int ys = bounds.y; ys < bounds.y + bounds.height; ys++) {
int offset1 = (ys - bounds.y) * bounds.width;
int offset2 = ys * width + bounds.x;
for (int xs = 0; xs < bounds.width; xs++) pixels2[offset1++] = pixels[offset2++] & 0xffff;
}
return pixels2;
} else {
float[] pixels2 = allocate(buffer, pixels.length);
for (int i = 0; i < pixels.length; i++) pixels2[i] = pixels[i] & 0xffff;
return pixels2;
}
} else if (oPixels instanceof byte[]) {
byte[] pixels = (byte[]) oPixels;
if (bounds != null && (bounds.x != 0 || bounds.y != 0 || bounds.width != width || bounds.height != height)) {
float[] pixels2 = allocate(buffer, bounds.width * bounds.height);
for (int ys = bounds.y; ys < bounds.y + bounds.height; ys++) {
int offset1 = (ys - bounds.y) * bounds.width;
int offset2 = ys * width + bounds.x;
for (int xs = 0; xs < bounds.width; xs++) pixels2[offset1++] = pixels[offset2++] & 0xff;
}
return pixels2;
} else {
float[] pixels2 = allocate(buffer, pixels.length);
for (int i = 0; i < pixels.length; i++) pixels2[i] = pixels[i] & 0xff;
return pixels2;
}
} else if (oPixels instanceof int[]) {
// The default processing
ImageProcessor ip = new ColorProcessor(width, height, (int[]) oPixels);
ip.setRoi(bounds);
FloatProcessor fp = ip.crop().toFloat(0, null);
return (float[]) fp.getPixels();
}
return null;
}
use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.
the class FRCTest method canComputeMirrored.
@Test
public void canComputeMirrored() {
// Sample lines through an image to create a structure.
int size = 1024;
double[][] data = new double[size * 2][];
RandomGenerator r = new Well19937c(30051977);
for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
data[i++] = new double[] { x + r.nextGaussian() * 5, y + r.nextGaussian() * 5 };
data[i++] = new double[] { x + r.nextGaussian() * 5, y2 + r.nextGaussian() * 5 };
}
// Create 2 images
Rectangle bounds = new Rectangle(0, 0, size, size);
IJImagePeakResults i1 = createImage(bounds);
IJImagePeakResults i2 = createImage(bounds);
int[] indices = Utils.newArray(data.length, 0, 1);
MathArrays.shuffle(indices, r);
for (int i : indices) {
IJImagePeakResults image = i1;
i1 = i2;
i2 = image;
image.add((float) data[i][0], (float) data[i][1], 1);
}
i1.end();
i2.end();
ImageProcessor ip1 = i1.getImagePlus().getProcessor();
ImageProcessor ip2 = i2.getImagePlus().getProcessor();
// Test
FRC frc = new FRC();
FloatProcessor[] fft1, fft2;
fft1 = frc.getComplexFFT(ip1);
fft2 = frc.getComplexFFT(ip2);
float[] dataA1 = (float[]) fft1[0].getPixels();
float[] dataB1 = (float[]) fft1[1].getPixels();
float[] dataA2 = (float[]) fft2[0].getPixels();
float[] dataB2 = (float[]) fft2[1].getPixels();
float[] numeratorE = new float[dataA1.length];
float[] absFFT1E = new float[dataA1.length];
float[] absFFT2E = new float[dataA1.length];
FRC.compute(numeratorE, absFFT1E, absFFT2E, dataA1, dataB1, dataA2, dataB2);
Assert.assertTrue("numeratorE", FRC.checkSymmetry(numeratorE, size));
Assert.assertTrue("absFFT1E", FRC.checkSymmetry(absFFT1E, size));
Assert.assertTrue("absFFT2E", FRC.checkSymmetry(absFFT2E, size));
float[] numeratorA = new float[dataA1.length];
float[] absFFT1A = new float[dataA1.length];
float[] absFFT2A = new float[dataA1.length];
FRC.computeMirrored(size, numeratorA, absFFT1A, absFFT2A, dataA1, dataB1, dataA2, dataB2);
//for (int y=0, i=0; y<size; y++)
// for (int x=0; x<size; x++, i++)
// {
// System.out.printf("[%d,%d = %d] %f ?= %f\n", x, y, i, numeratorE[i], numeratorA[i]);
// }
Assert.assertArrayEquals("numerator", numeratorE, numeratorA, 0);
Assert.assertArrayEquals("absFFT1", absFFT1E, absFFT1A, 0);
Assert.assertArrayEquals("absFFT2", absFFT2E, absFFT2A, 0);
FRC.computeMirroredFast(size, numeratorA, absFFT1A, absFFT2A, dataA1, dataB1, dataA2, dataB2);
// Check this.
for (int y = 1; y < size; y++) for (int x = 1, i = y * size + 1; x < size; x++, i++) {
Assert.assertEquals("numerator", numeratorE[i], numeratorA[i], 0);
Assert.assertEquals("absFFT1", absFFT1E[i], absFFT1A[i], 0);
Assert.assertEquals("absFFT2", absFFT2E[i], absFFT2A[i], 0);
}
}
use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.
the class FRCTest method computeMirroredIsFaster.
@Test
public void computeMirroredIsFaster() {
// Sample lines through an image to create a structure.
final int size = 2048;
double[][] data = new double[size * 2][];
RandomGenerator r = new Well19937c(30051977);
for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
data[i++] = new double[] { x + r.nextGaussian() * 5, y + r.nextGaussian() * 5 };
data[i++] = new double[] { x + r.nextGaussian() * 5, y2 + r.nextGaussian() * 5 };
}
// Create 2 images
Rectangle bounds = new Rectangle(0, 0, size, size);
IJImagePeakResults i1 = createImage(bounds);
IJImagePeakResults i2 = createImage(bounds);
int[] indices = Utils.newArray(data.length, 0, 1);
MathArrays.shuffle(indices, r);
for (int i : indices) {
IJImagePeakResults image = i1;
i1 = i2;
i2 = image;
image.add((float) data[i][0], (float) data[i][1], 1);
}
i1.end();
i2.end();
ImageProcessor ip1 = i1.getImagePlus().getProcessor();
ImageProcessor ip2 = i2.getImagePlus().getProcessor();
// Test
FRC frc = new FRC();
FloatProcessor[] fft1, fft2;
fft1 = frc.getComplexFFT(ip1);
fft2 = frc.getComplexFFT(ip2);
final float[] dataA1 = (float[]) fft1[0].getPixels();
final float[] dataB1 = (float[]) fft1[1].getPixels();
final float[] dataA2 = (float[]) fft2[0].getPixels();
final float[] dataB2 = (float[]) fft2[1].getPixels();
final float[] numerator = new float[dataA1.length];
final float[] absFFT1 = new float[dataA1.length];
final float[] absFFT2 = new float[dataA1.length];
TimingService ts = new TimingService(10);
ts.execute(new MyTimingTask("compute") {
public Object run(Object data) {
FRC.compute(numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
return null;
}
});
ts.execute(new MyTimingTask("computeMirrored") {
public Object run(Object data) {
FRC.computeMirrored(size, numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
return null;
}
});
ts.execute(new MyTimingTask("computeMirroredFast") {
public Object run(Object data) {
FRC.computeMirroredFast(size, numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
return null;
}
});
ts.repeat(ts.getSize());
ts.report();
}
Aggregations