Skip to content

Commit

Permalink
make stack.filter_mean faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Henley13 committed Nov 17, 2020
1 parent 44d7b36 commit 13b4ce8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 8 additions & 5 deletions bigfish/stack/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from skimage.filters import gaussian

from scipy.ndimage import gaussian_laplace
from scipy.ndimage import convolve


# ### Filters ###
Expand Down Expand Up @@ -70,7 +71,7 @@ def _define_kernel(shape, size, dtype):


def mean_filter(image, kernel_shape, kernel_size):
"""Apply a mean filter to a 2-d image.
"""Apply a mean filter to a 2-d through convolution filter.
Parameters
----------
Expand All @@ -96,13 +97,15 @@ def mean_filter(image, kernel_shape, kernel_size):
check_parameter(kernel_shape=str,
kernel_size=(int, tuple, list))

# get kernel
# build kernel
kernel = _define_kernel(shape=kernel_shape,
size=kernel_size,
dtype=image.dtype)
dtype=np.float64)
n = kernel.sum()
kernel /= n

# apply filter
image_filtered = rank.mean(image, kernel)
# apply convolution filter
image_filtered = convolve(image, kernel)

return image_filtered

Expand Down
4 changes: 2 additions & 2 deletions bigfish/stack/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def test_mean_filter():
expected_x = np.array(
[[2, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[1, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 1]],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]],
dtype=np.uint8)
assert_array_equal(filtered_x, expected_x)
assert filtered_x.dtype == np.uint8
Expand Down

0 comments on commit 13b4ce8

Please sign in to comment.