bigdataviewer / bigdataviewer-core

ImgLib2-based viewer for registered SPIM stacks and more
BSD 2-Clause "Simplified" License
33 stars 35 forks source link

2D support in TransformedBoxSelectionDialog #145

Open karlduderstadt opened 1 year ago

karlduderstadt commented 1 year ago

The TransformedBoxSelectionDialog doesn't support 2D.

If TransformedBoxSelectionDialog is provided with 2D intervals an ArrayIndexOutOfBoundsException is thrown. image

This can be reproduced in Fiji using the following groovy script:

import java.util.Random;
import bdv.util.*;

import net.imglib2.Interval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;

import bdv.tools.boundingbox.BoxSelectionOptions;
import bdv.tools.boundingbox.TransformedBoxSelectionDialog;

Random random = new Random();

Img< UnsignedByteType > img = ArrayImgs.unsignedBytes( 100, 100, 10 );
img.forEach( t -> t.set( random.nextInt( 128 ) ) );

AffineTransform3D imageTransform = new AffineTransform3D();
imageTransform.set( 2, 2, 2 );
def bdv = BdvFunctions.show( img, "image", BdvOptions.options().sourceTransform( imageTransform ).is2D() );

Interval initialInterval = Intervals.createMinMax( 30, 30, 80, 80 );
Interval rangeInterval = Intervals.createMinMax( 0, 0, 100, 100 );
TransformedBoxSelectionDialog.Result result = BdvFunctions.selectBox(
        bdv,
        imageTransform,
        initialInterval,
        rangeInterval,
        BoxSelectionOptions.options()
                .title( "Select box to fill" )
                .selectTimepointRange()
                .initialTimepointRange( 0, 5 ) );

if ( result.isValid() )
{
    for ( int tp = result.getMinTimepoint(); tp <= result.getMaxTimepoint(); ++tp )
        Views.interval( Views.extendZero( Views.hyperSlice( img, 3, tp ) ), result.getInterval() ).forEach( t -> t.set( 255 ) );
    bdv.getBdvHandle().getViewerPanel().requestRepaint();
}

However, it is possible to use the TransformedBoxSelectionDialog with the BDV in 2D mode as long as the intervals provided are 3D with the third dimension set to zero. The following groovy script works:

import java.util.Random;
import bdv.util.*;

import net.imglib2.Interval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;

import bdv.tools.boundingbox.BoxSelectionOptions;
import bdv.tools.boundingbox.TransformedBoxSelectionDialog;

Random random = new Random();

Img< UnsignedByteType > img = ArrayImgs.unsignedBytes( 100, 100, 10 );
img.forEach( t -> t.set( random.nextInt( 128 ) ) );

AffineTransform3D imageTransform = new AffineTransform3D();
imageTransform.set( 2, 2, 2 );
def bdv = BdvFunctions.show( img, "image", BdvOptions.options().sourceTransform( imageTransform ).is2D() );

final Interval initialInterval = Intervals.createMinMax( 0, 0, 0, 40, 40, 0 );
final Interval rangeInterval = Intervals.createMinMax( 0, 0, 0, 100, 100, 0 );
TransformedBoxSelectionDialog.Result result = BdvFunctions.selectBox(
        bdv,
        imageTransform,
        initialInterval,
        rangeInterval,
        BoxSelectionOptions.options()
                .title( "Select box to fill" )
                .selectTimepointRange()
                .initialTimepointRange( 0, 5 ) );

if ( result.isValid() )
{
    for ( int tp = result.getMinTimepoint(); tp <= result.getMaxTimepoint(); ++tp )
        Views.interval( Views.extendZero( Views.hyperSlice( img, 2, tp ) ), result.getInterval() ).forEach( t -> t.set( 255 ) );
    bdv.getBdvHandle().getViewerPanel().requestRepaint();
}

This is is a suitable workaround for the problem. However, the dialog then shows the z scrollbars and when zooming in closely both the green box on top and bottom below are visible. So a 3D box is kind of still drawn.

It would be nice if 2D intervals could be provided to the TransformedBoxSelectionDialog and the Dialog then showed up without the z scrollbars. It would also make sense to only show the green box in this situation and not have the background box outline. This part is more a minor comment. I think removing the z scrollbar would resolve the issue.

imagesc-bot commented 1 year ago

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/real-box-selection-dialogs-in-bigdataviewer/22650/9