net.imagej.Dataset Java Examples

The following examples show how to use net.imagej.Dataset. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Dataset open(final Location source, final SCIFIOConfig config)
	throws IOException
{
	final ImgOpener imageOpener = new ImgOpener(getContext());
	try {
		// TODO openImgs we are only using the first image index in the
		// SCIFIOConfig.imgOpenerGetRange - so this image index corresponds to the
		// first ImgPlus in the list returned by the ImgOpener. See
		// https://github.com/scifio/scifio/issues/259

		final SCIFIOImgPlus<?> imgPlus = imageOpener.openImgs(source, config).get(
			0);

		@SuppressWarnings({ "rawtypes", "unchecked" })
		final Dataset dataset = datasetService.create((ImgPlus) imgPlus);

		final ImageMetadata imageMeta = imgPlus.getImageMetadata();
		updateDataset(dataset, imageMeta);
		return dataset;
	}
	catch (final ImgIOException exc) {
		throw new IOException(exc);
	}
}
 
Example #2
Source File: BDVSlicingDemo.java    From sciview with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void run() {
    final Dataset cube;
    try {
        File cubeFile = ResourceLoader.createFile( getClass(), "/cored_cube_var2_8bit.tif" );

        cube = datasetIO.open( cubeFile.getAbsolutePath() );
    }
    catch (IOException exc) {
        log.error( exc );
        return;
    }

    Volume v = (Volume) sciView.addVolume( cube, new float[] { 1, 1, 1 } );
    v.setPixelToWorldRatio(0.1f);// FIXME
    v.setName( "Volume Render Demo" );
    v.setDirty(true);
    v.setNeedsUpdate(true);

    List<SourceAndConverter<UnsignedByteType>> sources = (List<SourceAndConverter<UnsignedByteType>>) v.getMetadata().get("sources");

    BdvFunctions.show(sources.get(0).getSpimSource(), new BdvOptions().frameTitle("slice of " + v.getName()));

    sciView.setActiveNode(v);
    sciView.centerOnNode( sciView.getActiveNode() );
}
 
Example #3
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Metadata save(final Dataset dataset, final Location destination,
	final SCIFIOConfig config) throws IOException
{
	@SuppressWarnings("rawtypes")
	final ImgPlus img = dataset.getImgPlus();
	final Metadata metadata;
	final ImgSaver imageSaver = new ImgSaver(getContext());
	try {
		metadata = imageSaver.saveImg(destination, img, config);
	}
	catch (ImgIOException | IncompatibleTypeException exc) {
		throw new IOException(exc);
	}
	final String name = destination.getName();
	dataset.setName(name);
	dataset.setDirty(false);
	return metadata;
}
 
Example #4
Source File: FileToDatasetConverterTest.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testFileToDatasetConverter() {
	final ConvertService convertService = c.service(ConvertService.class);
	File imageFile = new File("image&pixelType=uint8&axes=X,Y,Z&lengths=256,128,32.fake");
	
	Converter<?, ?> handler = convertService.getHandler(imageFile, Dataset.class);
	// Make sure we got the right converter back
	assertSame(FileToDatasetConverter.class, handler.getClass());
	
	// Test handler capabilities
	assertTrue(handler.canConvert(imageFile, Dataset.class));
	assertFalse(handler.canConvert(null, Dataset.class));

	// Make sure we can convert with ConvertService
	assertTrue(convertService.supports(imageFile, Dataset.class));

	// Convert and check dimensions
	Dataset dataset = convertService.convert(imageFile, Dataset.class);
	assertEquals(256, dataset.dimension(0));
	assertEquals(128, dataset.dimension(1));
	assertEquals(32, dataset.dimension(2));
}
 
