diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index e079275..74cc7d2 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -3,14 +3,16 @@ # License: MIT import numpy as np +from collections import namedtuple def read_roi(fileobj): ''' - points = read_roi(fileobj) + points, position = read_roi(fileobj) Read ImageJ's ROI format. Points are returned in a nx2 array. Each row - is in [row, column] -- that is, (y,x) -- order. + is in [row, column] -- that is, (y,x) -- order. Position is the slice + number. ''' # This is based on: # http://rsbweb.nih.gov/ij/developer/source/ij/io/RoiDecoder.java.html @@ -119,8 +121,11 @@ def getfloat(): if options & SUB_PIXEL_RESOLUTION == 0: points[:, 1] += left points[:, 0] += top - - return points + + Roi = namedtuple('Roi', 'points position') + roi = Roi(points=points, position=position) + + return roi def read_roi_zip(fname): diff --git a/ijroi/tests/test_ijroi.py b/ijroi/tests/test_ijroi.py index 852043f..1fc585e 100644 --- a/ijroi/tests/test_ijroi.py +++ b/ijroi/tests/test_ijroi.py @@ -29,46 +29,52 @@ def test_rectangle(): def test_freehand_circle(): fixture = get_fixture("freehand_circle.roi") with fixture.open("rb") as f: - circle = ijroi.read_roi(f) + circle, position = ijroi.read_roi(f) assert len(circle) == 100 assert abs(circle[:, 1].mean()-10) < 0.01 assert abs(circle[:, 0].mean()-15) < 0.01 + assert position == 0 def test_integer_freehand(): fixture = get_fixture("freehand_integer.roi") with fixture.open("rb") as f: - freehand = ijroi.read_roi(f) + freehand, position = ijroi.read_roi(f) assert len(freehand) == 3 assert all(freehand[2, :] == [1, 10]) assert freehand.dtype == np.int16 + assert position == 0 def test_polygon(): fixture = get_fixture("polygon_circle.roi") with fixture.open("rb") as f: - circle = ijroi.read_roi(f) + circle, position = ijroi.read_roi(f) assert len(circle) == 100 assert abs(circle[:, 1].mean()-10) < 0.01 assert abs(circle[:, 0].mean()-15) < 0.01 + assert position == 0 fixture = get_fixture("polygon_integer.roi") with fixture.open("rb") as f: - polyint = ijroi.read_roi(f) + polyint, position = ijroi.read_roi(f) assert len(polyint) == 3 assert all(polyint[2, :] == [1, 10]) assert polyint.dtype == np.int16 + assert position == 0 def test_point(): fixture = get_fixture("int_point.roi") with fixture.open("rb") as f: - point = ijroi.read_roi(f) + point, position = ijroi.read_roi(f) assert point.ndim == 2 assert point[0,0] == 256 assert point[0,1] == 128 + assert position == 0 def test_float_point(): fixture = get_fixture("float_point.roi") with fixture.open("rb") as f: - point = ijroi.read_roi(f) + point, position = ijroi.read_roi(f) assert point.ndim == 2 assert abs(point[0,0] - 567.8) < 0.01 assert abs(point[0,1] - 123.4) < 0.01 + assert position == 0 \ No newline at end of file