Skip to content

Commit

Permalink
Fix (x, y) vs. (y, x) confusion.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdsmith committed Apr 1, 2015
1 parent 020a615 commit d5d53a5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ it like:
>>> isinstance(roi, np.ndarray)
True
read\_roi returns a Nx2 array, where each row is a (row, column) or (y,
x) pair.

Based on `Luis Pedro Coelho <https://github.com/luispedro>`__'s
`readroi.py gist <https://gist.github.com/luispedro/3437255>`__.

Expand Down
7 changes: 4 additions & 3 deletions ijroi/ijroi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def read_roi(fileobj):
'''
points = read_roi(fileobj)
Read ImageJ's ROI format
Read ImageJ's ROI format. Points are returned in a nx2 array. Each row
is in [row, column] -- that is, (y,x) -- order.
'''
# This is based on:
# http://rsbweb.nih.gov/ij/developer/source/ij/io/RoiDecoder.java.html
Expand Down Expand Up @@ -97,11 +98,11 @@ def getfloat():
if roi_type == RoiType.RECT:
if options & SUB_PIXEL_RESOLUTION:
return np.array(
[[x1, y1], [x1+x2, y1], [x1+x2, y1+y2], [x1, y1+y2]],
[[y1, x1], [y1, x1+x2], [y1+y2, x1+x2], [y1+y2, x1]],
dtype=np.float32)
else:
return np.array(
[[left, top], [right, top], [right, bottom], [left, bottom]],
[[top, left], [top, right], [bottom, right], [bottom, left]],
dtype=np.int16)

if options & SUB_PIXEL_RESOLUTION:
Expand Down
Binary file modified ijroi/tests/fixtures/freehand_circle.roi
Binary file not shown.
2 changes: 1 addition & 1 deletion ijroi/tests/fixtures/generate_fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var y = new java.lang.reflect.Array.newInstance(java.lang.Float.TYPE, 100);

for(var i = 0; i < 100; ++i) {
x[i] = 10 + Math.cos(i/100.0 * 2*Math.PI);
y[i] = 10 + Math.sin(i/100.0 * 2*Math.PI);
y[i] = 15 + Math.sin(i/100.0 * 2*Math.PI);
}

freehand_circle = PolygonRoi(x, y, 100, Roi.FREEROI);
Expand Down
7 changes: 4 additions & 3 deletions ijroi/tests/test_ijroi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ def test_rectangle():
fixture = get_fixture("subpixel_rectangle.roi")
with fixture.open("rb") as f:
rect = ijroi.read_roi(f)
assert (rect == np.array([[4, 5], [8, 5], [8, 10], [4, 10]])).all()
assert (rect == np.array([[5, 4], [5, 8], [10, 8], [10, 4]])).all()
assert rect.dtype == np.float32

fixture = get_fixture("integer_rectangle.roi")
with fixture.open("rb") as f:
rect = ijroi.read_roi(f)
assert (rect == np.array([[4, 5], [8, 5], [8, 10], [4, 10]])).all()
assert (rect == np.array([[5, 4], [5, 8], [10, 8], [10, 4]])).all()
assert rect.dtype == np.int16

def test_freehand_circle():
fixture = get_fixture("freehand_circle.roi")
with fixture.open("rb") as f:
circle = ijroi.read_roi(f)
assert abs(circle[:,1].mean()-10) < 0.01
assert abs(circle[:, 1].mean()-10) < 0.01
assert abs(circle[:, 0].mean()-15) < 0.01

def test_integer_freehand():
fixture = get_fixture("freehand_integer.roi")
Expand Down

0 comments on commit d5d53a5

Please sign in to comment.