<div dir="ltr"><div><div><div><div><div><div>Hi Hadrien,<br><br></div>I've identified at least two bugs that could be affecting you here.<br><br></div>First of all, there was a bug in the OME-TIFF format writing out multi-dimensional images. That's fixed on a branch but not uploaded to Fiji yet [1].<br><br></div>Second, it seems like cropping is restricting pixel reads to the correct region but then doesn't change the image size.<br><br></div>I'm hoping when I fix the second issue writing cropped regions will be fixed.<br><br></div>Thanks for the great feedback!<br></div>- Mark<br><div><div><div><div><br>[1] <a href="https://github.com/scifio/scifio-ome-xml/tree/fix-writing">https://github.com/scifio/scifio-ome-xml/tree/fix-writing</a><br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 9, 2015 at 11:49 AM, Hadrien Mary <span dir="ltr"><<a href="mailto:hadrien.mary@gmail.com" target="_blank">hadrien.mary@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I also confirm the same behaviour when replacing original file by OME<br>
sample data : <a href="http://www.openmicroscopy.org/Schemas/Samples/2013-06/bioformats-artificial/multi-channel-4D-series.ome.tif.zip" target="_blank">http://www.openmicroscopy.org/Schemas/Samples/2013-06/bioformats-artificial/multi-channel-4D-series.ome.tif.zip</a>.<br>
<span class="im HOEnZb"><br>
--<br>
Hadrien Mary<br>
<br>
Ph.D student in Biology<br>
Tournier-Gachet Team<br>
CNRS - LBCMCP - UMR 5088<br>
<br>
Université de Toulouse - Bât. 4R3B1<br>
118, route de Narbonne - 31062 Toulouse<br>
<br>
<br>
</span><div class="HOEnZb"><div class="h5">On Fri, Jan 9, 2015 at 6:46 PM, Hadrien Mary <<a href="mailto:hadrien.mary@gmail.com">hadrien.mary@gmail.com</a>> wrote:<br>
> After some investigations I think there is a bug with ImgSaver. In the<br>
> following script, X, Y crop is fine but axes informations (T and Z)<br>
> are lost and all frames appears on the same axes. Now if I replace<br>
> "target = "/home/hadim/cropped.tif" by "target =<br>
> "/home/hadim/cropped.ome.tif" on line 9. X, Y crop fails, cropped<br>
> image has the same dimensions than original however axes informations<br>
> (T and Z) are ok. Note that in cropped.ome.tif, pixel values are<br>
> totally incoherent which is not the case in cropped.tif.<br>
><br>
> Files can be found here :<br>
> <a href="https://arwen.hadim.fr/public.php?service=files&t=71aa17336dcccb3437d9a3dff789d2c0" target="_blank">https://arwen.hadim.fr/public.php?service=files&t=71aa17336dcccb3437d9a3dff789d2c0</a><br>
><br>
> Archive contains cropped.tif, cropped.ome.tif and original.ome.tif.<br>
><br>
> The script:<br>
><br>
> ---------------------------------------------------<br>
> from io.scif import SCIFIO<br>
> from io.scif.config import SCIFIOConfig<br>
> from io.scif.img import ImageRegion<br>
> from io.scif.img import ImgOpener<br>
> from io.scif.img import ImgSaver<br>
> from net.imglib2.meta import Axes<br>
><br>
> fname = "/home/hadim/original.ome.tif"<br>
> target = "/home/hadim/cropped.tif"<br>
><br>
> axes = [Axes.X, Axes.Y]<br>
> ranges = ["%i-%i" % (0, 15), "%i-%i" % (0, 25)]<br>
> config = SCIFIOConfig()<br>
> config.imgOpenerSetRegion(ImageRegion(axes, ranges))<br>
><br>
> opener = ImgOpener()<br>
> imps = opener.openImgs(fname, config)<br>
> imp = imps[0]<br>
><br>
> saver = ImgSaver()<br>
> saver.saveImg(target, imp)<br>
><br>
> print('Done')<br>
> ---------------------------------------------------<br>
><br>
><br>
> --<br>
> Hadrien Mary<br>
><br>
><br>
> On Fri, Jan 9, 2015 at 12:25 AM, Hadrien Mary <<a href="mailto:hadrien.mary@gmail.com">hadrien.mary@gmail.com</a>> wrote:<br>
>> Thanks for the tip (reminder to me: always look for example in tests!).<br>
>><br>
>> Unfortunately crop does not work... I will try to do more tests<br>
>> tomorrow or wait for you or someone else to have a look.<br>
>><br>
>> Updated code:<br>
>><br>
>> from ij import IJ<br>
>> from ij import ImagePlus<br>
>><br>
>> from io.scif import SCIFIO<br>
>> from io.scif.config import SCIFIOConfig<br>
>> from io.scif.img import ImageRegion<br>
>> from io.scif.img import ImgOpener<br>
>> from io.scif.img import ImgSaver<br>
>> from net.imglib2.meta import Axes<br>
>><br>
>> fname = "/home/hadim/original.ome.tif"<br>
>> target = "/home/hadim/cropped.ome.tif"<br>
>><br>
>> axes = [Axes.X, Axes.Y]<br>
>> ranges = ["%i-%i" % (2, 15), "%i-%i" % (2, 25)]<br>
>> config = SCIFIOConfig()<br>
>> config.imgOpenerSetRegion(ImageRegion(axes, ranges))<br>
>><br>
>> opener = ImgOpener()<br>
>> imps = opener.openImgs(fname, config)<br>
>> imp = imps[0]<br>
>><br>
>> saver = ImgSaver()<br>
>> saver.saveImg(target, imp)<br>
>><br>
>> print('Done')<br>
>><br>
>> --<br>
>> Hadrien Mary<br>
>><br>
>><br>
>> On Fri, Jan 9, 2015 at 12:09 AM, Curtis Rueden <<a href="mailto:ctrueden@wisc.edu">ctrueden@wisc.edu</a>> wrote:<br>
>>> Hi Hadrien,<br>
>>><br>
>>>> I would like to be able to crop a region in X and Y while<br>
>>>> keeping any other dimensions which could exist (C, Z, T).<br>
>>><br>
>>> Here's some Java:<br>
>>><br>
>>>     int minX = 128, maxX = 255, minY = 128, minY = 255;<br>
>>>     AxisType[] axes = { Axes.X, Axes.Y };<br>
>>>     String[] ranges = { minX + "-" + maxX, minY + "-" + maxY };<br>
>>>     config.imgOpenerSetRegion(new ImageRegion(axes, ranges));<br>
>>><br>
>>> Adapted from here:<br>
>>> <a href="https://github.com/scifio/scifio/blob/scifio-0.18.0/src/test/java/io/scif/img/utests/ImgOpenerTest.java#L189-L191" target="_blank">https://github.com/scifio/scifio/blob/scifio-0.18.0/src/test/java/io/scif/img/utests/ImgOpenerTest.java#L189-L191</a><br>
>>><br>
>>> And untested.<br>
>>><br>
>>> HTH,<br>
>>> Curtis<br>
>>><br>
>>> On Thu, Jan 8, 2015 at 4:47 PM, Hadrien Mary <<a href="mailto:hadrien.mary@gmail.com">hadrien.mary@gmail.com</a>> wrote:<br>
>>>><br>
>>>> After some investigations, I'm pretty sure I don't instanciate<br>
>>>> ImageRegion correctly. Doc and source code didn't help me... I would<br>
>>>> like to be able to crop a region in X and Y while keeping any other<br>
>>>> dimensions which could exist (C, Z, T).<br>
>>>><br>
>>>> --<br>
>>>> Hadrien Mary<br>
>>>><br>
>>>><br>
>>>><br>
>>>> On Thu, Jan 8, 2015 at 10:22 PM, Hadrien Mary <<a href="mailto:hadrien.mary@gmail.com">hadrien.mary@gmail.com</a>><br>
>>>> wrote:<br>
>>>> > Thank you Curtis for the answer.<br>
>>>> ><br>
>>>> > I tried to apply what you told me and the code now works without<br>
>>>> > error. However the saved cropped image is not cropped (same size as<br>
>>>> > original) and pixel values are modified).<br>
>>>> ><br>
>>>> > Script:<br>
>>>> ><br>
>>>> > from io.scif.config import SCIFIOConfig<br>
>>>> > from io.scif.img import ImageRegion<br>
>>>> > from io.scif.img import ImgOpener<br>
>>>> > from io.scif.img import ImgSaver<br>
>>>> ><br>
>>>> > fname = "/home/hadim/original.ome.tif"<br>
>>>> > target = "/home/hadim/cropped.ome.tif"<br>
>>>> ><br>
>>>> > config = SCIFIOConfig()<br>
>>>> > region = ImageRegion(dict(x=2, y=2, width=10, height=10))<br>
>>>> > config.imgOpenerSetRegion(region)<br>
>>>> ><br>
>>>> > opener = ImgOpener()<br>
>>>> > imps = opener.openImgs(fname, config)<br>
>>>> > imp = imps[0]<br>
>>>> ><br>
>>>> > print(imps)<br>
>>>> ><br>
>>>> > saver = ImgSaver()<br>
>>>> > saver.saveImg(target, imp)<br>
>>>> ><br>
>>>> > Thanks again for your time. Don't be sorry if you don't have time to<br>
>>>> > write an example.<br>
>>>> ><br>
>>>> > I will be happy to provide some python/scifio examples scripts.<br>
>>>> ><br>
>>>> ><br>
>>>> > --<br>
>>>> > Hadrien Mary<br>
>>>> ><br>
>>>> > Ph.D student in Biology<br>
>>>> > Tournier-Gachet Team<br>
>>>> > CNRS - LBCMCP - UMR 5088<br>
>>>> ><br>
>>>> > Université de Toulouse - Bât. 4R3B1<br>
>>>> > 118, route de Narbonne - 31062 Toulouse<br>
>>>> ><br>
>>>> ><br>
>>>> > On Thu, Jan 8, 2015 at 9:56 PM, Curtis Rueden <<a href="mailto:ctrueden@wisc.edu">ctrueden@wisc.edu</a>> wrote:<br>
>>>> >> Hi Hadrien,<br>
>>>> >><br>
>>>> >>> I tried to use SCIFIO to write cropped image on disk but it doesn not<br>
>>>> >>> work.<br>
>>>> >><br>
>>>> >> The error you see is because SCIFIO operates on ImgLib2 data<br>
>>>> >> structures, not<br>
>>>> >> ImagePlus objects.<br>
>>>> >><br>
>>>> >>> Is there is any “easy” alternative to BF setCropRegion function in<br>
>>>> >>> SCIFIO ?<br>
>>>> >><br>
>>>> >> Yes: you create a SCIFIOConfig, calling imgOpenerSetRegion [1] on it,<br>
>>>> >> then<br>
>>>> >> pass it as an argument to the ImgOpener. You'll get back an ImgLib2<br>
>>>> >> data<br>
>>>> >> object which can then be fed to the SCIFIO ImgSaver.<br>
>>>> >><br>
>>>> >> I'm sorry that I don't have time to whip up an example for you right<br>
>>>> >> now. It<br>
>>>> >> would be great to add more SCIFIO tutorials [2] that use the ImgOpener<br>
>>>> >> and<br>
>>>> >> ImgSaver, since they are much higher level APIs akin to the Bio-Formats<br>
>>>> >> "BF"<br>
>>>> >> functionality... please feel welcome to contribute some!<br>
>>>> >><br>
>>>> >> Regards,<br>
>>>> >> Curtis<br>
>>>> >><br>
>>>> >> [1]<br>
>>>> >><br>
>>>> >> <a href="http://javadoc.imagej.net/SCIFIO/io/scif/config/SCIFIOConfig.html#imgOpenerSetRegion(io.scif.img.ImageRegion)" target="_blank">http://javadoc.imagej.net/SCIFIO/io/scif/config/SCIFIOConfig.html#imgOpenerSetRegion(io.scif.img.ImageRegion)</a><br>
>>>> >> [2] <a href="https://github.com/scifio/scifio-tutorials" target="_blank">https://github.com/scifio/scifio-tutorials</a><br>
>>>> >><br>
>>>> >> On Thu, Jan 8, 2015 at 2:18 PM, Hadrien Mary <<a href="mailto:hadrien.mary@gmail.com">hadrien.mary@gmail.com</a>><br>
>>>> >> wrote:<br>
>>>> >>><br>
>>>> >>> Hi,<br>
>>>> >>><br>
>>>> >>> I am writing a python macro which iterate over all rois in ROI Manager<br>
>>>> >>> and then use setCropRegion function from bioformat plugin to open a<br>
>>>> >>> cropped region of an image.<br>
>>>> >>><br>
>>>> >>> I tried to use SCIFIO to write cropped image on disk but it doesn not<br>
>>>> >>> work.<br>
>>>> >>><br>
>>>> >>> (I am using an updated version of Fiji.)<br>
>>>> >>><br>
>>>> >>> Here is my script:<br>
>>>> >>><br>
>>>> >>> from ij.plugin.frame import RoiManager<br>
>>>> >>> from ij import IJ<br>
>>>> >>> from io.scif.img import ImgSaver<br>
>>>> >>><br>
>>>> >>> from loci.plugins import BF<br>
>>>> >>> from <a href="http://loci.plugins.in" target="_blank">loci.plugins.in</a> import ImporterOptions<br>
>>>> >>> from loci.common import Region<br>
>>>> >>><br>
>>>> >>> import os<br>
>>>> >>><br>
>>>> >>> # Get current image filename<br>
>>>> >>> imp = IJ.getImage()<br>
>>>> >>> f = imp.getOriginalFileInfo()<br>
>>>> >>> fname = os.path.join(f.directory, f.fileName)<br>
>>>> >>><br>
>>>> >>> IJ.log('Image filename is %s' % fname)<br>
>>>> >>><br>
>>>> >>> # Iterate over all ROIs from ROI Manager<br>
>>>> >>> rois = RoiManager.getInstance().getRoisAsArray()<br>
>>>> >>> for i, roi in enumerate(rois):<br>
>>>> >>><br>
>>>> >>>     crop_id = i +1<br>
>>>> >>>     IJ.log("Opening crop %i / %i" % (crop_id, len(rois)))<br>
>>>> >>><br>
>>>> >>>     bounds = roi.getBounds()<br>
>>>> >>><br>
>>>> >>>     x = bounds.x<br>
>>>> >>>     y = bounds.y<br>
>>>> >>>     w = bounds.width<br>
>>>> >>>     h = bounds.height<br>
>>>> >>><br>
>>>> >>>     # Import only cropped region of the image<br>
>>>> >>>     options = ImporterOptions()<br>
>>>> >>>     options.setCrop(True)<br>
>>>> >>>     options.setCropRegion(0, Region(x, y, w, h))<br>
>>>> >>>     options.setId(fname)<br>
>>>> >>>     imps = BF.openImagePlus(options)<br>
>>>> >>><br>
>>>> >>>     imp = imps[0]<br>
>>>> >>>     imp.show()<br>
>>>> >>><br>
>>>> >>>     crop_basename = "crop%i_%s" % (crop_id, f.fileName)<br>
>>>> >>>     crop_fname = os.path.join(f.directory, crop_basename)<br>
>>>> >>>     imp.setTitle(crop_basename)<br>
>>>> >>><br>
>>>> >>>     # Save image<br>
>>>> >>>     IJ.log("Saving crop to %s" % crop_fname)<br>
>>>> >>>     saver = ImgSaver()<br>
>>>> >>>     saver.saveImg(crop_basename, imp)<br>
>>>> >>><br>
>>>> >>> IJ.log('Done')<br>
>>>> >>><br>
>>>> >>> It fails with this error:<br>
>>>> >>><br>
>>>> >>> Traceback (most recent call last):<br>
>>>> >>>   File "/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py", line<br>
>>>> >>> 49, in <module><br>
>>>> >>>     saver.saveImg(crop_basename, imp)<br>
>>>> >>> TypeError: saveImg(): 1st arg can't be coerced to io.scif.Writer,<br>
>>>> >>> String<br>
>>>> >>><br>
>>>> >>> at org.python.core.Py.TypeError(Py.java:235)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:209)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.python.core.PyReflectedFunction.throwBadArgError(PyReflectedFunction.java:312)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:321)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:167)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)<br>
>>>> >>> at org.python.core.PyObject.__call__(PyObject.java:422)<br>
>>>> >>> at org.python.core.PyObject.__call__(PyObject.java:426)<br>
>>>> >>> at org.python.core.PyMethod.__call__(PyMethod.java:139)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.python.pycode._pyx7.f$0(/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py:51)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.python.pycode._pyx7.call_function(/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py)<br>
>>>> >>> at org.python.core.PyTableCode.call(PyTableCode.java:165)<br>
>>>> >>> at org.python.core.PyCode.call(PyCode.java:18)<br>
>>>> >>> at org.python.core.Py.runCode(Py.java:1275)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.scijava.plugins.scripting.jython.JythonScriptEngine.eval(JythonScriptEngine.java:76)<br>
>>>> >>> at org.scijava.script.ScriptModule.run(ScriptModule.java:175)<br>
>>>> >>> at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167)<br>
>>>> >>> at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126)<br>
>>>> >>> at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:164)<br>
>>>> >>> at java.util.concurrent.FutureTask.run(FutureTask.java:262)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)<br>
>>>> >>> at<br>
>>>> >>><br>
>>>> >>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)<br>
>>>> >>> at java.lang.Thread.run(Thread.java:745)<br>
>>>> >>><br>
>>>> >>> Three questions:<br>
>>>> >>><br>
>>>> >>> how can I write cropped image (I need to write them as OME Tiff so I<br>
>>>> >>> need BF or SCIFIO).<br>
>>>> >>> Is there is any “easy” alternative to BF setCropRegion function in<br>
>>>> >>> SCIFIO<br>
>>>> >>> ?<br>
>>>> >>> I am currently using RoiManager.getInstance().getRoisAsArray() to<br>
>>>> >>> retrieve all rois. How can I directly get a ROIManager instance from<br>
>>>> >>> RoiSet.zip file ?<br>
>>>> >>><br>
>>>> >>> Any help would be very appreciated.<br>
>>>> >>><br>
>>>> >>> Thanks !<br>
>>>> >>><br>
>>>> >>> —<br>
>>>> >>> Hadrien Mary<br>
>>>> >>><br>
>>>> >>> _______________________________________________<br>
>>>> >>> ImageJ-devel mailing list<br>
>>>> >>> <a href="mailto:ImageJ-devel@imagej.net">ImageJ-devel@imagej.net</a><br>
>>>> >>> <a href="http://imagej.net/mailman/listinfo/imagej-devel" target="_blank">http://imagej.net/mailman/listinfo/imagej-devel</a><br>
>>>> >><br>
>>>> >><br>
>>><br>
>>><br>
<br>
_______________________________________________<br>
ImageJ-devel mailing list<br>
<a href="mailto:ImageJ-devel@imagej.net">ImageJ-devel@imagej.net</a><br>
<a href="http://imagej.net/mailman/listinfo/imagej-devel" target="_blank">http://imagej.net/mailman/listinfo/imagej-devel</a><br>
</div></div></blockquote></div><br></div>