scifio ImgOpener and ImgSaver

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

scifio ImgOpener and ImgSaver

Daniel Matthews
Hi

I am hoping to get some help with a very simple processing task that I want to run from using Fiji in headless mode.

I have an OME-TIFF which I have used some custom code to write a polygon ROI to the OME-XML. I want to get the ROI from the metadata, do a simple "clear outside" and then save the result as an OME-TIFF with the same metadata as the input image.

Initially I tried using Bioformats in a Jython script to open the image as an ImagePlus and to parse the OME-XML. I use the associated ImageProcessor object to clear outside and since I ran into problems using the Bioformats exporter, I used SCIFIO to wrap the resultant ImagePlus object which I then tried to save using ImgSaver. The trouble is I get the following error:

[ERROR] Cannot create plugin: class='net.imagej.legacy.LegacyImageJApp', name='ImageJ1', priority=0.0, enabled=true, pluginType=App
java.lang.IllegalArgumentException: Required service is missing: net.imagej.legacy.DefaultLegacyService

And a warning:

[WARNING] Expected positive value for PhysicalSizeX; got 0.0
[WARNING] Expected positive value for PhysicalSizeY; got 0.0

I also tried to use the ImgOpener class thinking that I could use the resultant imglib2 object to run the processing and then use ImgSaver to write back to OME-TIFF. When I use the ImgOpener I get the same java error as noted above. Any advice would be gratefully received!

Cheers,

Dan.

P.S. My script, as it stands, is as follows:


import sys
from ij import IJ as ij
from ij.plugin.frame import RoiManager
from ij.gui import Roi, PolygonRoi

from loci.plugins import BF
from loci.common import Region
from loci.plugins.in import ImporterOptions
from loci.formats import ImageReader
from loci.formats import MetadataTools
from ome.xml.meta import OMEXMLMetadata

from io.scif import Reader, SCIFIO
from io.scif.ome import OMEMetadata
from io.scif.img import ImgOpener, ImgSaver
from net.imglib2.img import ImagePlusAdapter

file = "/home/daniel/Desktop/roi_test.ome.tif"

options = ImporterOptions()
options.setId(file)
imps = BF.openImagePlus(options)

reader = ImageReader()
omeMeta = MetadataTools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.setId(file)
reader.close()
roiCount = omeMeta.getROICount()

if roiCount > 1:
    sys.exit(0)

omeMetaStr =  omeMeta.dumpXML()
shape = omeMeta.getShapeType(0,0)

scifio = SCIFIO()
meta = scifio.initializer().parseMetadata(file)
omexml = OMEMetadata(scifio.getContext())

if 'Polygon' not in shape:
    sys.exit(0)

prefix = omeMetaStr.index(shape)

stop = omeMetaStr.find('/',prefix,-1) - 1

start = len(shape + " " + "points=") + 1
pts = omeMetaStr[start+prefix:stop]

new_pts_str =pts.replace(" ",",")
new_pts = [int(p) for p in new_pts_str.split(",")]

xs = new_pts[0::2]
ys = new_pts[1::2]

proi = PolygonRoi(xs, ys, len(xs), Roi.POLYGON)  
for i,imp in enumerate(imps):
    imp.setRoi(proi)

    # get the image processor
    ip = imp.getProcessor()

    # fill outside
    ip.fillOutside(proi)
    ip.convertToFloat()
    img = ImagePlusAdapter.wrap(imp)
    saver = ImgSaver()
    saver.saveImg("/home/daniel/Desktop/cleared.ome.tif",img)
Reply | Threaded
Open this post in threaded view
|

Re: scifio ImgOpener and ImgSaver

Mark Hiner
Hi Daniel,

>[ERROR] Cannot create plugin: class='net.imagej.legacy.
>LegacyImageJApp',
>name='ImageJ1', priority=0.0, enabled=true, pluginType=App
>java.lang.IllegalArgumentException: Required service is missing:
>net.imagej.legacy.DefaultLegacyService

When you use these constructors:
>scifio = SCIFIO()
>saver = ImgSaver()
you create a new SciJava Context, which is clashing with Fiji's
pre-existing Context. It would be best to use script parameters[1] to get
the DatasetIOService[2] which provides higher-level I/O API. e.g.:

# @DatasetIOService dios

and you can then use the dios to read and write your images.

However, fixing this error won't actually get your script to do what you
want because SCIFIO does not yet support ROIs.

What I would suggest instead is using the Bio-Formats ROIHandler to write
ROIs to your MetadataStore[3] and then save the ImagePlus with Bio-Formats.

