Merge pull request #252 from gatagat/patch-regionprops

BUG: Fix Orientation for diagonal regions in regionprops.
This commit is contained in:
Stefan van der Walt
2012-08-16 14:11:10 -07:00
2 changed files with 18 additions and 2 deletions
+5 -2
View File
@@ -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))
+13
View File
@@ -197,6 +197,19 @@ 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
diag = np.eye(10, dtype=int)
orientation_diag = regionprops(diag, ['Orientation'])[0]['Orientation']
assert_almost_equal(orientation_diag, -math.pi / 4)
orientation_diag = regionprops(np.flipud(diag), ['Orientation']
)[0]['Orientation']
assert_almost_equal(orientation_diag, math.pi / 4)
orientation_diag = regionprops(np.fliplr(diag), ['Orientation']
)[0]['Orientation']
assert_almost_equal(orientation_diag, math.pi / 4)
orientation_diag = regionprops(np.fliplr(np.flipud(diag)), ['Orientation']
)[0]['Orientation']
assert_almost_equal(orientation_diag, -math.pi / 4)
def test_perimeter():
perimeter = regionprops(SAMPLE, ['Perimeter'])[0]['Perimeter']