Java Code Examples for net.imglib2.img.array.ArrayImgs#unsignedBytes()

The following examples show how to use net.imglib2.img.array.ArrayImgs#unsignedBytes() . 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: VolumeTimeseriesDemo.java    From sciview with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Img<UnsignedByteType> hardCopy(RandomAccessibleInterval<UnsignedByteType> img) {
    Img<UnsignedByteType> out =
            ArrayImgs.unsignedBytes(
                    img.dimension(0),
                    img.dimension(1),
                    img.dimension(2),
                    img.dimension(3));
    RandomAccess<UnsignedByteType> imgAccess = img.randomAccess();

    Cursor<UnsignedByteType> outCur = out.localizingCursor();
    while( outCur.hasNext() ) {
        outCur.fwd();
        imgAccess.setPosition(outCur);
        outCur.get().set(imgAccess.get());
    }

    return out;
}
 
Example 2
Source File: ASCIITest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testDefaultASCII() {
	// character set used in DefaultASCII, could be updated if necessary
	final String CHARS = "#O*o+-,. ";
	final int len = CHARS.length();
	final int width = 10;
	final int offset = 47;
	final byte[] array = new byte[width * len];
	for (int i = 0; i < len; i++) {
		for (int j = 0; j < width; j++) {
			array[i * width + j] = (byte) (offset + i * width + j);
		}
	}
	final Img<UnsignedByteType> img = ArrayImgs.unsignedBytes(array, width,
		len);
	final String ascii = (String) ops.run(DefaultASCII.class, img);
	for (int i = 0; i < len; i++) {
		for (int j = 0; j < width; j++) {
			assertTrue(ascii.charAt(i * (width + 1) + j) == CHARS.charAt(i));
		}
		assertTrue(ascii.charAt(i * (width + 1) + width) == '\n');
	}
}
 
Example 3
Source File: DefaultCoarsenessFeature.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Apply mean filter with given size of reactangle shape
 * 
 * @param input
 *            Input image
 * @param i
 *            Size of rectangle shape
 * @return Filered mean image
 */
@SuppressWarnings("unchecked")
private Img<I> mean(final RandomAccessibleInterval<I> input, final int i) {

	long[] dims = new long[input.numDimensions()];
	input.dimensions(dims);

	final byte[] array = new byte[(int) Intervals.numElements(new FinalInterval(dims))];
	Img<I> meanImg = (Img<I>) ArrayImgs.unsignedBytes(array, dims);

	OutOfBoundsMirrorFactory<ByteType, Img<ByteType>> oobFactory = new OutOfBoundsMirrorFactory<>(
			Boundary.SINGLE);

	ops().run(MeanFilterOp.class, meanImg, input, new RectangleShape(i, true), oobFactory);

	return meanImg;
}
 
Example 4
Source File: AbstractFeatureTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * @param dim a long array with the desired dimensions of the image
 * @param constValue constant image value
 * @return an {@link Img} of {@link UnsignedByteType} filled with a constant
 *         value.
 */
public Img<UnsignedByteType> getConstantUnsignedByteImg(final long[] dim,
	final int constValue)
{
	final ArrayImg<UnsignedByteType, ByteArray> img = ArrayImgs.unsignedBytes(
		dim);

	final UnsignedByteType type = img.firstElement();
	if (constValue < type.getMinValue() || constValue >= type.getMaxValue()) {
		throw new IllegalArgumentException("Can't create image for constant [" +
			constValue + "]");
	}

	final ArrayCursor<UnsignedByteType> cursor = img.cursor();
	while (cursor.hasNext()) {
		cursor.next().set(constValue);
	}

	return img;
}
 
Example 5
Source File: ConvertMapTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static Img<UnsignedByteType> generateUnsignedByteImg(
	final byte[] values)
{

	final byte[] array =
		new byte[(int) Intervals.numElements(new FinalInterval(dims))];

	if (array.length != values.length) {
		throw new RuntimeException("Number of values doesn't match dimmensions");
	}

	for (int i = 0; i < array.length; i++) {
		array[i] = values[i];
	}

	return ArrayImgs.unsignedBytes(array, dims);
}
 
