ImgLib2 Views.rotate unexpected behaviour

Hi all,

I’m rotating a 2x2 image with ImgLib2 by 90 degrees using Views.rotate. It results in an image with a different coordinate set than the original image. I would not have expected this.

Can someone explain to me what’s happening and how I can avoid it?

Img<IntType> img = new ArrayImgFactory<>(new IntType()).create(2, 2);

printCoordinates(img);
System.out.println("-------");
printCoordinates(Views.rotate(img, 1, 0));
private void printCoordinates(RandomAccessibleInterval<IntType> img) {
	Cursor<IntType> cursor = Views.iterable(img).localizingCursor();
	while(cursor.hasNext()) {
		cursor.next();
		long[] pos = new long[cursor.numDimensions()];
		cursor.localize(pos);
		System.out.println(Arrays.toString(pos));
	}
}

Here is the result:

[0, 0]
[1, 0]
[0, 1]
[1, 1]
-------
[0, -1]
[1, -1]
[0, 0]
[1, 0]

Thanks, Debo

@tpietzsch @axtimwalde

1 Like

It rotates around the origin. This is the only thing that makes sense when the input has no interval bounds. If it is an interval, you probably intuitively expect a rotation and a shift. If your input has minimum coordinate at zero as in your example, then Views.zeroMin(Views.rotate(...)) should give you what you expect.

4 Likes

Thanks for providing me with a solution, I always forget about Views.zeroMin.

1 Like