Example #5
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * The {@link DatasetService#create} methods make a best guess for populating
 * {@link Dataset} information. But this can be incorrect/over-aggressive,
 * e.g. in the case of RGBMerged state.
 * <p>
 * If we have access to the {@link Metadata} instance backing a
 * {@code Dataset}, we can use it to more accurately populate these settings.
 * </p>
 *
 * @param dataset Dataset instance to update.
 * @param imageMeta Metadata instance to query for updated information.
 */
private void updateDataset(final Dataset dataset,
	final ImageMetadata imageMeta)
{
	// If the original image had some level of merged channels, we should set
	// RGBmerged to true for the sake of backwards compatibility.
	// See https://github.com/imagej/imagej-legacy/issues/104

	// Look for Axes.CHANNEL in the planar axis list. If found, set RGBMerged to
	// true.
	boolean rgbMerged = false;

	for (final CalibratedAxis axis : imageMeta.getAxesPlanar()) {
		if (axis.type().equals(Axes.CHANNEL)) rgbMerged = true;
	}

	dataset.setRGBMerged(rgbMerged);
}
 
Example #6
Source File: StringToDatasetConverterTest.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testFileToDatasetConverter() {
	final ConvertService convertService = c.service(ConvertService.class);
	String imagePath = "image&pixelType=uint8&axes=X,Y,Z&lengths=256,128,32.fake";
	
	Converter<?, ?> handler = convertService.getHandler(imagePath, Dataset.class);
	// Make sure we got the right converter back
	assertSame(StringToDatasetConverter.class, handler.getClass());
	
	// Test handler capabilities
	assertTrue(handler.canConvert(imagePath, Dataset.class));
	assertFalse(handler.canConvert(null, Dataset.class));

	// Make sure we can convert with ConvertService
	assertTrue(convertService.supports(imagePath, Dataset.class));

	// Convert and check dimensions
	Dataset dataset = convertService.convert(imagePath, Dataset.class);
	assertEquals(256, dataset.dimension(0));
	assertEquals(128, dataset.dimension(1));
	assertEquals(32, dataset.dimension(2));
}
 
Example #7
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Dataset open(final Location source) throws IOException {
	final SCIFIOConfig config = new SCIFIOConfig();
	config.imgOpenerSetIndex(0);
	// skip min/max computation
	config.imgOpenerSetComputeMinMax(false);
	// prefer planar array structure, for ImageJ1 and ImgSaver compatibility
	config.imgOpenerSetImgModes(ImgMode.PLANAR);
	return open(source, config);
}
 
Example #8
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public List<net.imagej.Dataset> openAll(final Location source)
	throws IOException
{
	final SCIFIOConfig config = new SCIFIOConfig();
	config.imgOpenerSetImgModes(ImgMode.PLANAR);
	return openAll(source, config);
}
 
Example #9
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public List<net.imagej.Dataset> openAll(final Location source,
	final SCIFIOConfig config) throws IOException
{
	final ArrayList<Dataset> datasetList = new ArrayList<>();

	final ImgOpener imageOpener = new ImgOpener(getContext());
	try {
		final List<SCIFIOImgPlus<?>> openImgs = imageOpener.openImgs(source,
			config);
		for (int imgId = 0; imgId != openImgs.size(); imgId++) {

			final SCIFIOImgPlus<?> imgPlus = openImgs.get(imgId);

			@SuppressWarnings({ "rawtypes", "unchecked" })
			final Dataset dataset = datasetService.create((ImgPlus) imgPlus);

			final ImageMetadata imageMeta = imgPlus.getImageMetadata();
			updateDataset(dataset, imageMeta);
			datasetList.add(dataset);
		}

	}
	catch (final ImgIOException exc) {
		throw new IOException(exc);
	}
	return datasetList;
}
 
Example #10
Source File: FileToDatasetConverter.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T convert(final Object src, final Class<T> dest) {
	final File file = (File) src;
	Dataset data = null;
	try {
		data = io.open(file.getAbsolutePath());
	} catch (IOException e) {
		log.error("Error loading file", e);
	}
	return (T) data;
}
 
Example #11
Source File: SaveAsImage.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void run() {
	final Dataset dataset = dataset();
	try {
		datasetIOService.save(dataset, new FileLocation(outputFile.getAbsolutePath()));
	}
	catch (final IOException exc) {
		log.error(exc);
		uiService.showDialog(exc.getMessage(), "Error Saving Image",
			DialogPrompt.MessageType.ERROR_MESSAGE);
		return;
	}

	display.setName(dataset.getName());
}
 
Example #12
Source File: SciView.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Add a Dataset to the scene as a volume. Voxel resolution and name are extracted from the Dataset itself
 * @param image
 * @return a Node corresponding to the Volume
 */
public Node addVolume( Dataset image ) {

    float[] voxelDims = new float[image.numDimensions()];
    for( int d = 0; d < voxelDims.length; d++ ) {
        double inValue = image.axis(d).averageScale(0, 1);
        if( image.axis(d).unit() == null )
            voxelDims[d] = (float) inValue;
        else
            voxelDims[d] = (float) unitService.value( inValue, image.axis(d).unit(), axis(d).unit() );
    }

    return addVolume( image, voxelDims );
}
 
Example #13
Source File: VolumeRenderDemo.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void run() {
    final Dataset cube;
    try {
        File cubeFile = ResourceLoader.createFile( getClass(), "/cored_cube_var2_8bit.tif" );

        cube = datasetIO.open( cubeFile.getAbsolutePath() );
    }
    catch (IOException exc) {
        log.error( exc );
        return;
    }

    Volume v = (Volume) sciView.addVolume( cube, new float[] { 1, 1, 1 } );
    v.setPixelToWorldRatio(0.05f);
    v.setName( "Volume Render Demo" );
    v.setDirty(true);
    v.setNeedsUpdate(true);

    if (iso) {
        int isoLevel = 1;

        @SuppressWarnings("unchecked")
        Img<UnsignedByteType> cubeImg = ( Img<UnsignedByteType> ) cube.getImgPlus().getImg();

        Img<BitType> bitImg = ( Img<BitType> ) ops.threshold().apply( cubeImg, new UnsignedByteType( isoLevel ) );

        Mesh m = ops.geom().marchingCubes( bitImg, isoLevel, new BitTypeVertexInterpolator() );

        graphics.scenery.Mesh isoSurfaceMesh = MeshConverter.toScenery(m,false);
        v.addChild(isoSurfaceMesh);

        isoSurfaceMesh.setName( "Volume Render Demo Isosurface" );
    }

    sciView.setActiveNode(v);
    sciView.centerOnNode( sciView.getActiveNode() );
}
 
Example #14
Source File: DatasetToPNGNotebookConverter.java    From scijava-jupyter-kernel with Apache License 2.0 5 votes vote down vote up
@Override
public PNGImageNotebookOutput convert(Object object) {

    Dataset source = (Dataset) object;

    String base64Image = (String) ijnb.RAIToPNG((Img) source, //
            source.dimensionIndex(Axes.X),
            source.dimensionIndex(Axes.Y),
            source.dimensionIndex(Axes.CHANNEL),
            ImageJNotebookService.ValueScaling.AUTO);

    return new PNGImageNotebookOutput(base64Image);
}
 
Example #15
Source File: DatasetIOPlugin.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Dataset open(final String source) throws IOException {
	return datasetIOService.open(resolve(source, "source"));
}
 
Example #16
Source File: DatasetIOPlugin.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Class<Dataset> getDataType() {
	return Dataset.class;
}
 
Example #17
Source File: DatasetIOPlugin.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void save(final Dataset dataset, final String destination)
	throws IOException
{
	datasetIOService.save(dataset, resolve(destination, "destination"));
}
 
Example #18
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Metadata save(final Dataset dataset, final String destination,
	final SCIFIOConfig config) throws IOException
{
	return save(dataset, resolve(destination), config);
}
 
Example #19
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Metadata save(final Dataset dataset, final Location destination)
	throws IOException
{
	return save(dataset, destination, new SCIFIOConfig());
}
 
Example #20
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Metadata save(final Dataset dataset, final String destination)
	throws IOException
{
	return save(dataset, resolve(destination), new SCIFIOConfig());
}
 
Example #21
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public List<Dataset> openAll(final String source, final SCIFIOConfig config)
	throws IOException
{
	return openAll(resolve(source), config);
}
 
Example #22
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public List<Dataset> openAll(final String source) throws IOException {
	return openAll(resolve(source));
}
 
Example #23
Source File: DatasetToPNGNotebookConverter.java    From scijava-jupyter-kernel with Apache License 2.0 4 votes vote down vote up
@Override
public Class<Dataset> getInputType() {
    return Dataset.class;
}
 
Example #24
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Dataset open(final String source, final SCIFIOConfig config)
	throws IOException
{
	return open(resolve(source), config);
}
 
Example #25
Source File: DefaultDatasetIOService.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Dataset open(final String source) throws IOException {
	return open(resolve(source));
}
 
Example #26
Source File: ObjectsResourceTest.java    From imagej-server with Apache License 2.0 4 votes vote down vote up
/**
 * A integrated test for the workflow using IOResource:<br/>
 * <li>upload file</li>
 * <li>get IDs</li>
 * <li>get ID</li>
 * <li>remove ID</li>
 * <li>retrieve file</li>
 */
@Test
public void ioResource() {
	try {
		// Test upload image
		final String imgID = uploadFile("imgs/about4.tif");
		assertTrue(objectService.contains(imgID));

		// Test upload table
		final String tableID = uploadFile("texts/table.csv");
		assertTrue(objectService.contains(tableID));

		// Test getIDs
		final String secondImg = uploadFile("imgs/about4.tif");
		assertTrue(objectService.contains(secondImg));
		assertTrue(!imgID.equals(secondImg));
		final List<String> ids = Arrays.asList(getIDs());
		assertTrue(ids.contains(imgID));
		assertTrue(ids.contains(secondImg));
		assertTrue(ids.contains(tableID));

		// Test getID
		assertEquals(getObject(imgID).getStatusInfo(), Status.OK);
		assertEquals(getObject(secondImg).getStatusInfo(), Status.OK);
		assertEquals(getObject(tableID).getStatusInfo(), Status.OK);

		// Test removeID
		assertEquals(Status.OK, removeID(secondImg).getStatusInfo());
		assertEquals(Status.NOT_FOUND, retrieveFile(secondImg, "fmt")
			.getStatusInfo());

		// Test retrieve image
		final File downloaded = retrieveFile(imgID, "tiff").readEntity(
			File.class);
		final Dataset ds = ctx.service(DatasetIOService.class).open(downloaded
			.getAbsolutePath());
		final Iterator<?> expectedItr = ((Iterable<?>) objectService.find(imgID)
			.getObject()).iterator();
		final Iterator<?> actualItr = ds.iterator();
		while (expectedItr.hasNext()) {
			assertTrue(actualItr.hasNext());
			assertEquals(expectedItr.next(), actualItr.next());
		}
		assertTrue(!actualItr.hasNext());

		// Test retrieve table
		final File downloadTable = retrieveFile(tableID, "csv").readEntity(
			File.class);
		final String secondTable = uploadFile("secondTable.csv",
			new FileInputStream(downloadTable));
		assertEquals(objectService.find(tableID).getObject(), objectService.find(
			secondTable).getObject());
	}
	catch (IOException exc) {
		fail(exc.getMessage());
	}
}
 
Example #27
Source File: SaveAsImage.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected void initOutputFile() {
	final Dataset dataset = dataset();
	if (dataset == null) return;
	outputFile = new File(dataset.getSource());
}
 
Example #28
Source File: SaveAsImage.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private Dataset dataset() {
	return imageDisplayService.getActiveDataset(display);
}
 
Example #29
Source File: SaveImage.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setDataset(final Dataset d) {
	dataset = d;
}
 
Example #30
Source File: SaveImage.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Dataset getDataset() {
	return dataset;
}