Login  Register

Re: saving an ROI to a file

Posted by Rasband, Wayne (NIH/NIMH) [E] on Feb 02, 2014; 6:48pm
URL: http://imagej.273.s1.nabble.com/saving-an-ROI-to-a-file-tp5006367p5006372.html

On Feb 1, 2014, at 10:51 PM, Aryeh Weiss wrote:

> On 2/2/14, 2:17 AM, Rasband, Wayne (NIH/NIMH) [E] wrote:
>> On Feb 1, 2014, at 3:46 PM, Aryeh Weiss wrote:
>>
>>> I am writing a Python script in which I want to save an ROI.
>>> I thought that RoiWriter ought to do this, with the following method:
>>>
>>> |*saveRoi <http://imagej.nih.gov/ij/developer/api/ij/plugin/filter/RoiWriter.html#saveRoi%28ij.ImagePlus%29>*(ImagePlus <http://imagej.nih.gov/ij/developer/api/ij/ImagePlus.html> imp)|
>>>
>>> However, I do not see how to specify the path. RoiReader takes the path as an argument, but not RoiWriter.
>>> So, tried saving with the Roi manager, while recording the command. However, the save command did nto record anything. Open form a file recorded, as did everything else I tried, but not Save...
>>>
>>> So I  am apparently missing something really obvious, but I must ask -- how does one save an Roi to a file, similar to what the Roi manager does?
>> Try using the command recorder (Plugins>Macros>Record). This is what I get when I record the File>Save As>Selection command:
>>
>>     IJ.saveAs(imp, "Selection", path);
>>
>> -wayne
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> Thank you for your quick reply.
> I tried using the command  recorder while saving the ROI with the ROI Manager, using
> More>Save... , but this did nto record anything.
>
> 1. Why did that not work?

It did not work because of a bug that is fixed in the latest ImageJ daily build (1.48r1). Saving a single ROI with the ROI Manager now records

   imp = IJ.getImage();
   IJ.saveAs(imp, "Selection", path);

when the recorder is in "JavaScript", "BeanShell" or "Java" mode.

2. Why does the saveRoi method not accept a path to save and ROI the way that readRoi does?
> Is this just the way ti developed, or is the something sbout ROIsl that makes it unreasonable to do that?

The daily build adds two static methods for saving and opening ROIs:

   RoiEncoder.save(roi, path)
   RoiDecoder.open(path)

Here is a JavaScript that demonstrates how to use these methods:

  dir = IJ.getDirectory("temp");
  path = dir + "test.roi";
  imp = IJ.getImage();
  roi = imp.getRoi();
  if (roi==null)
     IJ.error("No ROI");
  ok = RoiEncoder.save(roi, path);
  print("ok="+ok);
  imp.deleteRoi();
  IJ.wait(2000);
  roi = RoiDecoder.open(path);
  if (roi==null)
     print("Error opening "+path);
  else
     imp.setRoi(roi);

-wayne

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