Example 6
Source File: ShuffledViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testSameSeed() {
	// FIRST - create 6x6 image filled with known values
	ArrayImg<UnsignedByteType, ByteArray> inputImage = ArrayImgs.unsignedBytes(
		new byte[] { //
			1, 2, 3, 4, 5, 6, //
			7, 8, 9, 10, 11, 12, //
			13, 14, 15, 16, 17, 18, //
			19, 20, 21, 22, 23, 24, //
			25, 26, 27, 28, 29, 30, //
			31, 32, 33, 34, 35, 36 //
		}, 6, 6);
	int[] blockSize = { 2, 2 };
	long seed = 0xdeadbeef;
	ShuffledView<UnsignedByteType> shuffled01 = new ShuffledView<>(inputImage,
		blockSize, seed);
	ShuffledView<UnsignedByteType> shuffled02 = new ShuffledView<>(inputImage,
		blockSize, seed);
	assertIterationsEqual(Views.iterable(shuffled01), Views.iterable(
		shuffled02));
}
 
Example 7
Source File: ShuffledViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testShuffleView() {
	// FIRST - create 6x6 image filled with known values
	ArrayImg<UnsignedByteType, ByteArray> actualInputImage = ArrayImgs
		.unsignedBytes(new byte[] { //
			1, 2, 3, 4, 5, 6, //
			7, 8, 9, 10, 11, 12, //
			13, 14, 15, 16, 17, 18, //
			19, 20, 21, 22, 23, 24, //
			25, 26, 27, 28, 29, 30, //
			31, 32, 33, 34, 35, 36 //
	}, 6, 6);
	int[] blockSize = { 2, 2 };
	long seed = 0xdeadbeef;
	ShuffledView<UnsignedByteType> shuffled = new ShuffledView<>(
		actualInputImage, blockSize, seed);
	ArrayImg<UnsignedByteType, ByteArray> expected = ArrayImgs.unsignedBytes(
		new byte[] { //
			27, 28, 3, 4, 15, 16, //
			33, 34, 9, 10, 21, 22, //
			5, 6, 17, 18, 13, 14, //
			11, 12, 23, 24, 19, 20, //
			1, 2, 29, 30, 25, 26, //
			7, 8, 35, 36, 31, 32 //
		}, 6, 6);
	assertIterationsEqual(expected, Views.iterable(shuffled));
}
 
Example 8
Source File: AbstractOpTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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;
}
 
Example 9
Source File: LinearIntensityMap.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
public static void main( final String[] args )
{
	new ImageJ();

	final double[] coefficients = new double[]{
			0, 2, 4, 8,
			1, 1, 1, 1,
			1, 10, 5, 1,
			1, 1, 1, 1,

			0, 10, 20, 30,
			40, 50, 60, 70,
			80, 90, 100, 110,
			120, 130, 140, 150
	};

	final LinearIntensityMap< DoubleType > transform = new LinearIntensityMap< DoubleType >( ArrayImgs.doubles( coefficients, 4, 4, 2 ) );

	//final ImagePlus imp = new ImagePlus( "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png" );
	final ImagePlus imp1 = new ImagePlus( "http://fly.mpi-cbg.de/~saalfeld/Pictures/norway.jpg");

	final ArrayImg< FloatType, FloatArray > image1 = ArrayImgs.floats( ( float[] )imp1.getProcessor().convertToFloatProcessor().getPixels(), imp1.getWidth(), imp1.getHeight() );
	final ArrayImg< UnsignedByteType, ByteArray > image2 = ArrayImgs.unsignedBytes( ( byte[] )imp1.getProcessor().convertToByteProcessor().getPixels(), imp1.getWidth(), imp1.getHeight() );
	final ArrayImg< UnsignedShortType, ShortArray > image3 = ArrayImgs.unsignedShorts( ( short[] )imp1.getProcessor().convertToShortProcessor().getPixels(), imp1.getWidth(), imp1.getHeight() );
	final ArrayImg< ARGBType, IntArray > image4 = ArrayImgs.argbs( ( int[] )imp1.getProcessor().getPixels(), imp1.getWidth(), imp1.getHeight() );

	ImageJFunctions.show( ArrayImgs.doubles( coefficients, 4, 4, 2 ) );

	transform.run( image1 );
	transform.run( image2 );
	transform.run( image3 );
	transform.run( image4 );

	ImageJFunctions.show( image1 );
	ImageJFunctions.show( image2 );
	ImageJFunctions.show( image3 );
	ImageJFunctions.show( image4 );
}
 