If you have any questions about using the Bio-Formats API in this way, the
best mailing list to use is OME-Devel[4].

Best,
Mark

[1] http://imagej.net/Script_parameters
[2]
https://github.com/scifio/scifio/blob/scifio-0.21.0/src/main/java/io/scif/services/DatasetIOService.java
[3]
https://github.com/openmicroscopy/bioformats/blob/v5.0.8/components/bio-formats-plugins/src/loci/plugins/util/ROIHandler.java#L161-194
[4] http://lists.openmicroscopy.org.uk/mailman/listinfo/ome-devel/

On Sun, Feb 22, 2015 at 7:41 PM, Daniel Matthews <[hidden email]>
wrote:

> Hi
>
> I am hoping to get some help with a very simple processing task that I want
> to run from using Fiji in headless mode.
>
> I have an OME-TIFF which I have used some custom code to write a polygon
> ROI
> to the OME-XML. I want to get the ROI from the metadata, do a simple "clear
> outside" and then save the result as an OME-TIFF with the same metadata as
> the input image.
>
> Initially I tried using Bioformats in a Jython script to open the image as
> an ImagePlus and to parse the OME-XML. I use the associated ImageProcessor
> object to clear outside and since I ran into problems using the Bioformats
> exporter, I used SCIFIO to wrap the resultant ImagePlus object which I then
> tried to save using ImgSaver. The trouble is I get the following error:
>
> [ERROR] Cannot create plugin: class='net.imagej.legacy.LegacyImageJApp',
> name='ImageJ1', priority=0.0, enabled=true, pluginType=App
> java.lang.IllegalArgumentException: Required service is missing:
> net.imagej.legacy.DefaultLegacyService
>
> And a warning:
>
> [WARNING] Expected positive value for PhysicalSizeX; got 0.0
> [WARNING] Expected positive value for PhysicalSizeY; got 0.0
>
> I also tried to use the ImgOpener class thinking that I could use the
> resultant imglib2 object to run the processing and then use ImgSaver to
> write back to OME-TIFF. When I use the ImgOpener I get the same java error
> as noted above. Any advice would be gratefully received!
>
> Cheers,
>
> Dan.
>
> P.S. My script, as it stands, is as follows:
>
>
> import sys
> from ij import IJ as ij
> from ij.plugin.frame import RoiManager
> from ij.gui import Roi, PolygonRoi
>
> from loci.plugins import BF
> from loci.common import Region
> from loci.plugins.in import ImporterOptions
> from loci.formats import ImageReader
> from loci.formats import MetadataTools
> from ome.xml.meta import OMEXMLMetadata
>
> from io.scif import Reader, SCIFIO
> from io.scif.ome import OMEMetadata
> from io.scif.img import ImgOpener, ImgSaver
> from net.imglib2.img import ImagePlusAdapter
>
> file = "/home/daniel/Desktop/roi_test.ome.tif"
>
> options = ImporterOptions()
> options.setId(file)
> imps = BF.openImagePlus(options)
>
> reader = ImageReader()
> omeMeta = MetadataTools.createOMEXMLMetadata()
> reader.setMetadataStore(omeMeta)
> reader.setId(file)
> reader.close()
> roiCount = omeMeta.getROICount()
>
> if roiCount > 1:
>     sys.exit(0)
>
> omeMetaStr =  omeMeta.dumpXML()
> shape = omeMeta.getShapeType(0,0)
>
> scifio = SCIFIO()
> meta = scifio.initializer().parseMetadata(file)
> omexml = OMEMetadata(scifio.getContext())
>
> if 'Polygon' not in shape:
>     sys.exit(0)
>
> prefix = omeMetaStr.index(shape)
>
> stop = omeMetaStr.find('/',prefix,-1)   - 1
>
> start = len(shape + " " + "points=") + 1
> pts = omeMetaStr[start+prefix:stop]
>
> new_pts_str =pts.replace(" ",",")
> new_pts = [int(p) for p in new_pts_str.split(",")]
>
> xs = new_pts[0::2]
> ys = new_pts[1::2]
>
> proi = PolygonRoi(xs, ys, len(xs), Roi.POLYGON)
> for i,imp in enumerate(imps):
>     imp.setRoi(proi)
>
>     # get the image processor
>     ip = imp.getProcessor()
>
>     # fill outside
>     ip.fillOutside(proi)
>     ip.convertToFloat()
>     img = ImagePlusAdapter.wrap(imp)
>     saver = ImgSaver()
>     saver.saveImg("/home/daniel/Desktop/cleared.ome.tif",img)
>
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/scifio-ImgOpener-and-ImgSaver-tp5011721.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html