Java Code Examples for net.imglib2.view.Views#offsetInterval()

The following examples show how to use net.imglib2.view.Views#offsetInterval() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommitCanvasN5.java    From paintera with GNU General Public License v2.0 6 votes vote down vote up
private static <I extends IntegerType<I> & NativeType<I>> BlockDiff downsampleIntegerTypeAndSerialize(
		final N5Writer n5,
		final String dataset,
		final DatasetAttributes attributes,
		final RandomAccessibleInterval<I> data,
		final int[] relativeFactors,
		final int[] size,
		final Interval blockInterval,
		final long[] blockPosition
) throws IOException {
	final I i = Util.getTypeFromInterval(data).createVariable();
	i.setInteger(Label.OUTSIDE);
	final RandomAccessibleInterval<I> input = Views.isZeroMin(data) ? data : Views.zeroMin(data);
	final RandomAccessibleInterval<I> output = new ArrayImgFactory<>(i).create(size);
	WinnerTakesAll.downsample(input, output, relativeFactors);

	final RandomAccessibleInterval<I> previousContents = Views.offsetInterval(N5Utils.<I>open(n5, dataset), blockInterval);
	final BlockDiff blockDiff = createBlockDiffInteger(previousContents, output);

	N5Utils.saveBlock(output, n5, dataset, attributes, blockPosition);
	return blockDiff;
}
 
Example 2
Source File: CropRAI.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public RandomAccessibleInterval<T> calculate(final RandomAccessibleInterval<T> input, final Interval interval) {
	boolean oneSizedDims = false;

	if (dropSingleDimensions) {
		for (int d = 0; d < interval.numDimensions(); d++) {
			if (interval.dimension(d) == 1) {
				oneSizedDims = true;
				break;
			}
		}
	}

	if (Intervals.equals(input, interval) && !oneSizedDims)
		return input;
	if (!Intervals.contains(input, interval))
		throw new RuntimeException("Intervals don't match!");
	IntervalView<T> res = Views.offsetInterval(input, interval);
	return oneSizedDims ? Views.dropSingletonDimensions(res) : res;
}
 
Example 3
Source File: LocalThresholdIntegral.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Removes leading 0s from integral image after composite creation.
 *
 * @param input Input RAI (can be a RAI of Composite)
 * @return An extended and cropped version of input
 */
private <T> RandomAccessibleInterval<T> removeLeadingZeros(
	final RandomAccessibleInterval<T> input)
{
	// Remove 0s from integralImg by shifting its interval by +1
	final long[] min = Intervals.minAsLongArray(input);
	final long[] max = Intervals.maxAsLongArray(input);

	for (int d = 0; d < input.numDimensions(); ++d) {
		int correctedSpan = getShape().getSpan() - 1;
		min[d] += (1 + correctedSpan);
		max[d] -= correctedSpan;
	}

	// Define the Interval on the infinite random accessibles
	final FinalInterval interval = new FinalInterval(min, max);

	final RandomAccessibleInterval<T> extendedImg = Views.offsetInterval(Views
		.extendBorder(input), interval);
	return extendedImg;
}
 
Example 4
Source File: OutlineTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Test the op with a 2x2 square. The square is in the middle of a 4x4 img */
@Test
public void testSquare() {
	// SETUP
	final Img<BitType> img = ArrayImgs.bits(4, 4);
	final IntervalView<BitType> square = Views.offsetInterval(img, new long[] {
		1, 1 }, new long[] { 2, 2 });
	square.cursor().forEachRemaining(BitType::setOne);

	// EXECUTE
	final Img<BitType> result = (Img<BitType>) ops.morphology().outline(img,
		Boolean.TRUE);

	// VERIFY
	assertEquals("Wrong number of foreground elements in interval", 4,
		countForeground(result));
	final IntervalView<BitType> resultSquare = Views.offsetInterval(result,
		new long[] { 1, 1 }, new long[] { 2, 2 });
	assertTrue("Wrong number of foreground elements in object", allForeground(
		resultSquare));
}
 