Example 10
Source File: ShuffledViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testAllShuffle() {
	// FIRST - create 6x6 image filled with known values
	ArrayImg<UnsignedByteType, ByteArray> actualInputImage = ArrayImgs
		.unsignedBytes(new byte[] { //
			1, 2, 3, 4, 5, 6, //
			7, 8, 9, 10, 11, 12, //
			13, 14, 15, 16, 17, 18, //
			19, 20, 21, 22, 23, 24, //
			25, 26, 27, 28, 29, 30, //
			31, 32, 33, 34, 35, 36 //
	}, 6, 6);
	int[] blockSize = { 1, 1 };
	long seed = 0xdeadbeef;
	ShuffledView<UnsignedByteType> shuffled = new ShuffledView<>(
		actualInputImage, blockSize, seed);
	ArrayImg<UnsignedByteType, ByteArray> expected = ArrayImgs.unsignedBytes(
		new byte[] { //
			33, 19, 14, 36, 31, 32, //
			34, 21, 17, 30, 35, 1, //
			7, 28, 29, 20, 9, 12, //
			5, 18, 27, 3, 8, 2, //
			11, 25, 4, 24, 26, 6, //
			23, 10, 13, 15, 22, 16 //
		}, 6, 6);
	assertIterationsEqual(expected, Views.iterable(shuffled));
}
 
Example 11
Source File: ShuffledViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testNonSquareBlocks2() {
	// FIRST - create 6x6 image filled with known values
	ArrayImg<UnsignedByteType, ByteArray> actualInputImage = ArrayImgs
		.unsignedBytes(new byte[] { //
			1, 2, 3, 4, 5, 6, //
			7, 8, 9, 10, 11, 12, //
			13, 14, 15, 16, 17, 18, //
			19, 20, 21, 22, 23, 24, //
			25, 26, 27, 28, 29, 30, //
			31, 32, 33, 34, 35, 36 //
	}, 6, 6);
	int[] blockSize = { 3, 2 };
	long seed = 0xdeadbeef;
	ShuffledView<UnsignedByteType> shuffled = new ShuffledView<>(
		actualInputImage, blockSize, seed);
	ArrayImg<UnsignedByteType, ByteArray> expected = ArrayImgs.unsignedBytes(
		new byte[] { //
			25, 26, 27, 13, 14, 15, //
			31, 32, 33, 19, 20, 21, //
			1, 2, 3, 28, 29, 30, //
			7, 8, 9, 34, 35, 36, //
			4, 5, 6, 16, 17, 18, //
			10, 11, 12, 22, 23, 24 //
		}, 6, 6);
	assertIterationsEqual(expected, Views.iterable(shuffled));
}
 
Example 12
Source File: ShuffledViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testNonSquareBlocks1() {
	// FIRST - create 6x6 image filled with known values
	ArrayImg<UnsignedByteType, ByteArray> actualInputImage = ArrayImgs
		.unsignedBytes(new byte[] { //
			1, 2, 3, 4, 5, 6, //
			7, 8, 9, 10, 11, 12, //
			13, 14, 15, 16, 17, 18, //
			19, 20, 21, 22, 23, 24, //
			25, 26, 27, 28, 29, 30, //
			31, 32, 33, 34, 35, 36 //
	}, 6, 6);
	int[] blockSize = { 2, 3 };
	long seed = 0xdeadbeef;
	ShuffledView<UnsignedByteType> shuffled = new ShuffledView<>(
		actualInputImage, blockSize, seed);
	ArrayImg<UnsignedByteType, ByteArray> expected = ArrayImgs.unsignedBytes(
		new byte[] { //
			21, 22, 5, 6, 1, 2, //
			27, 28, 11, 12, 7, 8, //
			33, 34, 17, 18, 13, 14, //
			23, 24, 3, 4, 19, 20, //
			29, 30, 9, 10, 25, 26, //
			35, 36, 15, 16, 31, 32 //
		}, 6, 6);
	assertIterationsEqual(expected, Views.iterable(shuffled));
}
 
