From 9f3fc737ba568b230334d45adf66d98067a90f5f Mon Sep 17 00:00:00 2001 From: Pradyumna Narayana Date: Thu, 3 Mar 2016 12:00:15 -0700 Subject: [PATCH 1/6] sigma_range is renamed to sigma_color and win_size scales with sigma_spatial --- skimage/restoration/_denoise.py | 11 +++++++---- skimage/restoration/tests/test_denoise.py | 8 ++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/skimage/restoration/_denoise.py b/skimage/restoration/_denoise.py index 7d47225a..032e4483 100644 --- a/skimage/restoration/_denoise.py +++ b/skimage/restoration/_denoise.py @@ -1,12 +1,13 @@ # coding: utf-8 import numpy as np +from math import ceil from .. import img_as_float from ..restoration._denoise_cy import _denoise_bilateral, _denoise_tv_bregman from .._shared.utils import _mode_deprecations import warnings -def denoise_bilateral(image, win_size=5, sigma_range=None, sigma_spatial=1, +def denoise_bilateral(image, win_size=None, sigma_color=None, sigma_spatial=1, bins=10000, mode='constant', cval=0, multichannel=True): """Denoise image using bilateral filter. @@ -19,7 +20,7 @@ def denoise_bilateral(image, win_size=5, sigma_range=None, sigma_spatial=1, Radiometric similarity is measured by the gaussian function of the euclidian distance between two color values and a certain standard deviation - (`sigma_range`). + (`sigma_color`). Parameters ---------- @@ -66,7 +67,7 @@ def denoise_bilateral(image, win_size=5, sigma_range=None, sigma_spatial=1, >>> astro = astro[220:300, 220:320] >>> noisy = astro + 0.6 * astro.std() * np.random.random(astro.shape) >>> noisy = np.clip(noisy, 0, 1) - >>> denoised = denoise_bilateral(noisy, sigma_range=0.05, sigma_spatial=15) + >>> denoised = denoise_bilateral(noisy, sigma_color=0.05, sigma_spatial=15) """ if multichannel: if image.ndim != 3: @@ -99,9 +100,11 @@ def denoise_bilateral(image, win_size=5, sigma_range=None, sigma_spatial=1, "``multichannel=True`` for 2-D RGB " "images.".format(image.shape)) + if win_size is None: + win_size = max(5, 2*ceil(3*sigma_spatial)+1) mode = _mode_deprecations(mode) - return _denoise_bilateral(image, win_size, sigma_range, sigma_spatial, + return _denoise_bilateral(image, win_size, sigma_color, sigma_spatial, bins, mode, cval) diff --git a/skimage/restoration/tests/test_denoise.py b/skimage/restoration/tests/test_denoise.py index e565d741..da963749 100644 --- a/skimage/restoration/tests/test_denoise.py +++ b/skimage/restoration/tests/test_denoise.py @@ -159,9 +159,9 @@ def test_denoise_bilateral_2d(): img += 0.5 * img.std() * np.random.rand(*img.shape) img = np.clip(img, 0, 1) - out1 = restoration.denoise_bilateral(img, sigma_range=0.1, + out1 = restoration.denoise_bilateral(img, sigma_color=0.1, sigma_spatial=20, multichannel=False) - out2 = restoration.denoise_bilateral(img, sigma_range=0.2, + out2 = restoration.denoise_bilateral(img, sigma_color=0.2, sigma_spatial=30, multichannel=False) # make sure noise is reduced in the checkerboard cells @@ -175,8 +175,8 @@ def test_denoise_bilateral_color(): img += 0.5 * img.std() * np.random.rand(*img.shape) img = np.clip(img, 0, 1) - out1 = restoration.denoise_bilateral(img, sigma_range=0.1, sigma_spatial=20) - out2 = restoration.denoise_bilateral(img, sigma_range=0.2, sigma_spatial=30) + out1 = restoration.denoise_bilateral(img, sigma_color=0.1, sigma_spatial=20) + out2 = restoration.denoise_bilateral(img, sigma_color=0.2, sigma_spatial=30) # make sure noise is reduced in the checkerboard cells assert img[30:45, 5:15].std() > out1[30:45, 5:15].std() From ecf90e1ccaede02c62ae4a1f5f81228f2a1e1089 Mon Sep 17 00:00:00 2001 From: Pradyumna Narayana Date: Thu, 3 Mar 2016 12:01:57 -0700 Subject: [PATCH 2/6] Added the chnage to comments --- skimage/restoration/_denoise.py | 1 + 1 file changed, 1 insertion(+) diff --git a/skimage/restoration/_denoise.py b/skimage/restoration/_denoise.py index 032e4483..7bb66b9c 100644 --- a/skimage/restoration/_denoise.py +++ b/skimage/restoration/_denoise.py @@ -28,6 +28,7 @@ def denoise_bilateral(image, win_size=None, sigma_color=None, sigma_spatial=1, Input image, 2D grayscale or RGB. win_size : int Window size for filtering. + If win_size is not specified, it is calculated as max(5, 2*ceil(3*sigma_spatial)+1) sigma_range : float Standard deviation for grayvalue/color distance (radiometric similarity). A larger value results in averaging of pixels with larger From 428cc3daeb44a7b893af41a73bc9a28b15b0ca1e Mon Sep 17 00:00:00 2001 From: Pradyumna Narayana Date: Thu, 3 Mar 2016 12:27:23 -0700 Subject: [PATCH 3/6] Casted ceil to int such that win_size is an int. --- skimage/restoration/_denoise.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/restoration/_denoise.py b/skimage/restoration/_denoise.py index 7bb66b9c..2151bbef 100644 --- a/skimage/restoration/_denoise.py +++ b/skimage/restoration/_denoise.py @@ -102,7 +102,7 @@ def denoise_bilateral(image, win_size=None, sigma_color=None, sigma_spatial=1, "images.".format(image.shape)) if win_size is None: - win_size = max(5, 2*ceil(3*sigma_spatial)+1) + win_size = max(5, 2*int(ceil(3*sigma_spatial))+1) mode = _mode_deprecations(mode) return _denoise_bilateral(image, win_size, sigma_color, sigma_spatial, From f069aada3c06dddcfdfbdd06986167007c8c2842 Mon Sep 17 00:00:00 2001 From: Pradyumna Narayana Date: Fri, 4 Mar 2016 08:32:57 -0700 Subject: [PATCH 4/6] Supports skimage_range. Displays deprecated warning when used. Two test cases to test this functionality. --- DEPENDS.txt | 2 +- skimage/restoration/_denoise.py | 14 +++++++++--- skimage/restoration/tests/test_denoise.py | 26 ++++++++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/DEPENDS.txt b/DEPENDS.txt index fc6d6679..edfe2dfa 100644 --- a/DEPENDS.txt +++ b/DEPENDS.txt @@ -2,7 +2,7 @@ Build Requirements ------------------ * `Python >= 2.6 `__ * `Numpy >= 1.7.2 `__ -* `Cython >= 0.21 `__ +* `Cython >= 0.23 `__ * `Six >=1.4 `__ * `SciPy >=0.9 `__ diff --git a/skimage/restoration/_denoise.py b/skimage/restoration/_denoise.py index 2151bbef..0519f39c 100644 --- a/skimage/restoration/_denoise.py +++ b/skimage/restoration/_denoise.py @@ -3,12 +3,12 @@ import numpy as np from math import ceil from .. import img_as_float from ..restoration._denoise_cy import _denoise_bilateral, _denoise_tv_bregman -from .._shared.utils import _mode_deprecations +from .._shared.utils import _mode_deprecations, skimage_deprecation, warn import warnings def denoise_bilateral(image, win_size=None, sigma_color=None, sigma_spatial=1, - bins=10000, mode='constant', cval=0, multichannel=True): + bins=10000, mode='constant', cval=0, multichannel=True, sigma_range=None): """Denoise image using bilateral filter. This is an edge-preserving and noise reducing denoising filter. It averages @@ -29,7 +29,7 @@ def denoise_bilateral(image, win_size=None, sigma_color=None, sigma_spatial=1, win_size : int Window size for filtering. If win_size is not specified, it is calculated as max(5, 2*ceil(3*sigma_spatial)+1) - sigma_range : float + sigma_color : float Standard deviation for grayvalue/color distance (radiometric similarity). A larger value results in averaging of pixels with larger radiometric differences. Note, that the image will be converted using @@ -101,6 +101,14 @@ def denoise_bilateral(image, win_size=None, sigma_color=None, sigma_spatial=1, "``multichannel=True`` for 2-D RGB " "images.".format(image.shape)) + if sigma_range is not None: + warn('`sigma_range` have been deprecated in favor of ' + '`sigma_color`. The `sigma_range` keyword argument ' + 'will be removed in v0.14', skimage_deprecation) + + #If sigma_range is provided, assign it to sigma_color + sigma_color = sigma_range + if win_size is None: win_size = max(5, 2*int(ceil(3*sigma_spatial))+1) diff --git a/skimage/restoration/tests/test_denoise.py b/skimage/restoration/tests/test_denoise.py index da963749..7bf9c7bf 100644 --- a/skimage/restoration/tests/test_denoise.py +++ b/skimage/restoration/tests/test_denoise.py @@ -4,7 +4,6 @@ from numpy.testing import run_module_suite, assert_raises, assert_equal from skimage import restoration, data, color, img_as_float, measure from skimage._shared._warnings import expected_warnings - np.random.seed(1234) @@ -212,6 +211,31 @@ def test_denoise_bilateral_nan(): out = restoration.denoise_bilateral(img, multichannel=False) assert_equal(img, out) +def test_denoise_sigma_range(): + img = checkerboard_gray.copy() + # add some random noise + img += 0.5 * img.std() * np.random.rand(*img.shape) + img = np.clip(img, 0, 1) + out1 = restoration.denoise_bilateral(img, sigma_color=0.1, + sigma_spatial=20, multichannel=False) + with expected_warnings('`sigma_range` have been deprecated in favor of `sigma_color`. ' + 'The `sigma_range` keyword argument will be removed in v0.14'): + out2 = restoration.denoise_bilateral(img, sigma_range=0.1, + sigma_spatial=20, multichannel=False) + assert_equal(out1, out2) + +def test_denoise_sigma_range_and_sigma_color(): + img = checkerboard_gray.copy() + # add some random noise + img += 0.5 * img.std() * np.random.rand(*img.shape) + img = np.clip(img, 0, 1) + out1 = restoration.denoise_bilateral(img, sigma_color=0.1, + sigma_spatial=20, multichannel=False) + with expected_warnings('`sigma_range` have been deprecated in favor of `sigma_color`. ' + 'The `sigma_range` keyword argument will be removed in v0.14'): + out2 = restoration.denoise_bilateral(img, sigma_color=0.2, sigma_range=0.1, + sigma_spatial=20, multichannel=False) + assert_equal(out1, out2) def test_nl_means_denoising_2d(): img = np.zeros((40, 40)) From 010df48453465705d0d5b527d4ff6f341f7dcfa0 Mon Sep 17 00:00:00 2001 From: Pradyumna Narayana Date: Fri, 4 Mar 2016 08:35:32 -0700 Subject: [PATCH 5/6] Added deprecated kwargs to TODO.txt --- TODO.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TODO.txt b/TODO.txt index a6dc649e..9e31032b 100644 --- a/TODO.txt +++ b/TODO.txt @@ -16,6 +16,8 @@ Version 0.14 * Remove deprecated ``skimage.data.lena`` and corresponding data files. * Remove deprecated ``skimage.measure.structural_similarity`` alias and deprecation warning test for this alias. +* Remove deprecated ``sigma_range`` kwargs in ``skimage.restoration.denoise_bilateral`` + and corresponding tests. Version 0.13 From 16ed4ed8657e976cd41187dcc047da173d354381 Mon Sep 17 00:00:00 2001 From: Pradyumna Narayana Date: Fri, 4 Mar 2016 09:29:32 -0700 Subject: [PATCH 6/6] Corrected a grammatical error in deprecated message. No functional change. --- skimage/restoration/_denoise.py | 2 +- skimage/restoration/tests/test_denoise.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/skimage/restoration/_denoise.py b/skimage/restoration/_denoise.py index 0519f39c..30954c70 100644 --- a/skimage/restoration/_denoise.py +++ b/skimage/restoration/_denoise.py @@ -102,7 +102,7 @@ def denoise_bilateral(image, win_size=None, sigma_color=None, sigma_spatial=1, "images.".format(image.shape)) if sigma_range is not None: - warn('`sigma_range` have been deprecated in favor of ' + warn('`sigma_range` has been deprecated in favor of ' '`sigma_color`. The `sigma_range` keyword argument ' 'will be removed in v0.14', skimage_deprecation) diff --git a/skimage/restoration/tests/test_denoise.py b/skimage/restoration/tests/test_denoise.py index 7bf9c7bf..71adee3f 100644 --- a/skimage/restoration/tests/test_denoise.py +++ b/skimage/restoration/tests/test_denoise.py @@ -218,7 +218,7 @@ def test_denoise_sigma_range(): img = np.clip(img, 0, 1) out1 = restoration.denoise_bilateral(img, sigma_color=0.1, sigma_spatial=20, multichannel=False) - with expected_warnings('`sigma_range` have been deprecated in favor of `sigma_color`. ' + with expected_warnings('`sigma_range` has been deprecated in favor of `sigma_color`. ' 'The `sigma_range` keyword argument will be removed in v0.14'): out2 = restoration.denoise_bilateral(img, sigma_range=0.1, sigma_spatial=20, multichannel=False) @@ -231,7 +231,7 @@ def test_denoise_sigma_range_and_sigma_color(): img = np.clip(img, 0, 1) out1 = restoration.denoise_bilateral(img, sigma_color=0.1, sigma_spatial=20, multichannel=False) - with expected_warnings('`sigma_range` have been deprecated in favor of `sigma_color`. ' + with expected_warnings('`sigma_range` has been deprecated in favor of `sigma_color`. ' 'The `sigma_range` keyword argument will be removed in v0.14'): out2 = restoration.denoise_bilateral(img, sigma_color=0.2, sigma_range=0.1, sigma_spatial=20, multichannel=False)