scifio ImgOpener and ImgSaver

Posted by Daniel Matthews on
URL: http://imagej.273.s1.nabble.com/scifio-ImgOpener-and-ImgSaver-tp5011721.html

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)