use of net.imglib2.FinalInterval in project imagej-ops by imagej.
the class DefaultCreateKernelLog method calculate.
@Override
public RandomAccessibleInterval<T> calculate(double[] sigmas) {
final double[] sigmaPixels = new double[sigmas.length];
for (int i = 0; i < sigmaPixels.length; i++) {
// Optimal sigma for LoG approach and dimensionality.
final double sigma_optimal = sigmas[i] / Math.sqrt(sigmas.length);
sigmaPixels[i] = sigma_optimal;
}
final int n = sigmaPixels.length;
final long[] dims = new long[n];
final long[] middle = new long[n];
for (int d = 0; d < n; ++d) {
// The half size of the kernel is 3 standard deviations (or a
// minimum half size of 2)
final int hksizes = Math.max(2, (int) (3 * sigmaPixels[d] + 0.5) + 1);
// add 3 border pixels to achieve smoother derivatives at the border
dims[d] = 3 + 2 * hksizes;
middle[d] = 1 + hksizes;
}
final RandomAccessibleInterval<T> output = createOp.calculate(new FinalInterval(dims));
final Cursor<T> c = Views.iterable(output).cursor();
final long[] coords = new long[sigmas.length];
/*
* The gaussian normalization factor, divided by a constant value. This
* is a fudge factor, that more or less put the quality values close to
* the maximal value of a blob of optimal radius.
*/
final double C = 1d / 20d * Math.pow(1d / sigmas[0] / Math.sqrt(2 * Math.PI), sigmas.length);
// Work in image coordinates
while (c.hasNext()) {
c.fwd();
c.localize(coords);
double mantissa = 0;
double exponent = 0;
for (int d = 0; d < coords.length; d++) {
final double x = (coords[d] - middle[d]);
mantissa += -C * (x * x / sigmas[0] / sigmas[0] - 1d);
exponent += -x * x / 2d / sigmas[0] / sigmas[0];
}
c.get().setReal(mantissa * Math.exp(exponent));
}
return output;
}
use of net.imglib2.FinalInterval in project imagej-ops by imagej.
the class NonCirculantNormalizationFactor method createNormalizationImageSemiNonCirculant.
protected void createNormalizationImageSemiNonCirculant() {
// k is the window size (valid image region)
final int length = k.numDimensions();
final long[] n = new long[length];
final long[] nFFT = new long[length];
// also referred to as object space size
for (int d = 0; d < length; d++) {
n[d] = k.dimension(d) + l.dimension(d) - 1;
}
for (int d = 0; d < length; d++) {
nFFT[d] = imgConvolutionInterval.dimension(d);
}
FinalDimensions fd = new FinalDimensions(nFFT);
// create the normalization image
normalization = create.calculate(fd);
// size of the measurement window
final Point size = new Point(length);
final long[] sizel = new long[length];
for (int d = 0; d < length; d++) {
size.setPosition(k.dimension(d), d);
sizel[d] = k.dimension(d);
}
// starting point of the measurement window when it is centered in fft space
final Point start = new Point(length);
final long[] startl = new long[length];
final long[] endl = new long[length];
for (int d = 0; d < length; d++) {
start.setPosition((nFFT[d] - k.dimension(d)) / 2, d);
startl[d] = (nFFT[d] - k.dimension(d)) / 2;
endl[d] = startl[d] + sizel[d] - 1;
}
// size of the object space
final Point maskSize = new Point(length);
final long[] maskSizel = new long[length];
for (int d = 0; d < length; d++) {
maskSize.setPosition(Math.min(n[d], nFFT[d]), d);
maskSizel[d] = Math.min(n[d], nFFT[d]);
}
// starting point of the object space within the fft space
final Point maskStart = new Point(length);
final long[] maskStartl = new long[length];
for (int d = 0; d < length; d++) {
maskStart.setPosition((Math.max(0, nFFT[d] - n[d]) / 2), d);
maskStartl[d] = (Math.max(0, nFFT[d] - n[d]) / 2);
}
final RandomAccessibleInterval<O> temp = Views.interval(normalization, new FinalInterval(startl, endl));
final Cursor<O> normCursor = Views.iterable(temp).cursor();
// draw a cube the size of the measurement space
while (normCursor.hasNext()) {
normCursor.fwd();
normCursor.get().setReal(1.0);
}
final Img<O> tempImg = create.calculate(fd);
// 3. correlate psf with the output of step 2.
correlater.compute(normalization, tempImg);
normalization = tempImg;
final Cursor<O> cursorN = normalization.cursor();
while (cursorN.hasNext()) {
cursorN.fwd();
if (cursorN.get().getRealFloat() <= 1e-3f) {
cursorN.get().setReal(1.0f);
}
}
}
use of net.imglib2.FinalInterval in project imagej-ops by imagej.
the class OffsetViewTest method defaultOffsetIntervalTest.
@Test
public void defaultOffsetIntervalTest() {
Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 10, 10 }, new DoubleType());
IntervalView<DoubleType> il2 = Views.offsetInterval(img, new FinalInterval(new long[] { 2, 2 }, new long[] { 9, 9 }));
IntervalView<DoubleType> opr = ops.transform().offsetView(img, new FinalInterval(new long[] { 2, 2 }, new long[] { 9, 9 }));
assertEquals(il2.realMax(0), opr.realMax(0), 1e-10);
assertEquals(il2.realMin(0), opr.realMin(0), 1e-10);
assertEquals(il2.realMax(1), opr.realMax(1), 1e-10);
assertEquals(il2.realMin(1), opr.realMin(1), 1e-10);
}
use of net.imglib2.FinalInterval in project imagej-ops by imagej.
the class DefaultDistanceTransformTest method test.
@SuppressWarnings("unchecked")
@Test
public void test() {
// create 4D image
final RandomAccessibleInterval<BitType> in = ops.create().img(new FinalInterval(20, 20, 5, 3), new BitType());
generate4DImg(in);
/*
* test normal DT
*/
RandomAccessibleInterval<FloatType> out = (RandomAccessibleInterval<FloatType>) ops.run(DefaultDistanceTransform.class, null, in);
compareResults(out, in, new double[] { 1, 1, 1, 1 });
/*
* test calibrated DT
*/
final double[] calibration = new double[] { 3.74, 5.19, 1.21, 2.21 };
out = (RandomAccessibleInterval<FloatType>) ops.run(DefaultDistanceTransformCalibration.class, null, in, calibration);
compareResults(out, in, calibration);
}
use of net.imglib2.FinalInterval in project imagej-ops by imagej.
the class CreateImgTest method testCreateFromRaiDifferentType.
@Test
public void testCreateFromRaiDifferentType() {
final IntervalView<ByteType> input = Views.interval(PlanarImgs.bytes(10, 10, 10), new FinalInterval(new long[] { 10, 10, 1 }));
final Img<?> res = (Img<?>) ops.run(CreateImgFromDimsAndType.class, input, new ShortType());
assertEquals("Image Type: ", ShortType.class, res.firstElement().getClass());
assertArrayEquals("Image Dimensions: ", Intervals.dimensionsAsLongArray(input), Intervals.dimensionsAsLongArray(res));
assertEquals("Image Factory: ", ArrayImgFactory.class, res.factory().getClass());
}
Aggregations