Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting a Dataset to ImagePlus implicitly shows it #134

Open
ctrueden opened this issue Feb 26, 2016 · 0 comments
Open

Converting a Dataset to ImagePlus implicitly shows it #134

ctrueden opened this issue Feb 26, 2016 · 0 comments
Assignees

Comments

@ctrueden
Copy link
Member

The ConvertService can be used to convert a Dataset to an ImagePlus, which is handy for use with IJ1. However, the internal linkage between these types is actually between ImageDisplay and ImagePlus; when the dataset is registered, an ImageDisplay is implicitly created using DisplayService.createDisplay, which publishes DisplayCreatedEvent, adding the ImageDisplay to the object index, which causes it to be later shown when UIService.showUI() is called.

The proper fix is unclear without further investigation. Perhaps the LegacyImageMap could call createDisplayQuietly instead of createDisplay, although that might cause other problems. When the ImageJ2 data model is reworked, this issue might become moot.

Here is an example (from @rimadoma) reproducing the issue:

package protoOps.datasetExample;

import ij.ImagePlus;
import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.axis.Axes;
import net.imagej.axis.AxisType;
import net.imagej.ops.Op;
import net.imagej.ops.OpEnvironment;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import org.scijava.convert.ConvertService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

import java.util.stream.IntStream;

/**
 * A (M)inimal, (C)omplete, and (V)erifiable (E)xample of an issue I've encountered with ConvertService.
 *
 * A call to ConvertService.convert(dataset, ImagePlus.class) causes an image window to pop up,
 * when the ImageJ UI is opened (line 77).
 *
 * The image turns red if you move the slider from slide 1.
 */

@Plugin(type = Op.class, name = "datasetWrapping")
public class MVCEDatasetWrapping extends AbstractOp {
    private ImagePlus imagePlus;

    @Parameter
    private ConvertService convertService;

    @Parameter
    private Dataset dataset;

    @Override
    public void run() {
        convertDataset();
    }

    public static void main(final String... args) throws Exception {
        int width = 320, height = 200, depth = 10;
        final ImageJ ij = new ImageJ();
        final Dataset inputDataset = createMonochromeDataset(ij ,width, height, depth, 0xFF);
        ij.op().run("datasetWrapping", inputDataset);
        ij.ui().showUI();
    }

    private static Dataset createMonochromeDataset(final ImageJ ijInstance, final int width, final int height,
                                                   final int depth, final int color) {
        final long[] dims = {width, height, depth};
        final AxisType[] axisTypes = {Axes.X, Axes.Y, Axes.Z};
        Dataset dataset = ijInstance.dataset().create(new UnsignedByteType(), dims, "Test image", axisTypes);

        IntStream.range(0, depth).forEach(i -> {
            final byte[] data = new byte[width * height];
            IntStream.range(0, data.length).forEach(j -> data[j] = (byte)color);
            dataset.setPlane(i, data);
        });

        return dataset;
    }

    private void convertDataset() {
        imagePlus = convertService.convert(dataset, ImagePlus.class);
    }
}
@ctrueden ctrueden changed the title Converting a Dataset to ImagePlus implicitly creates an ImageDisplay Converting a Dataset to ImagePlus implicitly creates an ImageDisplay, then shows it Feb 26, 2016
@ctrueden ctrueden changed the title Converting a Dataset to ImagePlus implicitly creates an ImageDisplay, then shows it Converting a Dataset to ImagePlus implicitly shows it Feb 26, 2016
ctrueden added a commit to ctrueden/sandbox that referenced this issue Aug 23, 2016
@maarzt maarzt self-assigned this Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants