Added Yen tests, typo fixed

This commit is contained in:
radioxoma
2013-09-07 19:10:40 +03:00
committed by radioxoma
parent 8beb2ae605
commit 9e74f4eb70
2 changed files with 35 additions and 3 deletions
+33 -1
View File
@@ -3,7 +3,9 @@ from numpy.testing import assert_array_equal
import skimage
from skimage import data
from skimage.filter.thresholding import threshold_otsu, threshold_adaptive
from skimage.filter.thresholding import (threshold_adaptive,
threshold_otsu,
threshold_yen)
class TestSimpleImage():
@@ -25,6 +27,26 @@ class TestSimpleImage():
image = np.float64(self.image)
assert 2 <= threshold_otsu(image) < 3
def test_yen(self):
assert threshold_yen(self.image) == 2
def test_yen_negative_int(self):
image = self.image - 2
assert threshold_yen(image) == 0
def test_yen_float_image(self):
image = np.float64(self.image)
assert 2 <= threshold_yen(image) < 3
def test_yen_arange(self):
image = np.arange(256)
assert threshold_yen(image) == 127
def test_yen_binary(self):
image = np.zeros([2,256], dtype='uint8')
image[0] = 255
assert threshold_yen(image) < 1
def test_threshold_adaptive_generic(self):
def func(arr):
return arr.sum() / arr.shape[0]
@@ -92,5 +114,15 @@ def test_otsu_lena_image():
assert 140 < threshold_otsu(lena) < 142
def test_yen_coins_image():
coins = skimage.img_as_ubyte(data.coins())
assert 109 < threshold_yen(coins) < 111
def test_yen_coins_image_as_float():
coins = skimage.img_as_float(data.coins())
assert 0.43 < threshold_yen(coins) < 0.44
if __name__ == '__main__':
np.testing.run_module_suite()
+2 -2
View File
@@ -1,4 +1,4 @@
__all__ = ['threshold_otsu', 'threshold_adaptive', 'threshold_yen']
__all__ = ['threshold_adaptive', 'threshold_otsu', 'threshold_yen']
import numpy as np
import scipy.ndimage
@@ -171,7 +171,7 @@ def threshold_yen(image, nbins=256):
>>> thresh = threshold_yen(image)
>>> binary = image <= thresh
"""
hist, bin_centers = histogram(img, nbins)
hist, bin_centers = histogram(image, nbins)
norm_histo = hist.astype(float) / hist.sum() # Probability mass function
P1 = np.cumsum(norm_histo) # Cumulative normalized histogram
P1_sq = np.cumsum(norm_histo ** 2)