mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-03 21:14:04 +08:00
cython implementation of adaptive thresholding replaced with pure python version
This commit is contained in:
@@ -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')
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user