Skip to content

Commit

Permalink
Sign error fix in measure.regionprops for orientations of 45 degrees (s…
Browse files Browse the repository at this point in the history
…cikit-image#6836)

* fixed sign error for PI/4 and -PI/4

* fixed orientation test and added continuity test
  • Loading branch information
jakeMartin1234 committed Mar 17, 2023
1 parent 907bfb7 commit 6db5c5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions skimage/measure/_regionprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,9 @@ def orientation(self):
a, b, b, c = self.inertia_tensor.flat
if a - c == 0:
if b < 0:
return -PI / 4.
else:
return PI / 4.
else:
return -PI / 4.
else:
return 0.5 * atan2(-2 * b, c - a)

Expand Down
23 changes: 19 additions & 4 deletions skimage/measure/tests/test_regionprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,22 +645,37 @@ def test_orientation():
# test diagonal regions
diag = np.eye(10, dtype=int)
orient_diag = regionprops(diag)[0].orientation
assert_almost_equal(orient_diag, -math.pi / 4)
assert_almost_equal(orient_diag, math.pi / 4)
orient_diag = regionprops(diag, spacing=(1, 2))[0].orientation
assert_almost_equal(orient_diag, np.arccos(0.5 / np.sqrt(1 + 0.5 ** 2)))
orient_diag = regionprops(np.flipud(diag))[0].orientation
assert_almost_equal(orient_diag, math.pi / 4)
assert_almost_equal(orient_diag, -math.pi / 4)
orient_diag = regionprops(np.flipud(diag), spacing=(1, 2))[0].orientation
assert_almost_equal(orient_diag, -np.arccos(0.5 / np.sqrt(1 + 0.5 ** 2)))
orient_diag = regionprops(np.fliplr(diag))[0].orientation
assert_almost_equal(orient_diag, math.pi / 4)
assert_almost_equal(orient_diag, -math.pi / 4)
orient_diag = regionprops(np.fliplr(diag), spacing=(1, 2))[0].orientation
assert_almost_equal(orient_diag, -np.arccos(0.5 / np.sqrt(1 + 0.5 ** 2)))
orient_diag = regionprops(np.fliplr(np.flipud(diag)))[0].orientation
assert_almost_equal(orient_diag, -math.pi / 4)
assert_almost_equal(orient_diag, math.pi / 4)
orient_diag = regionprops(np.fliplr(np.flipud(diag)), spacing=(1, 2))[0].orientation
assert_almost_equal(orient_diag, np.arccos(0.5 / np.sqrt(1 + 0.5 ** 2)))

def test_orientation_continuity():
# nearly diagonal array
arr1 = np.array([[0, 0, 1, 1],[0, 0, 1, 0],[0, 1, 0, 0],[1, 0, 0, 0]])
# diagonal array
arr2 = np.array([[0, 0, 0, 2],[0, 0, 2, 0],[0, 2, 0, 0],[2, 0, 0, 0]])
# nearly diagonal array
arr3 = np.array([[0, 0, 0, 3],[0, 0, 3, 3],[0, 3, 0, 0],[3, 0, 0, 0]])
image = np.hstack((arr1,arr2,arr3))
props = regionprops(image)
orientations = [prop.orientation for prop in props]
np.testing.assert_allclose(orientations, orientations[1], rtol=0, atol=0.08)
assert_almost_equal(orientations[0], -0.7144496360953664)
assert_almost_equal(orientations[1], -0.7853981633974483)
assert_almost_equal(orientations[2], -0.8563466906995303)


def test_perimeter():
per = regionprops(SAMPLE)[0].perimeter
Expand Down

0 comments on commit 6db5c5c

Please sign in to comment.