Login  Register

Re: ROI coordinate format

Posted by pcloetens on Oct 21, 2007; 12:33pm
URL: http://imagej.273.s1.nabble.com/ROI-coordinate-format-tp3698183p3698184.html

Moo K. Chung wrote:
> Hi all,
>
> Anyone aware of ROI coordinate format? I am trying to read *.roi files
> directly into MATLAB. Thanks.
>

Hello,
I have written for Gnu Octave the simple function roiread that reads a
roi-file created by ImageJ. You can find it below. It reads only the
roitype and the xy coordinates of the roi.
Peter

roiread is the user-defined function defined from:
/usr/local/octave-esrf/m/imagej/roiread.m

## Copyright (C) 2006 Peter Cloetens
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

## roiread
## function [ ret ] = roiread (filename)
##      reads an ImageJ roi stored in filename
##      created for example in ImageJ: File/Save As/Selection...
##      default filename extension is .roi
##      ret.roitype contains the kind of selection (polygon, rectangle,
ellips, line, freehandline, segmentedline, noroi, freehand, traced,
angle, point)
##      ret.xycoords_lefttop contains X and Y coordinates of the
left/top corner (starting at 0, included in roi)
##      ret.xycoords_rightbottom contains X and Y coordinates of the
right/bottom corner (starting at 0, not included in roi !)
##      does not read other information from roi file

## Author: Peter Cloetens <[hidden email]>
##
## 2006-08-15 Peter Cloetens <[hidden email]>
## * Initial revision
## 2006-08-30 PC
## * correction for little endian machines
## * add roitypes noroi (?), traced and angle

function [ ret ] = roiread (filename)

     if !exist('filename','var')
         filename = [];
     endif
     if isempty(filename)
         help roiread
         return
     endif

     if isempty(findstr(filename,'.'))
         # we add .roi for lazy people
         filename = [filename '.roi'];
     endif

     fid = fopen(filename,'r','ieee-be');
     hd = fread(fid,8,'uint8');
     # check that this is an ImageJ roi
     if !isequal(hd(1:2),[73 111]')
         printf('%s does not contain a valid ImageJ roi\n',filename)
         ret = [];
     else
         switch hd(7)
             case 0
                 ret =
struct('roitype','polygon','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 1
                 ret =
struct('roitype','rectangle','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 2
                 ret =
struct('roitype','ellips','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 3
                 ret =
struct('roitype','line','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 4
                 ret =
struct('roitype','freehandline','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 5
                 ret =
struct('roitype','segmentedline','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 6
                 ret =
struct('roitype','noroi','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 7
                 ret =
struct('roitype','freehand','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 8
                 ret =
struct('roitype','traced','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 9
                 ret =
struct('roitype','angle','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             case 10
                 ret =
struct('roitype','point','xycoords_lefttop',[],'xycoords_rightbottom',[]);
             otherwise
                 ret =
struct('roitype','notyetimplemented','xycoords_lefttop',[],'xycoords_rightbottom',[]);
         endswitch
         coord = fread(fid,4,'int16');
         ret.xycoords_lefttop = [coord(2) coord(1)];
         ret.xycoords_rightbottom = [coord(4) coord(3)];
     endif

     fclose(fid);

endfunction