Example 5
Source File: OutlineTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Test the op with a 3x3 square with a hole in the middle. The square is in
 * the middle of a 5x5 img
 */
@Test
public void testOutlineSquare() {
	// SETUP
	final Img<BitType> img = ArrayImgs.bits(5, 5);
	final IntervalView<BitType> square = Views.offsetInterval(img, new long[] {
		1, 1 }, new long[] { 3, 3 });
	square.cursor().forEachRemaining(BitType::setOne);
	final RandomAccess<BitType> access = square.randomAccess();
	access.setPosition(new long[] { 1, 1 });
	access.get().setZero();

	// EXECUTION
	final Img<BitType> result = (Img<BitType>) ops.morphology().outline(img,
		Boolean.TRUE);

	// VERIFY
	assertEquals("Wrong number of foreground elements in interval", 8,
		countForeground(result));
	final IntervalView<BitType> resultSquare = Views.offsetInterval(result,
		new long[] { 1, 1 }, new long[] { 3, 3 });
	assertEquals("Wrong number of foreground elements in object", 8,
		countForeground(resultSquare));
	assertPositionBackground(result, new long[] { 2, 2 });
}
 
Example 6
Source File: OutlineTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Test the op with a 3x3 square starting from (0,1) in a 5x5 img
 *
 * @see Outline#compute(RandomAccessibleInterval, Boolean,
 *      RandomAccessibleInterval)
 * @see #testEdgeSquare()
 */
@Test
public void testEdgeSquare() {
	// SETUP
	final Img<BitType> img = ArrayImgs.bits(5, 5);
	final IntervalView<BitType> square = Views.offsetInterval(img, new long[] {
		0, 1 }, new long[] { 3, 3 });
	square.cursor().forEachRemaining(BitType::setOne);

	// EXECUTION
	final Img<BitType> result = (Img<BitType>) ops.morphology().outline(img,
		Boolean.TRUE);

	// VERIFY
	assertEquals("Wrong number of foreground elements in interval", 7,
		countForeground(result));
	final IntervalView<BitType> resultSquare = Views.offsetInterval(result,
		new long[] { 0, 1 }, new long[] { 3, 3 });
	assertEquals("Wrong number of foreground elements in object", 7,
		countForeground(resultSquare));
	assertPositionBackground(result, new long[] { 0, 2 });
	assertPositionBackground(result, new long[] { 1, 2 });
}
 
Example 7
Source File: OutlineTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Test the op with a 3x3 square starting from (0,1) in a 5x5 img without
 * excluding edges
 *
 * @see Outline#compute(RandomAccessibleInterval, Boolean,
 *      RandomAccessibleInterval)
 * @see #testEdgeSquare()
 */
@Test
public void testEdgeSquareExcludeEdgesFalse() {
	// SETUP
	final Img<BitType> img = ArrayImgs.bits(5, 5);
	final IntervalView<BitType> square = Views.offsetInterval(img, new long[] {
		0, 1 }, new long[] { 3, 3 });
	square.cursor().forEachRemaining(BitType::setOne);

	final Img<BitType> result = (Img<BitType>) ops.morphology().outline(img,
		Boolean.FALSE);

	assertEquals("Wrong number of foreground elements in interval", 8,
		countForeground(result));
	final IntervalView<BitType> resultSquare = Views.offsetInterval(result,
		new long[] { 0, 1 }, new long[] { 3, 3 });
	assertEquals("Wrong number of foreground elements in object", 8,
		countForeground(resultSquare));
	assertPositionBackground(result, new long[] { 1, 2 });
}
 
Example 8
Source File: OutlineTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Test the op with a 3x3x3x3 hypercube. The cube is in the middle of a
 * 5x5x5x5 img
 */
