diff --git a/skimage/measure/_regionprops.py b/skimage/measure/_regionprops.py index ee58868c..be10d6ec 100644 --- a/skimage/measure/_regionprops.py +++ b/skimage/measure/_regionprops.py @@ -124,7 +124,7 @@ def regionprops(label_image, properties=['Area', 'Centroid'], * Orientation : float Angle between the X-axis and the major axis of the ellipse that has the same second-moments as the region. Ranging from `-pi/2` to - `-pi/2` in counter-clockwise direction. + `pi/2` in counter-clockwise direction. * Perimeter : float Perimeter of object which approximates the contour as a line through the centers of border pixels using a 4-connectivity. @@ -299,7 +299,10 @@ def regionprops(label_image, properties=['Area', 'Centroid'], if 'Orientation' in properties: if a - c == 0: - obj_props['Orientation'] = PI / 2 + if b > 0: + obj_props['Orientation'] = -PI / 4. + else: + obj_props['Orientation'] = PI / 4. else: obj_props['Orientation'] = - 0.5 * atan2(2 * b, (a - c)) diff --git a/skimage/measure/tests/test_regionprops.py b/skimage/measure/tests/test_regionprops.py index 915087dc..7faf0bac 100644 --- a/skimage/measure/tests/test_regionprops.py +++ b/skimage/measure/tests/test_regionprops.py @@ -197,6 +197,15 @@ def test_orientation(): # test correct quadrant determination orientation2 = regionprops(SAMPLE.T, ['Orientation'])[0]['Orientation'] assert_almost_equal(orientation2, math.pi / 2 - orientation) + # test diagonal regions + orientation_diag = regionprops(np.eye(10, dtype=int), ['Orientation'])[0]['Orientation'] + assert_almost_equal(orientation_diag, -math.pi / 4) + orientation_diag = regionprops(np.flipud(np.eye(10, dtype=int)), ['Orientation'])[0]['Orientation'] + assert_almost_equal(orientation_diag, math.pi / 4) + orientation_diag = regionprops(np.fliplr(np.eye(10, dtype=int)), ['Orientation'])[0]['Orientation'] + assert_almost_equal(orientation_diag, math.pi / 4) + orientation_diag = regionprops(np.fliplr(np.flipud(np.eye(10, dtype=int))), ['Orientation'])[0]['Orientation'] + assert_almost_equal(orientation_diag, -math.pi / 4) def test_perimeter(): perimeter = regionprops(SAMPLE, ['Perimeter'])[0]['Perimeter']