Problem removing last slice of an ImageStack while displaying last slice

ImageJ 1.53c, Windows 10

I’m not sure whether I’m not doing the slice deletion properly or whether it is a small bug.

Run the following jython code:

from ij import IJ

IJ.run('MRI Stack') # has 27 slices
im = IJ.getImage()

# set stack to the last slice
im.setSlice(im.getNSlices())

# delete last slice
im.getImageStack().deleteLastSlice()

# update (I do not know which one I should be calling)
im.updateAndRepaintWindow()
im.updateImage()
im.updateAndDraw()
im.repaintWindow()

you should see

There are two issues:

  1. the slice displayed is the last one (27) that should have been deleted
  2. the slice number at the top (26/26) does not match the bottom slider

From there

  • if you do a mousewheel scroll down
    – the slice image is updated to the correct new last slice (slice 26)
    – the last slice (27) correctly disappears
    – the slider is still wrong. Only after wheel scrolling back and forth it gets back to normal
  • if you do a mousewheel scroll up, or click on either arrow of the stack slider
    – the last slice (27) is still present while it should have been deleted
    – the second last slice (26) has been deleted instead

If you comment out displaying the last slice

# im.setSlice(im.getNSlices()-1)

there is no issue (or it resolves upon scrolling when you go check the end of the stack).


If you set and then delete the second last slice:

from ij import IJ

a = IJ.run('MRI Stack')
im = IJ.getImage()

# set stack to the second last slice
im.setSlice(im.getNSlices()-1)

# delete second last slice
im.getImageStack().deleteSlice(im.getNSlices()-1)

# update (I do not know which one I should be calling)
im.updateAndRepaintWindow()
im.updateImage()
im.updateAndDraw()
im.repaintWindow()

you should see

Only one issue: the bottom slider is wrong. There are no issues with deletion of the wrong slice regardless of scrolling up/down or using the slider arrows.

Any idea what is happening?
Thanks

In the ImageJ-macro language it’s a one-liner:

run("Make Substack...", "slices="+nSlices+"-"+nSlices+" delete");

I see no problem with the slider.

Thanks @notQRV for the hint, I should have indeed looked at how other standard plugins proceed to remove slices.

SubstackMaker is doing a

imp.setStack(stack);

which solves the issue.

SubstackMaker also uses a

swin.updateSliceSelector()

which solves the slider update, but this alone does not solve the wrong slice being deleted.

I’m not sure whether I’m not doing the slice deletion properly or whether it is a small bug.

So it was probably the latter, here the minimal working jython code:

from ij import IJ

IJ.run('MRI Stack') # has 27 slices
im = IJ.getImage()

# set stack to the last slice (not needed, just for the purpose of this post)
im.setSlice(im.getNSlices())

# delete last slice
im.getStack().deleteLastSlice()
im.setStack(im.getStack())

Actually this is confirmed in the description of getStack:

After adding or removing slices, call setStack() to update the image and the window that is displaying it.

1 Like