@Test
public void testHyperCube() {
	// SETUP
	final Img<BitType> img = ArrayImgs.bits(5, 5, 5, 5);
	final IntervalView<BitType> hyperCube = Views.offsetInterval(img,
		new long[] { 1, 1, 1, 1 }, new long[] { 3, 3, 3, 3 });
	hyperCube.cursor().forEachRemaining(BitType::setOne);

	// EXECUTE
	final Img<BitType> result = (Img<BitType>) ops.morphology().outline(img,
		Boolean.TRUE);

	// VERIFY
	assertEquals("Wrong number of foreground elements in interval", 80,
		countForeground(result));
	final IntervalView<BitType> resultHyperCube = Views.offsetInterval(result,
		new long[] { 1, 1, 1, 1 }, new long[] { 3, 3, 3, 3 });
	assertEquals("Wrong number of foreground elements in object", 80,
		countForeground(resultHyperCube));
	assertPositionBackground(result, new long[] { 2, 2, 2, 2 });
}
 
Example 9
Source File: BoxCountTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testHyperCube() {
	// SETUP
	final double[] expectedSizes = DoubleStream.of(4, 2, 1).map(i -> -Math.log(
		i)).toArray();
	final double[] expectedCounts = DoubleStream.of(1, 16, 16).map(Math::log)
		.toArray();
	final Img<BitType> img = ArrayImgs.bits(4, 4, 4, 4);
	final IntervalView<BitType> hyperView = Views.offsetInterval(img,
		new long[] { 1, 1, 1, 1 }, new long[] { 2, 2, 2, 2 });
	hyperView.forEach(BitType::setOne);

	// EXECUTE
	final List<ValuePair<DoubleType, DoubleType>> points = ops.topology()
		.boxCount(img, 4L, 1L, 2.0);

	// VERIFY
	for (int i = 0; i < expectedSizes.length; i++) {
		assertEquals(expectedSizes[i], points.get(i).a.get(), 1e-12);
		assertEquals(expectedCounts[i], points.get(i).b.get(), 1e-12);
	}
}
 
Example 10
Source File: BoxCountTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Test box counting with a hyper cube and one grid translation (should find a
 * better fit than in @see {@link #testHyperCube()})
 */
@Test
public void testHyperCubeTranslations() {
	final double[] expectedSizes = DoubleStream.of(4, 2, 1).map(i -> -Math.log(
		i)).toArray();
	final double[] expectedCounts = DoubleStream.of(1, 1, 16).map(Math::log)
		.toArray();
	final Img<BitType> img = ArrayImgs.bits(4, 4, 4, 4);
	final IntervalView<BitType> hyperView = Views.offsetInterval(img,
		new long[] { 1, 1, 1, 1 }, new long[] { 2, 2, 2, 2 });
	hyperView.forEach(BitType::setOne);

	// EXECUTE
	final List<ValuePair<DoubleType, DoubleType>> points = ops.topology()
		.boxCount(img, 4L, 1L, 2.0, 1L);

	// VERIFY
	for (int i = 0; i < expectedSizes.length; i++) {
		assertEquals(expectedSizes[i], points.get(i).a.get(), 1e-12);
		assertEquals(expectedCounts[i], points.get(i).b.get(), 1e-12);
	}
}
 
Example 11
Source File: SlicesII.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public RandomAccessibleInterval<T> get() {
	localize(tmpPosition);

	final long[] offset = tmpPosition.clone();
	for (int d = 0; d < max.length; d++) {
		offset[d] += sliceOffset[d];
	}

	final IntervalView<T> res = Views.offsetInterval(src, offset, sliceDims);

	return dropSingltonDimensions ? Views.dropSingletonDimensions(res) : res;
}
 
Example 12
Source File: OffsetViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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);
}
 
Example 13
Source File: OffsetViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void defaultOffsetStartEndTest() {

	Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 10, 10 }, new DoubleType());

	IntervalView<DoubleType> il2 = Views.offsetInterval(img, new long[] { 2, 2 }, new long[] { 9, 9 });
	IntervalView<DoubleType> opr = ops.transform().offsetView(img, 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);
}
 
Example 14
Source File: OffsetViewInterval.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public IntervalView<T> calculate(RandomAccessible<T> input) {
	return Views.offsetInterval(input, interval);
}
 
Example 15
Source File: OffsetViewOriginSize.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public IntervalView<T> calculate(RandomAccessible<T> input) {
	return Views.offsetInterval(input, origin, dimension);
}