Example 13
Source File: ShuffledViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public <T extends RealType<T>, U extends RealType<U>> void testDiffSeeds() {
	// FIRST - create 6x6 image filled with known values
	ArrayImg<UnsignedByteType, ByteArray> inputImage = ArrayImgs.unsignedBytes(
		new byte[] { //
			1, 2, 3, 4, 5, 6, //
			7, 8, 9, 10, 11, 12, //
			13, 14, 15, 16, 17, 18, //
			19, 20, 21, 22, 23, 24, //
			25, 26, 27, 28, 29, 30, //
			31, 32, 33, 34, 35, 36 //
		}, 6, 6);
	int[] blockSize = { 2, 2 };
	long seed1 = 0xdeadbeef;
	long seed2 = 0x22334455;
	ShuffledView<UnsignedByteType> shuffled1 = new ShuffledView<>(inputImage,
		blockSize, seed1);
	ArrayImg<UnsignedByteType, ByteArray> expected1 = ArrayImgs.unsignedBytes(
		new byte[] { //
			27, 28, 3, 4, 15, 16, //
			33, 34, 9, 10, 21, 22, //
			5, 6, 17, 18, 13, 14, //
			11, 12, 23, 24, 19, 20, //
			1, 2, 29, 30, 25, 26, //
			7, 8, 35, 36, 31, 32 //
		}, 6, 6);
	ShuffledView<UnsignedByteType> shuffled2 = new ShuffledView<>(inputImage,
		blockSize, seed2);
	ArrayImg<UnsignedByteType, ByteArray> expected2 = ArrayImgs.unsignedBytes(
		new byte[] { //
			29, 30, 25, 26, 17, 18, //
			35, 36, 31, 32, 23, 24, //
			5, 6, 27, 28, 15, 16, //
			11, 12, 33, 34, 21, 22, //
			3, 4, 13, 14, 1, 2, //
			9, 10, 19, 20, 7, 8 //
		}, 6, 6);
	assertIterationsEqual(expected1, Views.iterable(shuffled1));
	assertIterationsEqual(expected2, Views.iterable(shuffled2));
}
 
Example 14
Source File: ShowSegmentationDemo.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private RandomAccessibleInterval<UnsignedByteType> generateDemo(int w, int h, int d, int numSegments) {
    double radiusSq = 36;

    List<RealPoint> points = new ArrayList<>();

    for( int k = 0; k < numSegments; k++ ) {
        points.add( new RealPoint(rng.nextFloat() * w, rng.nextFloat() * h, rng.nextFloat() * d) );
    }

    long[] pos = new long[3];
    RandomAccessibleInterval<UnsignedByteType> img = ArrayImgs.unsignedBytes(w, h, d);
    Cursor<UnsignedByteType> cur = Views.iterable(img).cursor();
    while(cur.hasNext()) {
        cur.fwd();
        cur.localize(pos);

        cur.get().set(0);
        for( int k = 0; k < points.size(); k++ ) {
            double dt = dist(pos, points.get(k));
            //System.out.println(dt + " " + Arrays.toString(pos) + " " + points.get(k));
            if( dt < radiusSq ) {
                cur.get().set(k+1);
            }
        }
    }
    return img;
}
 
Example 15
Source File: AbstractFeatureTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param dim dimensions of the image
 * @param radii of the ellipse
 * @param offset of the ellipse
 * @return an {@link Img} of {@link BitType} filled with a ellipse
 */
@SuppressWarnings({ "deprecation" })
public Img<UnsignedByteType> getEllipsedBitImage(final long[] dim,
	final double[] radii, final double[] offset)
{

	// create empty bittype image with desired dimensions
	final ArrayImg<UnsignedByteType, ByteArray> img = ArrayImgs.unsignedBytes(
		dim);

	// create ellipse
	final EllipseRegionOfInterest ellipse = new EllipseRegionOfInterest();
	ellipse.setRadii(radii);

	// set origin in the center of image
	final double[] origin = new double[dim.length];
	for (int i = 0; i < dim.length; i++)
		origin[i] = dim[i] / 2;
	ellipse.setOrigin(origin);

	// get iterable intervall and cursor of ellipse
	final IterableInterval<UnsignedByteType> ii = ellipse
		.getIterableIntervalOverROI(img);
	final Cursor<UnsignedByteType> cursor = ii.cursor();

	// fill image with ellipse
	while (cursor.hasNext()) {
		cursor.next();
		cursor.get().set(255);
	}

	return img;
}
 
