Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions ijroi/ijroi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 12 additions & 6 deletions ijroi/tests/test_ijroi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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