Error in imglib2-roi Regions.sample

I am not able to understand this error:

from net.imglib2.img.display.imagej import ImageJFunctions as IL
from net.imglib2.img.array import ArrayImgs
from net.imglib2.roi.geom import GeomMasks
from net.imglib2.roi import Masks, Regions

# A binary image
img = ArrayImgs.bits([512, 512, 5])

# Add some data to it
center = img.dimension(0) / 2, img.dimension(1) / 2
for z in xrange(img.dimension(2)):
  radius = img.dimension(0) * 0.4 / (z + 1)
  circle = GeomMasks.openSphere(center, radius)
  for t in Regions.sample(Masks.toIterableRegion(circle), img):
    t.setOne()

IL.wrap(img, "bit img").show()

The error: java.lang.ArrayIndexOutOfBoundsException: 2

Started New_.py at Wed May 13 10:50:56 BST 2020
Traceback (most recent call last):
  File "New_.py", line 15, in <module>
java.lang.ArrayIndexOutOfBoundsException: 2
	at net.imglib2.AbstractRealLocalizable.getDoublePosition(AbstractRealLocalizable.java:100)
	at net.imglib2.view.RandomAccessibleOnRealRandomAccessible$RandomAccessOnRealRandomAccessible.getIntPosition(RandomAccessibleOnRealRandomAccessible.java:86)
	at net.imglib2.view.RandomAccessibleIntervalCursor.getIntPosition(RandomAccessibleIntervalCursor.java:209)
	at net.imglib2.AbstractWrappedLocalizable.getIntPosition(AbstractWrappedLocalizable.java:70)
	at net.imglib2.img.array.ArrayRandomAccess.setPosition(ArrayRandomAccess.java:166)
	at net.imglib2.roi.util.SamplingCursor.get(SamplingCursor.java:53)
	at net.imglib2.converter.AbstractConvertedCursor.next(AbstractConvertedCursor.java:134)
	at org.python.core.JavaIterator.__iternext__(JavaIterator.java:18)
	at org.python.pycode._pyx8.f$0(New_.py:18)
	at org.python.pycode._pyx8.call_function(New_.py)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.__builtin__.eval(__builtin__.java:497)
	at org.python.core.__builtin__.eval(__builtin__.java:501)
	at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
	at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:57)
	at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
	at org.scijava.script.ScriptModule.run(ScriptModule.java:160)
	at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
	at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:228)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: 2

	at org.python.core.Py.JavaError(Py.java:552)
	at org.python.core.PyTableCode.call(PyTableCode.java:180)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.__builtin__.eval(__builtin__.java:497)
	at org.python.core.__builtin__.eval(__builtin__.java:501)
	at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
	at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:57)
	at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
	at org.scijava.script.ScriptModule.run(ScriptModule.java:160)
	at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
	at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:228)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
	at net.imglib2.AbstractRealLocalizable.getDoublePosition(AbstractRealLocalizable.java:100)
	at net.imglib2.view.RandomAccessibleOnRealRandomAccessible$RandomAccessOnRealRandomAccessible.getIntPosition(RandomAccessibleOnRealRandomAccessible.java:86)
	at net.imglib2.view.RandomAccessibleIntervalCursor.getIntPosition(RandomAccessibleIntervalCursor.java:209)
	at net.imglib2.AbstractWrappedLocalizable.getIntPosition(AbstractWrappedLocalizable.java:70)
	at net.imglib2.img.array.ArrayRandomAccess.setPosition(ArrayRandomAccess.java:166)
	at net.imglib2.roi.util.SamplingCursor.get(SamplingCursor.java:53)
	at net.imglib2.converter.AbstractConvertedCursor.next(AbstractConvertedCursor.java:134)
	at org.python.core.JavaIterator.__iternext__(JavaIterator.java:18)
	at org.python.pycode._pyx8.f$0(New_.py:18)
	at org.python.pycode._pyx8.call_function(New_.py)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	... 17 more

Any ideas, @tpietzsch ?

The error line is the t.setOne(), which puzzles me: as if the Cursor was in the wrong place? I’ve tried with ArrayImgs.unsignedBytes and the error is the same (so it’s not a BitType-specific error).

I think I found the error: I was missing a Views.hyperSlice(img, 2, z).

1 Like