use of net.imglib2.Cursor in project imagej-ops by imagej.
the class SubsampleViewTest method testIntervalSubsample.
@Test
public void testIntervalSubsample() {
Img<DoubleType> img = ArrayImgs.doubles(10, 10);
Random r = new Random();
for (DoubleType d : img) {
d.set(r.nextDouble());
}
SubsampleIntervalView<DoubleType> expected = Views.subsample((RandomAccessibleInterval<DoubleType>) img, 2);
SubsampleIntervalView<DoubleType> actual = (SubsampleIntervalView<DoubleType>) ops.transform().subsampleView((RandomAccessibleInterval<DoubleType>) img, 2);
Cursor<DoubleType> il2C = Views.interval(expected, new long[] { 0, 0 }, new long[] { 4, 4 }).localizingCursor();
RandomAccess<DoubleType> oprRA = actual.randomAccess();
while (il2C.hasNext()) {
il2C.next();
oprRA.setPosition(il2C);
assertEquals(il2C.get().get(), oprRA.get().get(), 1e-10);
}
assertTrue(Intervals.equals(expected, actual));
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class PartialDerivativeFilterTest method test.
@Test
public void test() {
Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 20, 20 });
Cursor<FloatType> cursorImg = img.cursor();
int counterX = 0;
int counterY = 0;
while (cursorImg.hasNext()) {
if (counterX > 8 && counterX < 12 || counterY > 8 && counterY < 12) {
cursorImg.next().setOne();
} else {
cursorImg.next().setZero();
}
counterX++;
if (counterX % 20 == 0) {
counterY++;
}
if (counterX == 20) {
counterX = 0;
}
if (counterY == 20) {
counterY = 0;
}
}
RandomAccessibleInterval<FloatType> out = ops.filter().partialDerivative(img, 0);
FloatType type = Util.getTypeFromInterval(out).createVariable();
type.set(4.0f);
RandomAccess<FloatType> outRA = out.randomAccess();
for (int i = 0; i < 8; i++) {
outRA.setPosition(new int[] { 9, i });
assertEquals(type, outRA.get());
}
outRA.setPosition(new int[] { 9, 8 });
type.set(3.0f);
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 9, 10 });
type.set(0.0f);
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 9, 11 });
type.set(1.0f);
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 9, 12 });
type.set(3.0f);
assertEquals(type, outRA.get());
type.set(4.0f);
for (int i = 13; i < 20; i++) {
outRA.setPosition(new int[] { 9, i });
assertEquals(type, outRA.get());
}
type.set(-4.0f);
for (int i = 0; i < 8; i++) {
outRA.setPosition(new int[] { 12, i });
assertEquals(type, outRA.get());
}
outRA.setPosition(new int[] { 12, 8 });
type.set(-3.0f);
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 12, 10 });
type.set(0.0f);
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 12, 11 });
type.set(-1.0f);
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 12, 12 });
type.set(-3.0f);
assertEquals(type, outRA.get());
type.set(-4.0f);
for (int i = 13; i < 20; i++) {
outRA.setPosition(new int[] { 12, i });
assertEquals(type, outRA.get());
}
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class HessianFilterTest method test.
@Test
public void test() {
Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 50, 50 });
Cursor<FloatType> cursorImg = img.cursor();
int counterX = 0;
int counterY = 0;
while (cursorImg.hasNext()) {
if (counterX > 20 && counterX < 30 || counterY > 20 && counterY < 30) {
cursorImg.next().setOne();
} else {
cursorImg.next().setZero();
}
counterX++;
if (counterX % 50 == 0) {
counterY++;
}
if (counterX == 50) {
counterX = 0;
}
if (counterY == 50) {
counterY = 0;
}
}
CompositeIntervalView<FloatType, RealComposite<FloatType>> out = ops.filter().hessian(img);
Cursor<RealComposite<FloatType>> outCursor = Views.iterable(out).cursor();
while (outCursor.hasNext()) {
RealComposite<FloatType> values = outCursor.next();
assertEquals(values.get(1), values.get(2));
}
CompositeView<FloatType, RealComposite<FloatType>>.CompositeRandomAccess outRA = out.randomAccess();
// two numbers represent a coordinate: 20|0 ; 21|0 ...
int[] positions = new int[] { 20, 0, 21, 0, 19, 31, 19, 30 };
float[] valuesXX = new float[] { 16.0f, -16.0f, 15.0f, 11.0f };
float[] valuesXY = new float[] { 0.0f, 0.0f, 1.0f, 3.0f };
float[] valuesYY = new float[] { 0.0f, 0.0f, 15.0f, 15.0f };
FloatType type = Util.getTypeFromInterval(img).createVariable();
int i = 0;
int j = 0;
while (i < positions.length - 1) {
int[] pos = new int[2];
pos[0] = positions[i];
pos[1] = positions[i + 1];
outRA.setPosition(pos);
type.set(valuesXX[j]);
assertEquals(type, outRA.get().get(0));
outRA.setPosition(pos);
type.set(valuesXY[j]);
assertEquals(type, outRA.get().get(1));
outRA.setPosition(pos);
type.set(valuesYY[j]);
assertEquals(type, outRA.get().get(3));
i += 2;
j++;
}
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class SobelFilterTest method test.
@Test
public void test() {
Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 20, 20 });
Cursor<FloatType> cursorImg = img.cursor();
int counterX = 0;
int counterY = 0;
while (cursorImg.hasNext()) {
if (counterX > 8 && counterX < 12 || counterY > 8 && counterY < 12) {
cursorImg.next().setOne();
} else {
cursorImg.next().setZero();
}
counterX++;
if (counterX % 20 == 0) {
counterY++;
}
if (counterX == 20) {
counterX = 0;
}
if (counterY == 20) {
counterY = 0;
}
}
RandomAccessibleInterval<FloatType> out = ops.filter().sobel(img);
RandomAccess<FloatType> outRA = out.randomAccess();
outRA.setPosition(new int[] { 0, 8 });
FloatType type = Util.getTypeFromInterval(out).createVariable();
type.set(4.0f);
assertEquals(type, outRA.get());
type.setZero();
outRA.setPosition(new int[] { 0, 10 });
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 10, 8 });
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 10, 10 });
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 10, 12 });
type.set(0.0f);
assertEquals(type, outRA.get());
outRA.setPosition(new int[] { 12, 10 });
assertEquals(type, outRA.get());
}
use of net.imglib2.Cursor in project imagej-ops by imagej.
the class AbstractOpTest method generateRandomlyFilledUnsignedByteTestImgWithSeed.
public Img<UnsignedByteType> generateRandomlyFilledUnsignedByteTestImgWithSeed(final long[] dims, final long tempSeed) {
final Img<UnsignedByteType> img = ArrayImgs.unsignedBytes(dims);
final Random rand = new Random(tempSeed);
final Cursor<UnsignedByteType> cursor = img.cursor();
while (cursor.hasNext()) {
cursor.next().set(rand.nextInt((int) img.firstElement().getMaxValue()));
}
return img;
}
Aggregations