<div dir="ltr">Hi Hadrien,<div><br></div><div><div>> I would like to be able to crop a region in X and Y while</div><div>> keeping any other dimensions which could exist (C, Z, T).</div></div><div><br></div><div>Here's some Java:</div><div><br></div><div><div> int minX = 128, maxX = 255, minY = 128, minY = 255;</div><div> AxisType[] axes = { Axes.X, Axes.Y };</div><div> String[] ranges = { minX + "-" + maxX, minY + "-" + maxY };</div><div> config.imgOpenerSetRegion(new ImageRegion(axes, ranges));</div></div><div><br></div><div>Adapted from here:</div><div><a href="https://github.com/scifio/scifio/blob/scifio-0.18.0/src/test/java/io/scif/img/utests/ImgOpenerTest.java#L189-L191">https://github.com/scifio/scifio/blob/scifio-0.18.0/src/test/java/io/scif/img/utests/ImgOpenerTest.java#L189-L191</a><br></div><div><br></div><div>And untested.</div><div><br></div><div>HTH,</div><div>Curtis</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 8, 2015 at 4:47 PM, 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">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>
<div class="HOEnZb"><div class="h5"><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>> 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 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, then<br>
>> pass it as an argument to the ImgOpener. You'll get back an ImgLib2 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 now. It<br>
>> would be great to add more SCIFIO tutorials [2] that use the ImgOpener and<br>
>> ImgSaver, since they are much higher level APIs akin to the Bio-Formats "BF"<br>
>> functionality... please feel welcome to contribute some!<br>
>><br>
>> Regards,<br>
>> Curtis<br>
>><br>
>> [1]<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>> 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, String<br>
>>><br>
>>> at org.python.core.Py.TypeError(Py.java:235)<br>
>>> at<br>
>>> org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:209)<br>
>>> at<br>
>>> org.python.core.PyReflectedFunction.throwBadArgError(PyReflectedFunction.java:312)<br>
>>> at<br>
>>> org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:321)<br>
>>> at<br>
>>> org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:167)<br>
>>> at<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>
>>> org.python.pycode._pyx7.f$0(/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py:51)<br>
>>> at<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>
>>> 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>
>>> org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:164)<br>
>>> at java.util.concurrent.FutureTask.run(FutureTask.java:262)<br>
>>> at<br>
>>> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)<br>
>>> at<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 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>
</div></div></blockquote></div><br></div>