From a3d336f5606cff227c93c4ff683baa35b1c628df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Scho=CC=88nberger?= Date: Sun, 29 Apr 2012 17:49:33 +0200 Subject: [PATCH] cython implementation of adaptive thresholding replaced with pure python version --- skimage/filter/_thresholding.pyx | 35 -------------------------------- skimage/filter/setup.py | 3 --- skimage/filter/thresholding.py | 29 ++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 42 deletions(-) delete mode 100644 skimage/filter/_thresholding.pyx diff --git a/skimage/filter/_thresholding.pyx b/skimage/filter/_thresholding.pyx deleted file mode 100644 index c7a80478..00000000 --- a/skimage/filter/_thresholding.pyx +++ /dev/null @@ -1,35 +0,0 @@ -import numpy as np -import scipy.ndimage -cimport numpy as np -cimport cython - - -@cython.boundscheck(False) -@cython.wraparound(False) -def _threshold_adaptive(np.ndarray[np.double_t, ndim=2] image, int block_size, - method, double offset, mode, param): - cdef int r, c - cdef np.ndarray[np.double_t, ndim=2] thresh_image - - if method == 'generic': - thresh_image = scipy.ndimage.generic_filter(image, param, block_size, - mode=mode) - elif method == 'gaussian': - if param is None: - # automatically determine sigma which covers > 99% of distribution - sigma = (block_size - 1) / 6.0 - thresh_image = scipy.ndimage.gaussian_filter(image, sigma, mode=mode) - elif method == 'mean': - mask = 1. / block_size * np.ones((block_size,)) - # separation of filters to speedup convolution - thresh_image = scipy.ndimage.convolve1d(image, mask, axis=0, mode=mode) - thresh_image = scipy.ndimage.convolve1d(thresh_image, mask, axis=1, - mode=mode) - elif method == 'median': - thresh_image = scipy.ndimage.median_filter(image, block_size, mode=mode) - - for r in range(image.shape[0]): - for c in range(image.shape[1]): - thresh_image[r,c] = image[r,c] > (thresh_image[r,c] - offset) - - return thresh_image.astype('bool') diff --git a/skimage/filter/setup.py b/skimage/filter/setup.py index f93f7143..12cb84a7 100644 --- a/skimage/filter/setup.py +++ b/skimage/filter/setup.py @@ -12,12 +12,9 @@ def configuration(parent_package='', top_path=None): config.add_data_dir('tests') cython(['_ctmf.pyx'], working_path=base_path) - cython(['_thresholding.pyx'], working_path=base_path) config.add_extension('_ctmf', sources=['_ctmf.c'], include_dirs=[get_numpy_include_dirs()]) - config.add_extension('_thresholding', sources=['_thresholding.c'], - include_dirs=[get_numpy_include_dirs()]) return config diff --git a/skimage/filter/thresholding.py b/skimage/filter/thresholding.py index 77c6bf9d..e34a7751 100644 --- a/skimage/filter/thresholding.py +++ b/skimage/filter/thresholding.py @@ -1,5 +1,5 @@ import numpy as np - +import scipy.ndimage from skimage.exposure import histogram from ._thresholding import _threshold_adaptive @@ -63,9 +63,30 @@ def threshold_adaptive(image, block_size, method='gaussian', offset=0, >>> func = lambda arr: arr.mean() >>> binary_image2 = threshold_adaptive(image, 15, 'generic', param=func) """ - # not using img_as_float because offset parameter wouldn't work - image = image.astype('double') - return _threshold_adaptive(image, block_size, method, offset, mode, param) + thresh_image = np.zeros(image.shape, 'double') + if method == 'generic': + scipy.ndimage.generic_filter(image, param, block_size, + output=thresh_image, mode=mode) + elif method == 'gaussian': + if param is None: + # automatically determine sigma which covers > 99% of distribution + sigma = (block_size - 1) / 6.0 + scipy.ndimage.gaussian_filter(image, sigma, output=thresh_image, + mode=mode) + elif method == 'mean': + mask = 1. / block_size * np.ones((block_size,)) + # separation of filters to speedup convolution + scipy.ndimage.convolve1d(image, mask, axis=0, output=thresh_image, + mode=mode) + scipy.ndimage.convolve1d(thresh_image, mask, axis=1, + output=thresh_image, mode=mode) + elif method == 'median': + scipy.ndimage.median_filter(image, block_size, output=thresh_image, + mode=mode) + + thresh_image = image > (thresh_image - offset) + + return thresh_image.astype('bool') def threshold_otsu(image, nbins=256): """Return threshold value based on Otsu's method.