Example 16
Source File: AbstractFeatureTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param dim a long array with the desired dimensions of the image
 * @return an {@link Img} of {@link UnsignedByteType} filled with random
 *         values.
 */
public Img<UnsignedByteType> getRandomUnsignedByteImg(final long[] dim) {
	final ArrayImg<UnsignedByteType, ByteArray> img = ArrayImgs.unsignedBytes(
		dim);

	final UnsignedByteType type = img.firstElement();

	final ArrayCursor<UnsignedByteType> cursor = img.cursor();
	while (cursor.hasNext()) {
		cursor.next().set(rand.nextInt((int) type.getMaxValue()));
	}

	return img;
}
 
Example 17
Source File: ImageMomentsTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@BeforeClass
public static void createImg() {

	Img<UnsignedByteType> tmp =
		ArrayImgs.unsignedBytes(new long[] { 100, 100 });

	Random rand = new Random(1234567890L);
	final Cursor<UnsignedByteType> cursor = tmp.cursor();
	while (cursor.hasNext()) {
		cursor.next().set(rand.nextInt((int) tmp.firstElement().getMaxValue()));
	}

	img = tmp;
}
 
Example 18
Source File: GameOfLife3D.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    public void run() {
        field = ArrayImgs.unsignedBytes( w, h, d );
        randomize();

        dialog = new GenericDialog("Game of Life 3D");
        dialog.addNumericField("Starvation threshold", starvation, 0);
        dialog.addNumericField("Birth threshold", birth, 0);
        dialog.addNumericField("Suffocation threshold", suffocation, 0);
        dialog.addNumericField("Initial saturation % when randomizing", saturation, 0);
        dialog.showDialog();

        if( dialog.wasCanceled() ) return;

        starvation = (int) dialog.getNextNumber();
        birth = (int) dialog.getNextNumber();
        suffocation = (int) dialog.getNextNumber();
        saturation = (int) dialog.getNextNumber();

        randomize();
        play();

//
//    @Parameter(callback = "iterate")
//    private Button iterate;
//
//    @Parameter(callback = "randomize")
//    private Button randomize;
//
//    @Parameter(callback = "play")
//    private Button play;
//
//    @Parameter(callback = "pause")
//    private Button pause;

        //play();

        //eventService.subscribe(this);
    }
 
Example 19
Source File: FSLoader.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected boolean mapIntensities(final Patch p, final ImagePlus imp) {
	
	final ImagePlus coefficients = new Opener().openImage(
		getUNUIdFolder() +
		"trakem2.its/" +
		createIdPath(Long.toString(p.getId()), "it", ".tif"));

	if (coefficients == null)
		return false;
	
	final ImageProcessor ip = imp.getProcessor();
	
	@SuppressWarnings({"rawtypes"})
	final LinearIntensityMap<FloatType> map =
			new LinearIntensityMap<FloatType>(
					(FloatImagePlus)ImagePlusImgs.from(coefficients));

	@SuppressWarnings("rawtypes")
	Img img;

	final long[] dims = new long[]{imp.getWidth(), imp.getHeight()};
	switch (p.getType()) {
	case ImagePlus.GRAY8:
	case ImagePlus.COLOR_256:		// this only works for continuous color tables
		img = ArrayImgs.unsignedBytes((byte[])ip.getPixels(), dims);
		break;
	case ImagePlus.GRAY16:
		img = ArrayImgs.unsignedShorts((short[])ip.getPixels(), dims);
		break;
	case ImagePlus.COLOR_RGB:
		img = ArrayImgs.argbs((int[])ip.getPixels(), dims);
		break;
	case ImagePlus.GRAY32:
		img = ArrayImgs.floats((float[])ip.getPixels(), dims);
		break;
	default:
		img = null;
	}
	
	if (img == null)
		return false;

	map.run(img);
	
	return true;
}
 
Example 20
Source File: AbstractFeatureTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * @param dim a long array with the desired dimensions of the image
 * @return an empty {@link Img} of {@link UnsignedByteType}.
 */
public Img<UnsignedByteType> getEmptyUnsignedByteImg(final long[] dim) {
	return ArrayImgs.unsignedBytes(dim);
}