use of org.jtransforms.dht.FloatDHT_2D in project GDSC-SMLM by aherbert.
the class FhtFilter method initialiseKernel.
/**
* Initialise the kernel FHT. It is recreated only if the target size has changed.
*
* @param maxx the width of the target image
* @param maxy the height of the target image
*/
public void initialiseKernel(int maxx, int maxy) {
final int maxN = MathUtils.nextPow2(MathUtils.max(maxx, maxy, kn));
final int size = maxN * maxN;
if (tmp == null || tmp.length != size) {
tmp = new float[size];
}
if (kernelFht != null && maxN == kernelFht.getWidth()) {
// Already initialised
return;
}
// No window function for the kernel so just create a new FHT
float[] data;
if (kw < maxN || kh < maxN) {
// Too small so insert in the middle
data = new float[size];
final int x = getInsert(maxN, kw);
final int y = getInsert(maxN, kh);
insert(kernel, kw, data, maxN, x, y);
} else {
// Clone to avoid destroying data
data = kernel.clone();
}
// Do the transform using JTransforms as it is faster. Do not allow multi-threading.
final long begin = CommonUtils.getThreadsBeginN_2D();
CommonUtils.setThreadsBeginN_2D(Long.MAX_VALUE);
dht = new FloatDHT_2D(maxN, maxN);
CommonUtils.setThreadsBeginN_2D(begin);
dht.forward(data);
kernelFht = new Fht(data, maxN, true);
if (operation == Operation.DECONVOLUTION) {
kernelFht.initialiseFastOperations();
} else {
kernelFht.initialiseFastMultiply();
}
}
use of org.jtransforms.dht.FloatDHT_2D in project GDSC-SMLM by aherbert.
the class JTransformsTest method canComputeFhtUsingJTransforms.
@Test
void canComputeFhtUsingJTransforms() {
// Note: no need to test the correlation as the transformed data
// is the same format as FHT so we just test that.
final int size = 16;
final int ex = 5;
final int ey = 7;
final int ox = 1;
final int oy = 2;
final FloatProcessor fp1 = createProcessor(size, ex, ey, 4, 4, null);
final FloatProcessor fp2 = createProcessor(size, size / 2 + ox, size / 2 + oy, 4, 4, null);
final float[] input1 = (float[]) fp1.getPixels();
final float[] input2 = (float[]) fp2.getPixels();
final Fht fht1 = new Fht(input1.clone(), size, false);
final Fht fht2 = new Fht(input2.clone(), size, false);
fht1.transform();
fht2.transform();
// Do the same with JTransforms
final FloatDHT_2D dht = new FloatDHT_2D(size, size);
dht.forward(input1);
dht.forward(input2);
Assertions.assertArrayEquals(fht1.getData(), input1, 1e-5f);
Assertions.assertArrayEquals(fht2.getData(), input2, 1e-5f);
}
use of org.jtransforms.dht.FloatDHT_2D in project GDSC-SMLM by aherbert.
the class FloatDht2D method fromDft.
/**
* Convert a discrete Fourier transform (DFT) to a DHT.
*
* @param real the real component
* @param imaginary the imaginary component
* @param tmp the tmp buffer to use for the result
* @return the DHT
* @throws IllegalArgumentException If there is a dimension mismatch
*/
public static FloatDht2D fromDft(FloatImage2D real, FloatImage2D imaginary, float[] tmp) {
if (real.nr != imaginary.nr || real.nc != imaginary.nc) {
throw new IllegalArgumentException("Dimension mismatch");
}
final float[] re = real.getData();
final float[] im = imaginary.getData();
if (tmp == null || tmp.length != re.length) {
tmp = new float[re.length];
}
final int nc = real.nc;
final int nr = real.nr;
for (int r = 0, nrMinusR = 0, i = 0; r < nr; r++, nrMinusR = nr - r) {
for (int c = 0, ncMinusC = 0; c < nc; c++, ncMinusC = nc - c, i++) {
final int j = nrMinusR * nc + ncMinusC;
// Reverse the toDFT() method
// re = (a+b)/2
// im = (-a+b)/2
// b = re + im
// a = 2*re - b
tmp[j] = re[i] + im[i];
tmp[i] = 2 * re[i] - tmp[j];
}
}
return new FloatDht2D(nc, nr, tmp, true, new FloatDHT_2D(nr, nc));
}
Aggregations