From 391b2edad6f12116191bb7bb27f86ffb787549ac Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 16 Apr 2013 06:38:11 +0530 Subject: [PATCH 01/13] Added a function for Gamma Correction --- skimage/exposure/__init__.py | 4 ++- skimage/exposure/exposure.py | 41 ++++++++++++++++++++++++- skimage/exposure/tests/test_exposure.py | 30 ++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/skimage/exposure/__init__.py b/skimage/exposure/__init__.py index aba05dae..c1789ab5 100644 --- a/skimage/exposure/__init__.py +++ b/skimage/exposure/__init__.py @@ -1,5 +1,6 @@ from .exposure import histogram, equalize, equalize_hist, \ rescale_intensity, cumulative_distribution + from ._adapthist import equalize_adapthist __all__ = ['histogram', @@ -7,4 +8,5 @@ __all__ = ['histogram', 'equalize_hist', 'equalize_adapthist', 'rescale_intensity', - 'cumulative_distribution'] + 'cumulative_distribution', + 'gammaCorrect'] diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 84462931..7e86c351 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -1,7 +1,7 @@ import warnings import numpy as np -from skimage import img_as_float +from skimage import img_as_float, img_as_ubyte from skimage.util.dtype import dtype_range import skimage.color as color from skimage.util.dtype import convert @@ -216,3 +216,42 @@ def rescale_intensity(image, in_range=None, out_range=None): image = (image - imin) / float(imax - imin) return dtype(image * (omax - omin) + omin) + + +def gammaCorrect(image, gamma = 1): + """Transform an image according to Gamma Correction also known as the + Power Law Transform. + + Ref :: http://en.wikipedia.org/wiki/Gamma_correction + + Parameters + ---------- + image : ndarray + Input image. + gamma : float + Non-negative real number + + Returns + ------- + out : ndarray + Image with Gamma Correction on the input image. + + Notes + ----- + This function transforms the input image pixelwise according to the + equation O = I**gamma after scaling each pixel in the range 0 to 1. + + For gamma greater than 1, the histogram will shift towards left and + the output image will be darker than the input image. + + For gamma less than 1, the histogram will shift towards right and + the output image will be brighter than the input image. + + """ + + if gamma < 0: + return "Gamma should be a non-negative real number" + scale = 255.0 + temp = img_as_ubyte(image) + out = ((temp/scale)**gamma)*scale + return out diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index a5f5aef9..79b866b8 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -175,3 +175,33 @@ def norm_brightness_err(img1, img2): if __name__ == '__main__': from numpy import testing testing.run_module_suite() + + +# Test Gamma Correction +# ===================== + +def test_gammaCorrect_one(): + """Same image should be returned for gamma equal to one""" + image = skimage.data.camera() + result = exposure.gammaCorrect(image, 1) + assert result.mean() == image.mean() + assert result.std() == image.std() + +def test_gammaCorrect_zero(): + """White image should be returned for gamma less than zero""" + image = skimage.img_as_float(data.camera()) + result = exposure.gammaCorrect(image, 0) + assert result.mean() == 255 + assert result.std() == 0 + +def test_gammaCorrect_less_one(): + """Output's mean should be greater than input's mean for gamma less than one""" + image = skimage.data.camera() + result = exposure.gammaCorrect(image, 0.5) + assert result.mean() > image.mean() + +def test_gammaCorrect_greater_one(): + """Output's mean should be less than input's mean for gamma greater than one""" + image = skimage.data.camera() + result = exposure.gammaCorrect(image, 2) + assert result.mean() < image.mean() From c1e8440f72001f1c88c389bcfdadeafccceee31a Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Thu, 18 Apr 2013 05:26:10 +0530 Subject: [PATCH 02/13] Prevented info loss, implemented Log and Sigmoid Correction --- skimage/exposure/__init__.py | 7 +- skimage/exposure/exposure.py | 114 +++++++++++++++++++----- skimage/exposure/tests/test_exposure.py | 76 ++++++++++++---- 3 files changed, 155 insertions(+), 42 deletions(-) diff --git a/skimage/exposure/__init__.py b/skimage/exposure/__init__.py index c1789ab5..b873c339 100644 --- a/skimage/exposure/__init__.py +++ b/skimage/exposure/__init__.py @@ -1,5 +1,6 @@ from .exposure import histogram, equalize, equalize_hist, \ - rescale_intensity, cumulative_distribution + rescale_intensity, cumulative_distribution, \ + adjust_gamma, adjust_sigmoid, adjust_log from ._adapthist import equalize_adapthist @@ -9,4 +10,6 @@ __all__ = ['histogram', 'equalize_adapthist', 'rescale_intensity', 'cumulative_distribution', - 'gammaCorrect'] + 'adjust_gamma', + 'adjust_sigmoid', + 'adjust_log'] diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 7e86c351..55ff6318 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -218,40 +218,106 @@ def rescale_intensity(image, in_range=None, out_range=None): return dtype(image * (omax - omin) + omin) -def gammaCorrect(image, gamma = 1): - """Transform an image according to Gamma Correction also known as the - Power Law Transform. +def correct(image, type = None, param1 = None, param2 = None ): + """Performs pixelwise image correction based on the type passed. - Ref :: http://en.wikipedia.org/wiki/Gamma_correction + Types of correction : gamma, logarithmic, sigmoid Parameters ---------- - image : ndarray + image : ndarray, type, param1, param2 Input image. - gamma : float - Non-negative real number + + type : {'gamma', 'logarithmic', 'sigmoid'} + Type of correction. + 'gamma' + Gamma Correction or Power Law Transform. + + 'logarithmic' + Logarithmic and Inverse Logarithmic transform. + + 'sigmoid' + Sigmoidal Transform or Contrast Adjustment + + param1 : float + For type 'gamma', gamma varying from zero to infinity. Default value 1. + For type 'logarithmic', param1 should be -1 for inverse logarithmic, + else correction will be logarithmic. Default to logarithmic. + For type 'sigmoid', gain. Default value 10. + + param2 : float + For type 'gamma', positive constatnt multiplier. Default value 1. + For type 'logarithmic', positive constatnt multiplier. Default value 1. + For type 'sigmoid', cutoff between 0 and 1. Default value 0.5. Returns ------- out : ndarray - Image with Gamma Correction on the input image. + Corrected input image according to the type used. - Notes - ----- - This function transforms the input image pixelwise according to the - equation O = I**gamma after scaling each pixel in the range 0 to 1. - - For gamma greater than 1, the histogram will shift towards left and - the output image will be darker than the input image. - - For gamma less than 1, the histogram will shift towards right and - the output image will be brighter than the input image. + References + ---------- + ..[1] http://en.wikipedia.org/wiki/Gamma_correction + ..[2] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf + ..[3] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : """ + if type == None: + return image + + if type == 'gamma': + + if param1 == None: + param1 = 1 + if param2 == None: + param2 = 1 + + + gamma = param1 + dtype = image.dtype.type + + if gamma < 0: + return "Gamma should be a non-negative real number" + + scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + out = ((image/scale)**gamma)*scale*param2 + return dtype(out) + + if type == 'logarithmic': + + dtype = image.dtype.type + scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + + if param2 == None: + param2 = 1 + if param1 == -1: + out = (2**(image/scale) - 1)*scale*param2 + return dtype(out) + + dtype = image.dtype.type + scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + out = np.log2(1 + image/scale)*scale*param2 + return dtype(out) + + if type == 'sigmoid': + + if param1 == None: + param1 = 10 + if param2 == None: + param2 = 0.5 + + gain = param1 + cutoff = param2 + + dtype = image.dtype.type + scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + out = (1/(1 + np.exp(gain*(cutoff - image/scale))))*scale + return dtype(out) + + + + + + + - if gamma < 0: - return "Gamma should be a non-negative real number" - scale = 255.0 - temp = img_as_ubyte(image) - out = ((temp/scale)**gamma)*scale - return out diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index 79b866b8..05715512 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -180,28 +180,72 @@ if __name__ == '__main__': # Test Gamma Correction # ===================== -def test_gammaCorrect_one(): +def test_gamma_correct_one(): """Same image should be returned for gamma equal to one""" - image = skimage.data.camera() - result = exposure.gammaCorrect(image, 1) + image = data.camera() + result = exposure.correct(image, 'gamma', 1) assert result.mean() == image.mean() assert result.std() == image.std() -def test_gammaCorrect_zero(): - """White image should be returned for gamma less than zero""" - image = skimage.img_as_float(data.camera()) - result = exposure.gammaCorrect(image, 0) - assert result.mean() == 255 + +def test_gamma_correct_zero(): + """White image should be returned for gamma equal to zero""" + image = data.camera() + result = exposure.correct(image, 'gamma', 0) + dtype = image.dtype.type + assert result.mean() == dtype_range[dtype][1] assert result.std() == 0 -def test_gammaCorrect_less_one(): - """Output's mean should be greater than input's mean for gamma less than one""" - image = skimage.data.camera() - result = exposure.gammaCorrect(image, 0.5) + +def test_gamma_correct_less_one(): + """Output's mean should be greater than input's mean for gamma less than + one""" + image = data.camera() + result = exposure.correct(image, 'gamma', 0.5) assert result.mean() > image.mean() -def test_gammaCorrect_greater_one(): - """Output's mean should be less than input's mean for gamma greater than one""" - image = skimage.data.camera() - result = exposure.gammaCorrect(image, 2) + +def test_gamma_correct_greater_one(): + """Output's mean should be less than input's mean for gamma greater than + one""" + image = data.camera() + result = exposure.correct(image,'gamma', 2) assert result.mean() < image.mean() + + +# Test Logarithmic Correction +# =========================== + +def test_logarithmic_correct(): + """Output's mean should be greater than input's mean for logarithmic + correction with multiplier constant equal to unity""" + image = data.camera() + result = exposure.correct(image, 'logarithmic') + assert result.mean() > image.mean() + + +def test_inv_logarithmic_correct(): + """Output's mean should be less than input's mean for inverse logarithmic + correction with multiplier constant equal to unity""" + image = data.camera() + result = exposure.correct(image, 'logarithmic', -1) + assert result.mean() < image.mean() + + +# Test Sigmoid Correction +# ======================= + +def test_sigmoid_correct_cutoff_one(): + """Output's mean should be less than input's mean for sigmoid + correction with cutoff equal to one and gain of 10""" + image = data.camera() + result = exposure.correct(image, 'sigmoid', 10, 1) + assert result.mean() < image.mean() + + +def test_sigmoid_correct_cutoff_zero(): + """Output's mean should be greater than input's mean for sigmoid + correction with cutoff equal to zero and gain of 10""" + image = data.camera() + result = exposure.correct(image, 'sigmoid', 10, 0) + assert result.mean() > image.mean() From 4d1c3348422c1872f22ae14637cad0789710e419 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Sat, 20 Apr 2013 04:56:00 +0530 Subject: [PATCH 03/13] Separating the functions --- skimage/exposure/exposure.py | 94 +++++++++++------------------------- 1 file changed, 28 insertions(+), 66 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 55ff6318..27ae3544 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -1,7 +1,7 @@ import warnings import numpy as np -from skimage import img_as_float, img_as_ubyte +from skimage import img_as_float from skimage.util.dtype import dtype_range import skimage.color as color from skimage.util.dtype import convert @@ -218,26 +218,16 @@ def rescale_intensity(image, in_range=None, out_range=None): return dtype(image * (omax - omin) + omin) -def correct(image, type = None, param1 = None, param2 = None ): - """Performs pixelwise image correction based on the type passed. - - Types of correction : gamma, logarithmic, sigmoid +def rescale_intensity_gamma(image, gamma = 1): + """Performs Gamma Correction also known as Power Law Transform. Parameters ---------- - image : ndarray, type, param1, param2 + image : ndarray Input image. - type : {'gamma', 'logarithmic', 'sigmoid'} - Type of correction. - 'gamma' - Gamma Correction or Power Law Transform. - - 'logarithmic' - Logarithmic and Inverse Logarithmic transform. - - 'sigmoid' - Sigmoidal Transform or Contrast Adjustment + gamma : float + Non negative real number param1 : float For type 'gamma', gamma varying from zero to infinity. Default value 1. @@ -258,66 +248,38 @@ def correct(image, type = None, param1 = None, param2 = None ): References ---------- ..[1] http://en.wikipedia.org/wiki/Gamma_correction - ..[2] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf - ..[3] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : - """ - if type == None: - return image - if type == 'gamma': + dtype = image.dtype.type - if param1 == None: - param1 = 1 - if param2 == None: - param2 = 1 + if gamma < 0: + return "Gamma should be a non-negative real number" + + scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + out = ((image / scale)**gamma) * scale * param2 + return dtype(out) - gamma = param1 - dtype = image.dtype.type +def rescale_intensity_logarithmic(image, gain = 1, inv = 1): - if gamma < 0: - return "Gamma should be a non-negative real number" + dtype = image.dtype.type + scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - out = ((image/scale)**gamma)*scale*param2 + if inv == -1: + out = (2**(image / scale) - 1) * scale * param2 return dtype(out) - if type == 'logarithmic': - - dtype = image.dtype.type - scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - - if param2 == None: - param2 = 1 - if param1 == -1: - out = (2**(image/scale) - 1)*scale*param2 - return dtype(out) - - dtype = image.dtype.type - scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - out = np.log2(1 + image/scale)*scale*param2 - return dtype(out) - - if type == 'sigmoid': - - if param1 == None: - param1 = 10 - if param2 == None: - param2 = 0.5 - - gain = param1 - cutoff = param2 - - dtype = image.dtype.type - scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - out = (1/(1 + np.exp(gain*(cutoff - image/scale))))*scale - return dtype(out) - - - - + out = np.log2(1 + image / scale) * scale * param2 + return dtype(out) +def rescale_intensity_sigmoid(image, cutoff = 0.5, gain = 1): + + dtype = image.dtype.type + scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale + return dtype(out) +..[2] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf + ..[3] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : From 8f8c58317c8c1c66fc96aa11e77ac2e234b49f28 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 22 Apr 2013 13:26:25 +0530 Subject: [PATCH 04/13] Separating correction methods into different functions --- skimage/exposure/exposure.py | 105 ++++++++++++++++++------ skimage/exposure/tests/test_exposure.py | 32 ++++---- 2 files changed, 98 insertions(+), 39 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 27ae3544..03c21a49 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -218,8 +218,9 @@ def rescale_intensity(image, in_range=None, out_range=None): return dtype(image * (omax - omin) + omin) -def rescale_intensity_gamma(image, gamma = 1): - """Performs Gamma Correction also known as Power Law Transform. +def rescale_intensity_gamma(image, gamma = 1, gain = 1): + """Performs Gamma Correction on the input image. + Also known as Power Law Transform. Parameters ---------- @@ -227,59 +228,117 @@ def rescale_intensity_gamma(image, gamma = 1): Input image. gamma : float - Non negative real number + Non negative real number. Default value is 1. - param1 : float - For type 'gamma', gamma varying from zero to infinity. Default value 1. - For type 'logarithmic', param1 should be -1 for inverse logarithmic, - else correction will be logarithmic. Default to logarithmic. - For type 'sigmoid', gain. Default value 10. - - param2 : float - For type 'gamma', positive constatnt multiplier. Default value 1. - For type 'logarithmic', positive constatnt multiplier. Default value 1. - For type 'sigmoid', cutoff between 0 and 1. Default value 0.5. + gain : float + The constant multiplier. Default value is 1. Returns ------- out : ndarray - Corrected input image according to the type used. + Gamma corrected output image. + + Notes + ----- + This function transforms the input image pixelwise according to the + equation O = I**gamma after scaling each pixel to the range 0 to 1. + + For gamma greater than 1, the histogram will shift towards left and + the output image will be darker than the input image. + + For gamma less than 1, the histogram will shift towards right and + the output image will be brighter than the input image. References ---------- ..[1] http://en.wikipedia.org/wiki/Gamma_correction - """ + """ dtype = image.dtype.type if gamma < 0: return "Gamma should be a non-negative real number" scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - out = ((image / scale)**gamma) * scale * param2 + out = ((image / scale) ** gamma) * scale * gain return dtype(out) def rescale_intensity_logarithmic(image, gain = 1, inv = 1): + """Performs Logarithmic correction on the input image. + Parameters + ---------- + image : ndarray + Input image. + + gain : float + The constant multiplier. Default value is 1. + + inv : float + Value passed should be -1 for inverse logarithmic correction, + else correction will be logarithmic. Default to logarithmic. + + Returns + ------- + out : ndarray + Logarithm corrected output image. + + Notes + ----- + This function transforms the input image pixelwise according to the + equation O = gain*log(1 + I) after scaling each pixel to the range 0 to 1. + For inverse logarithmic correction, the equation is O = gain*(2**I - 1) + + References + ---------- + ..[1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf + + """ dtype = image.dtype.type scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) if inv == -1: - out = (2**(image / scale) - 1) * scale * param2 + out = (2 ** (image / scale) - 1) * scale * gain return dtype(out) - out = np.log2(1 + image / scale) * scale * param2 + out = np.log2(1 + image / scale) * scale * gain return dtype(out) -def rescale_intensity_sigmoid(image, cutoff = 0.5, gain = 1): +def rescale_intensity_sigmoid(image, cutoff = 0.5, gain = 10): + """Performs Sigmoid Correction on input image also known + as Contrast Adjustment. + Parameters + ---------- + image : ndarray + Input image. + + cutoff : float + Cutoff of the sigmoid function. Default value is 0.5. + + gain : float + The constant multiplier in exponential's power of sigmoid function. + Default value is 10. + + Returns + ------- + out : ndarray + Sigmoid corrected output image. + + Notes + ----- + This function transforms the input image pixelwise according to the + equation O = 1/(1 + exp*(gain*(cutoff - I))) after scaling each pixel to + the range 0 to 1. + + References + ---------- + ..[1] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : + + """ dtype = image.dtype.type scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale return dtype(out) - - -..[2] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf - ..[3] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index 05715512..bf7e1a24 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -180,72 +180,72 @@ if __name__ == '__main__': # Test Gamma Correction # ===================== -def test_gamma_correct_one(): +def test_rescale_intensity_gamma_one(): """Same image should be returned for gamma equal to one""" image = data.camera() - result = exposure.correct(image, 'gamma', 1) + result = exposure.rescale_intensity_gamma(image, 1) assert result.mean() == image.mean() assert result.std() == image.std() -def test_gamma_correct_zero(): +def test_rescale_intensity_gamma_zero(): """White image should be returned for gamma equal to zero""" image = data.camera() - result = exposure.correct(image, 'gamma', 0) + result = exposure.rescale_intensity_gamma(image, 0) dtype = image.dtype.type assert result.mean() == dtype_range[dtype][1] assert result.std() == 0 -def test_gamma_correct_less_one(): +def test_rescale_intensity_gamma_less_one(): """Output's mean should be greater than input's mean for gamma less than one""" image = data.camera() - result = exposure.correct(image, 'gamma', 0.5) + result = exposure.rescale_intensity_gamma(image, 0.5) assert result.mean() > image.mean() -def test_gamma_correct_greater_one(): +def test_rescale_intensity_gamma_greater_one(): """Output's mean should be less than input's mean for gamma greater than one""" image = data.camera() - result = exposure.correct(image,'gamma', 2) + result = exposure.rescale_intensity_gamma(image, 2) assert result.mean() < image.mean() # Test Logarithmic Correction # =========================== -def test_logarithmic_correct(): +def test_rescale_intensity_logarithmic(): """Output's mean should be greater than input's mean for logarithmic correction with multiplier constant equal to unity""" image = data.camera() - result = exposure.correct(image, 'logarithmic') + result = exposure.rescale_intensity_logarithmic(image, 1) assert result.mean() > image.mean() -def test_inv_logarithmic_correct(): +def test_rescale_intensity_inv_logarithmic(): """Output's mean should be less than input's mean for inverse logarithmic correction with multiplier constant equal to unity""" image = data.camera() - result = exposure.correct(image, 'logarithmic', -1) + result = exposure.rescale_intensity_logarithmic(image, 1, -1) assert result.mean() < image.mean() # Test Sigmoid Correction # ======================= -def test_sigmoid_correct_cutoff_one(): +def test_rescale_intensity_sigmoid_cutoff_one(): """Output's mean should be less than input's mean for sigmoid correction with cutoff equal to one and gain of 10""" image = data.camera() - result = exposure.correct(image, 'sigmoid', 10, 1) + result = exposure.rescale_intensity_sigmoid(image, 1, 10) assert result.mean() < image.mean() -def test_sigmoid_correct_cutoff_zero(): +def test_rescale_intensity_sigmoid_cutoff_zero(): """Output's mean should be greater than input's mean for sigmoid correction with cutoff equal to zero and gain of 10""" image = data.camera() - result = exposure.correct(image, 'sigmoid', 10, 0) + result = exposure.rescale_intensity_sigmoid(image, 0, 10) assert result.mean() > image.mean() From c9330ea1861c0aa85e52b90e4c77982d62411cbc Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 22 Apr 2013 14:03:53 +0530 Subject: [PATCH 05/13] Correcting code quality issues --- skimage/exposure/exposure.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 03c21a49..7537406c 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -218,18 +218,17 @@ def rescale_intensity(image, in_range=None, out_range=None): return dtype(image * (omax - omin) + omin) -def rescale_intensity_gamma(image, gamma = 1, gain = 1): +def rescale_intensity_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. + Also known as Power Law Transform. Parameters ---------- image : ndarray Input image. - gamma : float Non negative real number. Default value is 1. - gain : float The constant multiplier. Default value is 1. @@ -264,17 +263,15 @@ def rescale_intensity_gamma(image, gamma = 1, gain = 1): return dtype(out) -def rescale_intensity_logarithmic(image, gain = 1, inv = 1): +def rescale_intensity_logarithmic(image, gain=1, inv=1): """Performs Logarithmic correction on the input image. Parameters ---------- image : ndarray Input image. - gain : float The constant multiplier. Default value is 1. - inv : float Value passed should be -1 for inverse logarithmic correction, else correction will be logarithmic. Default to logarithmic. @@ -306,18 +303,17 @@ def rescale_intensity_logarithmic(image, gain = 1, inv = 1): return dtype(out) -def rescale_intensity_sigmoid(image, cutoff = 0.5, gain = 10): - """Performs Sigmoid Correction on input image also known - as Contrast Adjustment. +def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10): + """Performs Sigmoid Correction on input image. + + Also known as Contrast Adjustment. Parameters ---------- image : ndarray Input image. - cutoff : float Cutoff of the sigmoid function. Default value is 0.5. - gain : float The constant multiplier in exponential's power of sigmoid function. Default value is 10. From e1c23ac08c2ac3a956d75136ea63377e439ae867 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 22 Apr 2013 14:38:56 +0530 Subject: [PATCH 06/13] Added better tests and removed weak ones --- skimage/exposure/exposure.py | 18 +++++------ skimage/exposure/tests/test_exposure.py | 40 ++++++++++++++++++------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 7537406c..ca8e99df 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -240,7 +240,7 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): Notes ----- This function transforms the input image pixelwise according to the - equation O = I**gamma after scaling each pixel to the range 0 to 1. + equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1. For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image. @@ -250,7 +250,7 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): References ---------- - ..[1] http://en.wikipedia.org/wiki/Gamma_correction + .. [1] http://en.wikipedia.org/wiki/Gamma_correction """ dtype = image.dtype.type @@ -284,12 +284,12 @@ def rescale_intensity_logarithmic(image, gain=1, inv=1): Notes ----- This function transforms the input image pixelwise according to the - equation O = gain*log(1 + I) after scaling each pixel to the range 0 to 1. - For inverse logarithmic correction, the equation is O = gain*(2**I - 1) + equation ``O = gain*log(1 + I)`` after scaling each pixel to the range 0 to 1. + For inverse logarithmic correction, the equation is ``O = gain*(2**I - 1)``. References ---------- - ..[1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf + .. [1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf """ dtype = image.dtype.type @@ -305,7 +305,7 @@ def rescale_intensity_logarithmic(image, gain=1, inv=1): def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10): """Performs Sigmoid Correction on input image. - + Also known as Contrast Adjustment. Parameters @@ -326,12 +326,12 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10): Notes ----- This function transforms the input image pixelwise according to the - equation O = 1/(1 + exp*(gain*(cutoff - I))) after scaling each pixel to - the range 0 to 1. + equation ``O = 1/(1 + exp*(gain*(cutoff - I)))`` after scaling each pixel + to the range 0 to 1. References ---------- - ..[1] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : + .. [1] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : """ dtype = image.dtype.type diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index bf7e1a24..0bd32812 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -217,35 +217,53 @@ def test_rescale_intensity_gamma_greater_one(): # =========================== def test_rescale_intensity_logarithmic(): - """Output's mean should be greater than input's mean for logarithmic - correction with multiplier constant equal to unity""" + """Output's mean should be greater than input's mean and all pixel values + in output should be either greater than or equal to that of corresponding + pixel in input for logarithmiccorrection with multiplier constant equal + to unity""" image = data.camera() result = exposure.rescale_intensity_logarithmic(image, 1) assert result.mean() > image.mean() - + assert result.all() >= image.all() def test_rescale_intensity_inv_logarithmic(): - """Output's mean should be less than input's mean for inverse logarithmic - correction with multiplier constant equal to unity""" + """Output's mean should be less than input's mean and all pixel values + in output should be either less than or equal to that of corresponding + pixel in inputfor inverse logarithmic correction with multiplier constant + equal to unity""" image = data.camera() result = exposure.rescale_intensity_logarithmic(image, 1, -1) assert result.mean() < image.mean() + assert result.all() <= image.all() # Test Sigmoid Correction # ======================= def test_rescale_intensity_sigmoid_cutoff_one(): - """Output's mean should be less than input's mean for sigmoid - correction with cutoff equal to one and gain of 10""" + """Output's std should be less than input's std and all pixel values + in output should be either less than or equal to that of + corresponding pixel in input for sigmoid correction with cutoff equal + to one and gain of 10""" image = data.camera() result = exposure.rescale_intensity_sigmoid(image, 1, 10) - assert result.mean() < image.mean() + assert result.std() < image.std() + assert result.all() <= image.all() def test_rescale_intensity_sigmoid_cutoff_zero(): - """Output's mean should be greater than input's mean for sigmoid - correction with cutoff equal to zero and gain of 10""" + """Output's std should be less than input's std and all pixel values + in output should be either greater than or equal to that of + corresponding pixel in input for sigmoid correction with cutoff equal + to zero and gain of 10""" image = data.camera() result = exposure.rescale_intensity_sigmoid(image, 0, 10) - assert result.mean() > image.mean() + assert result.std() < image.std() + assert result.all() >= image.all() + +def test_rescale_intensity_sigmoid_cutoff_half(): + """Output's std should be greater than input's std for sigmoid + correction with cutoff equal to 0.5 and gain of 10""" + image = data.camera() + result = exposure.rescale_intensity_sigmoid(image, 0.5, 10) + assert result.std() > image.std() From a88ae64efbbffec81b8c9b33b126a2ce89f5a9da Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 23 Apr 2013 05:42:21 +0530 Subject: [PATCH 07/13] Added stronger tests --- skimage/exposure/tests/test_exposure.py | 139 ++++++++++++++++-------- 1 file changed, 96 insertions(+), 43 deletions(-) diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index 0bd32812..b77f9a37 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -2,6 +2,7 @@ import warnings import numpy as np from numpy.testing import assert_array_almost_equal as assert_close +from numpy.testing import assert_array_equal import skimage from skimage import data from skimage import exposure @@ -184,8 +185,7 @@ def test_rescale_intensity_gamma_one(): """Same image should be returned for gamma equal to one""" image = data.camera() result = exposure.rescale_intensity_gamma(image, 1) - assert result.mean() == image.mean() - assert result.std() == image.std() + assert_array_equal(result, image) def test_rescale_intensity_gamma_zero(): @@ -198,72 +198,125 @@ def test_rescale_intensity_gamma_zero(): def test_rescale_intensity_gamma_less_one(): - """Output's mean should be greater than input's mean for gamma less than - one""" - image = data.camera() + """Verifying the output with expected results for gamma + correction with gamma equal to half""" + image = np.uint8(4 * np.arange(64).reshape(8,8)) + expected = np.array([[ 0, 31, 45, 55, 63, 71, 78, 84], + [ 90, 95, 100, 105, 110, 115, 119, 123], + [127, 131, 135, 139, 142, 146, 149, 153], + [156, 159, 162, 165, 168, 171, 174, 177], + [180, 183, 186, 188, 191, 194, 196, 199], + [201, 204, 206, 209, 211, 214, 216, 218], + [221, 223, 225, 228, 230, 232, 234, 236], + [238, 241, 243, 245, 247, 249, 251, 253]], dtype=np.uint8) + result = exposure.rescale_intensity_gamma(image, 0.5) - assert result.mean() > image.mean() + assert_array_equal(result, expected) def test_rescale_intensity_gamma_greater_one(): - """Output's mean should be less than input's mean for gamma greater than - one""" - image = data.camera() + """Verifying the output with expected results for gamma + correction with gamma equal to two""" + image = np.uint8(4 * np.arange(64).reshape(8,8)) + expected = np.array([[ 0, 0, 0, 0, 1, 1, 2, 3], + [ 4, 5, 6, 7, 9, 10, 12, 14], + [ 16, 18, 20, 22, 25, 27, 30, 33], + [ 36, 39, 42, 45, 49, 52, 56, 60], + [ 64, 68, 72, 76, 81, 85, 90, 95], + [100, 105, 110, 116, 121, 127, 132, 138], + [144, 150, 156, 163, 169, 176, 182, 189], + [196, 203, 211, 218, 225, 233, 241, 249]], dtype=np.uint8) + result = exposure.rescale_intensity_gamma(image, 2) - assert result.mean() < image.mean() + assert_array_equal(result, expected) # Test Logarithmic Correction # =========================== def test_rescale_intensity_logarithmic(): - """Output's mean should be greater than input's mean and all pixel values - in output should be either greater than or equal to that of corresponding - pixel in input for logarithmiccorrection with multiplier constant equal - to unity""" - image = data.camera() + """Verifying the output with expected results for logarithmic + correction with multiplier constant multiplier equal to unity""" + image = np.uint8(4 * np.arange(64).reshape(8,8)) + expected = np.array([[ 0, 5, 11, 16, 22, 27, 33, 38], + [ 43, 48, 53, 58, 63, 68, 73, 77], + [ 82, 86, 91, 95, 100, 104, 109, 113], + [117, 121, 125, 129, 133, 137, 141, 145], + [149, 153, 157, 160, 164, 168, 172, 175], + [179, 182, 186, 189, 193, 196, 199, 203], + [206, 209, 213, 216, 219, 222, 225, 228], + [231, 234, 238, 241, 244, 246, 249, 252]], dtype=np.uint8) + result = exposure.rescale_intensity_logarithmic(image, 1) - assert result.mean() > image.mean() - assert result.all() >= image.all() + assert_array_equal(result, expected) + def test_rescale_intensity_inv_logarithmic(): - """Output's mean should be less than input's mean and all pixel values - in output should be either less than or equal to that of corresponding - pixel in inputfor inverse logarithmic correction with multiplier constant - equal to unity""" - image = data.camera() + """Verifying the output with expected results for inverse logarithmic + correction with multiplier constant multiplier equal to unity""" + image = np.uint8(4 * np.arange(64).reshape(8,8)) + expected = np.array([[ 0, 2, 5, 8, 11, 14, 17, 20], + [ 23, 26, 29, 32, 35, 38, 41, 45], + [ 48, 51, 55, 58, 61, 65, 68, 72], + [ 76, 79, 83, 87, 90, 94, 98, 102], + [106, 110, 114, 118, 122, 126, 130, 134], + [138, 143, 147, 151, 156, 160, 165, 170], + [174, 179, 184, 188, 193, 198, 203, 208], + [213, 218, 224, 229, 234, 239, 245, 250]], dtype=np.uint8) + result = exposure.rescale_intensity_logarithmic(image, 1, -1) - assert result.mean() < image.mean() - assert result.all() <= image.all() + assert_array_equal(result, expected) # Test Sigmoid Correction # ======================= def test_rescale_intensity_sigmoid_cutoff_one(): - """Output's std should be less than input's std and all pixel values - in output should be either less than or equal to that of - corresponding pixel in input for sigmoid correction with cutoff equal - to one and gain of 10""" - image = data.camera() - result = exposure.rescale_intensity_sigmoid(image, 1, 10) - assert result.std() < image.std() - assert result.all() <= image.all() + """Verifying the output with expected results for sigmoid correction + with cutoff equal to one and gain of 5""" + image = np.uint8(4 * np.arange(64).reshape(8,8)) + expected = np.array([[ 1, 1, 1, 2, 2, 2, 2, 2], + [ 3, 3, 3, 4, 4, 4, 5, 5], + [ 5, 6, 6, 7, 7, 8, 9, 10], + [ 10, 11, 12, 13, 14, 15, 16, 18], + [ 19, 20, 22, 24, 25, 27, 29, 32], + [ 34, 36, 39, 41, 44, 47, 50, 54], + [ 57, 61, 64, 68, 72, 76, 80, 85], + [ 89, 94, 99, 104, 108, 113, 118, 123]], dtype=np.uint8) + + result = exposure.rescale_intensity_sigmoid(image, 1, 5) + assert_array_equal(result, expected) def test_rescale_intensity_sigmoid_cutoff_zero(): - """Output's std should be less than input's std and all pixel values - in output should be either greater than or equal to that of - corresponding pixel in input for sigmoid correction with cutoff equal - to zero and gain of 10""" - image = data.camera() + """Verifying the output with expected results for sigmoid correction + with cutoff equal to zero and gain of 10""" + image = np.uint8(4 * np.arange(64).reshape(8,8)) + expected = np.array([[127, 137, 147, 156, 166, 175, 183, 191], + [198, 205, 211, 216, 221, 225, 229, 232], + [235, 238, 240, 242, 244, 245, 247, 248], + [249, 250, 250, 251, 251, 252, 252, 253], + [253, 253, 253, 253, 254, 254, 254, 254], + [254, 254, 254, 254, 254, 254, 254, 254], + [254, 254, 254, 254, 254, 254, 254, 254], + [254, 254, 254, 254, 254, 254, 254, 254]], dtype=np.uint8) + result = exposure.rescale_intensity_sigmoid(image, 0, 10) - assert result.std() < image.std() - assert result.all() >= image.all() + assert_array_equal(result, expected) + def test_rescale_intensity_sigmoid_cutoff_half(): - """Output's std should be greater than input's std for sigmoid - correction with cutoff equal to 0.5 and gain of 10""" - image = data.camera() + """Verifying the output with expected results for sigmoid correction + with cutoff equal to half and gain of 10""" + image = np.uint8(4 * np.arange(64).reshape(8,8)) + expected = np.array([[ 1, 1, 2, 2, 3, 3, 4, 5], + [ 5, 6, 7, 9, 10, 12, 14, 16], + [ 19, 22, 25, 29, 34, 39, 44, 50], + [ 57, 64, 72, 80, 89, 99, 108, 118], + [128, 138, 148, 158, 167, 176, 184, 192], + [199, 205, 211, 217, 221, 226, 229, 233], + [236, 238, 240, 242, 244, 246, 247, 248], + [249, 250, 250, 251, 251, 252, 252, 253]], dtype=np.uint8) + result = exposure.rescale_intensity_sigmoid(image, 0.5, 10) - assert result.std() > image.std() + assert_array_equal(result, expected) From a475d3c694812f008f34a684dcd9ae630cc24f8c Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 29 Apr 2013 13:19:51 +0530 Subject: [PATCH 08/13] Corrections and Improvements --- skimage/exposure/exposure.py | 19 ++++++++++++------- skimage/exposure/tests/test_exposure.py | 25 ++++++++++++------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index ca8e99df..6a8ee2f2 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -263,7 +263,7 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): return dtype(out) -def rescale_intensity_logarithmic(image, gain=1, inv=1): +def rescale_intensity_log(image, gain=1, inv=False): """Performs Logarithmic correction on the input image. Parameters @@ -273,8 +273,8 @@ def rescale_intensity_logarithmic(image, gain=1, inv=1): gain : float The constant multiplier. Default value is 1. inv : float - Value passed should be -1 for inverse logarithmic correction, - else correction will be logarithmic. Default to logarithmic. + If True, it performs inverse logarithmic correction, + else correction will be logarithmic. Defaults to False. Returns ------- @@ -295,7 +295,7 @@ def rescale_intensity_logarithmic(image, gain=1, inv=1): dtype = image.dtype.type scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - if inv == -1: + if inv == True: out = (2 ** (image / scale) - 1) * scale * gain return dtype(out) @@ -303,7 +303,7 @@ def rescale_intensity_logarithmic(image, gain=1, inv=1): return dtype(out) -def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10): +def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): """Performs Sigmoid Correction on input image. Also known as Contrast Adjustment. @@ -313,11 +313,13 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10): image : ndarray Input image. cutoff : float - Cutoff of the sigmoid function. Default value is 0.5. + Cutoff of the sigmoid function that shifts the characteristic curve + in horizontal direction. Default value is 0.5. gain : float The constant multiplier in exponential's power of sigmoid function. Default value is 10. - + inv : If True, returns the negative sigmoid correction. Defaults to + False. Returns ------- out : ndarray @@ -336,5 +338,8 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10): """ dtype = image.dtype.type scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + if inv == True: + out = 1 - (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale + return dtype(out) out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale return dtype(out) diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index b77f9a37..bb542bc3 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -183,24 +183,23 @@ if __name__ == '__main__': def test_rescale_intensity_gamma_one(): """Same image should be returned for gamma equal to one""" - image = data.camera() + image = np.random.random((8, 8)) result = exposure.rescale_intensity_gamma(image, 1) assert_array_equal(result, image) def test_rescale_intensity_gamma_zero(): """White image should be returned for gamma equal to zero""" - image = data.camera() + image = np.random.random((8, 8)) result = exposure.rescale_intensity_gamma(image, 0) dtype = image.dtype.type - assert result.mean() == dtype_range[dtype][1] - assert result.std() == 0 + assert_array_equal(result, dtype_range[dtype][1]) def test_rescale_intensity_gamma_less_one(): """Verifying the output with expected results for gamma correction with gamma equal to half""" - image = np.uint8(4 * np.arange(64).reshape(8,8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 0, 31, 45, 55, 63, 71, 78, 84], [ 90, 95, 100, 105, 110, 115, 119, 123], [127, 131, 135, 139, 142, 146, 149, 153], @@ -217,7 +216,7 @@ def test_rescale_intensity_gamma_less_one(): def test_rescale_intensity_gamma_greater_one(): """Verifying the output with expected results for gamma correction with gamma equal to two""" - image = np.uint8(4 * np.arange(64).reshape(8,8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 0, 0, 0, 0, 1, 1, 2, 3], [ 4, 5, 6, 7, 9, 10, 12, 14], [ 16, 18, 20, 22, 25, 27, 30, 33], @@ -237,7 +236,7 @@ def test_rescale_intensity_gamma_greater_one(): def test_rescale_intensity_logarithmic(): """Verifying the output with expected results for logarithmic correction with multiplier constant multiplier equal to unity""" - image = np.uint8(4 * np.arange(64).reshape(8,8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 0, 5, 11, 16, 22, 27, 33, 38], [ 43, 48, 53, 58, 63, 68, 73, 77], [ 82, 86, 91, 95, 100, 104, 109, 113], @@ -247,14 +246,14 @@ def test_rescale_intensity_logarithmic(): [206, 209, 213, 216, 219, 222, 225, 228], [231, 234, 238, 241, 244, 246, 249, 252]], dtype=np.uint8) - result = exposure.rescale_intensity_logarithmic(image, 1) + result = exposure.rescale_intensity_log(image, 1) assert_array_equal(result, expected) def test_rescale_intensity_inv_logarithmic(): """Verifying the output with expected results for inverse logarithmic correction with multiplier constant multiplier equal to unity""" - image = np.uint8(4 * np.arange(64).reshape(8,8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 0, 2, 5, 8, 11, 14, 17, 20], [ 23, 26, 29, 32, 35, 38, 41, 45], [ 48, 51, 55, 58, 61, 65, 68, 72], @@ -264,7 +263,7 @@ def test_rescale_intensity_inv_logarithmic(): [174, 179, 184, 188, 193, 198, 203, 208], [213, 218, 224, 229, 234, 239, 245, 250]], dtype=np.uint8) - result = exposure.rescale_intensity_logarithmic(image, 1, -1) + result = exposure.rescale_intensity_log(image, 1, True) assert_array_equal(result, expected) @@ -274,7 +273,7 @@ def test_rescale_intensity_inv_logarithmic(): def test_rescale_intensity_sigmoid_cutoff_one(): """Verifying the output with expected results for sigmoid correction with cutoff equal to one and gain of 5""" - image = np.uint8(4 * np.arange(64).reshape(8,8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 1, 1, 1, 2, 2, 2, 2, 2], [ 3, 3, 3, 4, 4, 4, 5, 5], [ 5, 6, 6, 7, 7, 8, 9, 10], @@ -291,7 +290,7 @@ def test_rescale_intensity_sigmoid_cutoff_one(): def test_rescale_intensity_sigmoid_cutoff_zero(): """Verifying the output with expected results for sigmoid correction with cutoff equal to zero and gain of 10""" - image = np.uint8(4 * np.arange(64).reshape(8,8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[127, 137, 147, 156, 166, 175, 183, 191], [198, 205, 211, 216, 221, 225, 229, 232], [235, 238, 240, 242, 244, 245, 247, 248], @@ -308,7 +307,7 @@ def test_rescale_intensity_sigmoid_cutoff_zero(): def test_rescale_intensity_sigmoid_cutoff_half(): """Verifying the output with expected results for sigmoid correction with cutoff equal to half and gain of 10""" - image = np.uint8(4 * np.arange(64).reshape(8,8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 1, 1, 2, 2, 3, 3, 4, 5], [ 5, 6, 7, 9, 10, 12, 14, 16], [ 19, 22, 25, 29, 34, 39, 44, 50], From 23f61795ebcbba1fa49b1f33e78b0544be2301c3 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Fri, 3 May 2013 15:24:53 +0530 Subject: [PATCH 09/13] Minor improvements in docs, tests --- skimage/exposure/exposure.py | 28 ++++++++++--------------- skimage/exposure/tests/test_exposure.py | 25 ++++++++++++++++++---- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 6a8ee2f2..4e556654 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -222,6 +222,8 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. Also known as Power Law Transform. + This function transforms the input image pixelwise according to the + equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1. Parameters ---------- @@ -239,9 +241,6 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): Notes ----- - This function transforms the input image pixelwise according to the - equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1. - For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image. @@ -266,6 +265,10 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): def rescale_intensity_log(image, gain=1, inv=False): """Performs Logarithmic correction on the input image. + This function transforms the input image pixelwise according to the + equation ``O = gain*log(1 + I)`` after scaling each pixel to the range 0 to 1. + For inverse logarithmic correction, the equation is ``O = gain*(2**I - 1)``. + Parameters ---------- image : ndarray @@ -281,12 +284,6 @@ def rescale_intensity_log(image, gain=1, inv=False): out : ndarray Logarithm corrected output image. - Notes - ----- - This function transforms the input image pixelwise according to the - equation ``O = gain*log(1 + I)`` after scaling each pixel to the range 0 to 1. - For inverse logarithmic correction, the equation is ``O = gain*(2**I - 1)``. - References ---------- .. [1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf @@ -304,9 +301,12 @@ def rescale_intensity_log(image, gain=1, inv=False): def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): - """Performs Sigmoid Correction on input image. + """Performs Sigmoid Correction on the input image. Also known as Contrast Adjustment. + This function transforms the input image pixelwise according to the + equation ``O = 1/(1 + exp*(gain*(cutoff - I)))`` after scaling each pixel + to the range 0 to 1. Parameters ---------- @@ -325,12 +325,6 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): out : ndarray Sigmoid corrected output image. - Notes - ----- - This function transforms the input image pixelwise according to the - equation ``O = 1/(1 + exp*(gain*(cutoff - I)))`` after scaling each pixel - to the range 0 to 1. - References ---------- .. [1] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : @@ -339,7 +333,7 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): dtype = image.dtype.type scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) if inv == True: - out = 1 - (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale + out = (1 - 1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale return dtype(out) out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale return dtype(out) diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index bb542bc3..d8ba340a 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -183,14 +183,14 @@ if __name__ == '__main__': def test_rescale_intensity_gamma_one(): """Same image should be returned for gamma equal to one""" - image = np.random.random((8, 8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) result = exposure.rescale_intensity_gamma(image, 1) assert_array_equal(result, image) def test_rescale_intensity_gamma_zero(): """White image should be returned for gamma equal to zero""" - image = np.random.random((8, 8)) + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) result = exposure.rescale_intensity_gamma(image, 0) dtype = image.dtype.type assert_array_equal(result, dtype_range[dtype][1]) @@ -233,7 +233,7 @@ def test_rescale_intensity_gamma_greater_one(): # Test Logarithmic Correction # =========================== -def test_rescale_intensity_logarithmic(): +def test_rescale_intensity_log(): """Verifying the output with expected results for logarithmic correction with multiplier constant multiplier equal to unity""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -250,7 +250,7 @@ def test_rescale_intensity_logarithmic(): assert_array_equal(result, expected) -def test_rescale_intensity_inv_logarithmic(): +def test_rescale_intensity_inv_log(): """Verifying the output with expected results for inverse logarithmic correction with multiplier constant multiplier equal to unity""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -319,3 +319,20 @@ def test_rescale_intensity_sigmoid_cutoff_half(): result = exposure.rescale_intensity_sigmoid(image, 0.5, 10) assert_array_equal(result, expected) + + +def test_rescale_intensity_inv_sigmoid_cutoff_half(): + """Verifying the output with expected results for inverse sigmoid + correction with cutoff equal to half and gain of 10""" + image = np.arange(0, 255, 4, np.uint8).reshape(8,8) + expected = np.array([[253, 253, 252, 252, 251, 251, 250, 249], + [249, 248, 247, 245, 244, 242, 240, 238], + [235, 232, 229, 225, 220, 215, 210, 204], + [197, 190, 182, 174, 165, 155, 146, 136], + [126, 116, 106, 96, 87, 78, 70, 62], + [ 55, 49, 43, 37, 33, 28, 25, 21], + [ 18, 16, 14, 12, 10, 8, 7, 6], + [ 5, 4, 4, 3, 3, 2, 2, 1]], dtype=np.uint8) + + result = exposure.rescale_intensity_sigmoid(image, 0.5, 10, True) + assert_array_equal(result, expected) From 00ae24152533dbed0c2d3ab5a6576bad9afa4cd2 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 21 May 2013 13:00:49 +0800 Subject: [PATCH 10/13] Error handling for images with negative values in Image Correction methods --- skimage/exposure/exposure.py | 41 ++++++++++++++++++------- skimage/exposure/tests/test_exposure.py | 4 +-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 4e556654..95e5694d 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -2,7 +2,7 @@ import warnings import numpy as np from skimage import img_as_float -from skimage.util.dtype import dtype_range +from skimage.util.dtype import dtype_range, dtype_limits import skimage.color as color from skimage.util.dtype import convert from skimage._shared.utils import deprecated @@ -257,7 +257,13 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): if gamma < 0: return "Gamma should be a non-negative real number" - scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) + if np.any(image < 0): + raise ValueError('Image Correction methods work correctly only on ' + 'images with non-negative values. Use ' + 'skimage.exposure.rescale_intensity.') + else: + scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) + out = ((image / scale) ** gamma) * scale * gain return dtype(out) @@ -290,11 +296,17 @@ def rescale_intensity_log(image, gain=1, inv=False): """ dtype = image.dtype.type - scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - if inv == True: - out = (2 ** (image / scale) - 1) * scale * gain - return dtype(out) + if np.any(image < 0): + raise ValueError('Image Correction methods work correctly only on ' + 'images with non-negative values. Use ' + 'skimage.exposure.rescale_intensity.') + else: + scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) + + if inv == True: + out = (2 ** (image / scale) - 1) * scale * gain + return dtype(out) out = np.log2(1 + image / scale) * scale * gain return dtype(out) @@ -331,9 +343,16 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): """ dtype = image.dtype.type - scale = float(dtype_range[dtype][1] - dtype_range[dtype][0]) - if inv == True: - out = (1 - 1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale + + if np.any(image < 0): + raise ValueError('Image Correction methods work correctly only on ' + 'images with non-negative values. Use ' + 'skimage.exposure.rescale_intensity.') + else: + scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) + if inv == True: + out = (1 - 1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale + return dtype(out) + + out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale return dtype(out) - out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale - return dtype(out) diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index d8ba340a..354a007e 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -183,14 +183,14 @@ if __name__ == '__main__': def test_rescale_intensity_gamma_one(): """Same image should be returned for gamma equal to one""" - image = np.arange(0, 255, 4, np.uint8).reshape(8,8) + image = np.random.uniform(0, 255, (8, 8)) result = exposure.rescale_intensity_gamma(image, 1) assert_array_equal(result, image) def test_rescale_intensity_gamma_zero(): """White image should be returned for gamma equal to zero""" - image = np.arange(0, 255, 4, np.uint8).reshape(8,8) + image = np.random.uniform(0, 255, (8, 8)) result = exposure.rescale_intensity_gamma(image, 0) dtype = image.dtype.type assert_array_equal(result, dtype_range[dtype][1]) From 9a1c39f0890ce4a886fbd410c14c76f29a2cc6c5 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 27 May 2013 08:36:44 +0800 Subject: [PATCH 11/13] Adding a utility function to test non-negativity of an image --- skimage/exposure/exposure.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 95e5694d..c74d2a61 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -9,7 +9,8 @@ from skimage._shared.utils import deprecated __all__ = ['histogram', 'cumulative_distribution', 'equalize', - 'rescale_intensity'] + 'rescale_intensity', 'rescale_intensity_gamma', + 'rescale_intensity_log', 'rescale_intensity_sigmoid'] def histogram(image, nbins=256): @@ -218,6 +219,16 @@ def rescale_intensity(image, in_range=None, out_range=None): return dtype(image * (omax - omin) + omin) +def _assert_not_negative(image): + + if np.any(image < 0): + raise ValueError('Image Correction methods work correctly only on ' + 'images with non-negative values. Use ' + 'skimage.exposure.rescale_intensity.') + else: + return True + + def rescale_intensity_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. @@ -257,11 +268,7 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): if gamma < 0: return "Gamma should be a non-negative real number" - if np.any(image < 0): - raise ValueError('Image Correction methods work correctly only on ' - 'images with non-negative values. Use ' - 'skimage.exposure.rescale_intensity.') - else: + if _assert_not_negative(image): scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) out = ((image / scale) ** gamma) * scale * gain @@ -297,11 +304,7 @@ def rescale_intensity_log(image, gain=1, inv=False): """ dtype = image.dtype.type - if np.any(image < 0): - raise ValueError('Image Correction methods work correctly only on ' - 'images with non-negative values. Use ' - 'skimage.exposure.rescale_intensity.') - else: + if _assert_not_negative(image): scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) if inv == True: @@ -344,11 +347,7 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): """ dtype = image.dtype.type - if np.any(image < 0): - raise ValueError('Image Correction methods work correctly only on ' - 'images with non-negative values. Use ' - 'skimage.exposure.rescale_intensity.') - else: + if _assert_not_negative(image): scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) if inv == True: out = (1 - 1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale From f94f3db5979e5e770c6fc0db9ec239b7851074cc Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 27 May 2013 19:30:36 +0800 Subject: [PATCH 12/13] Renaming rescale_intensity_* as adjust_* --- skimage/exposure/exposure.py | 41 ++++++++++++------------- skimage/exposure/tests/test_exposure.py | 40 ++++++++++++------------ 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index c74d2a61..6a6430e2 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -9,8 +9,8 @@ from skimage._shared.utils import deprecated __all__ = ['histogram', 'cumulative_distribution', 'equalize', - 'rescale_intensity', 'rescale_intensity_gamma', - 'rescale_intensity_log', 'rescale_intensity_sigmoid'] + 'rescale_intensity', 'adjust_gamma', + 'adjust_log', 'adjust_sigmoid'] def histogram(image, nbins=256): @@ -219,7 +219,7 @@ def rescale_intensity(image, in_range=None, out_range=None): return dtype(image * (omax - omin) + omin) -def _assert_not_negative(image): +def _assert_non_negative(image): if np.any(image < 0): raise ValueError('Image Correction methods work correctly only on ' @@ -229,7 +229,7 @@ def _assert_not_negative(image): return True -def rescale_intensity_gamma(image, gamma=1, gain=1): +def adjust_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. Also known as Power Law Transform. @@ -263,19 +263,19 @@ def rescale_intensity_gamma(image, gamma=1, gain=1): .. [1] http://en.wikipedia.org/wiki/Gamma_correction """ + _assert_non_negative(image) dtype = image.dtype.type if gamma < 0: return "Gamma should be a non-negative real number" - if _assert_not_negative(image): - scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) + scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) out = ((image / scale) ** gamma) * scale * gain return dtype(out) -def rescale_intensity_log(image, gain=1, inv=False): +def adjust_log(image, gain=1, inv=False): """Performs Logarithmic correction on the input image. This function transforms the input image pixelwise according to the @@ -302,20 +302,19 @@ def rescale_intensity_log(image, gain=1, inv=False): .. [1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf """ + _assert_non_negative(image) dtype = image.dtype.type + scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) - if _assert_not_negative(image): - scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) - - if inv == True: - out = (2 ** (image / scale) - 1) * scale * gain - return dtype(out) + if inv == True: + out = (2 ** (image / scale) - 1) * scale * gain + return dtype(out) out = np.log2(1 + image / scale) * scale * gain return dtype(out) -def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): +def adjust_sigmoid(image, cutoff=0.5, gain=10, inv=False): """Performs Sigmoid Correction on the input image. Also known as Contrast Adjustment. @@ -345,13 +344,13 @@ def rescale_intensity_sigmoid(image, cutoff=0.5, gain=10, inv=False): .. [1] http://bme.med.upatras.gr/improc/matalb_code_toc.htm#12. Adjust Contrast : """ + _assert_non_negative(image) dtype = image.dtype.type + scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) - if _assert_not_negative(image): - scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) - if inv == True: - out = (1 - 1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale - return dtype(out) - - out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale + if inv == True: + out = (1 - 1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale return dtype(out) + + out = (1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale + return dtype(out) diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py index 354a007e..2b696b32 100644 --- a/skimage/exposure/tests/test_exposure.py +++ b/skimage/exposure/tests/test_exposure.py @@ -181,22 +181,22 @@ if __name__ == '__main__': # Test Gamma Correction # ===================== -def test_rescale_intensity_gamma_one(): +def test_adjust_gamma_one(): """Same image should be returned for gamma equal to one""" image = np.random.uniform(0, 255, (8, 8)) - result = exposure.rescale_intensity_gamma(image, 1) + result = exposure.adjust_gamma(image, 1) assert_array_equal(result, image) -def test_rescale_intensity_gamma_zero(): +def test_adjust_gamma_zero(): """White image should be returned for gamma equal to zero""" image = np.random.uniform(0, 255, (8, 8)) - result = exposure.rescale_intensity_gamma(image, 0) + result = exposure.adjust_gamma(image, 0) dtype = image.dtype.type assert_array_equal(result, dtype_range[dtype][1]) -def test_rescale_intensity_gamma_less_one(): +def test_adjust_gamma_less_one(): """Verifying the output with expected results for gamma correction with gamma equal to half""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -209,11 +209,11 @@ def test_rescale_intensity_gamma_less_one(): [221, 223, 225, 228, 230, 232, 234, 236], [238, 241, 243, 245, 247, 249, 251, 253]], dtype=np.uint8) - result = exposure.rescale_intensity_gamma(image, 0.5) + result = exposure.adjust_gamma(image, 0.5) assert_array_equal(result, expected) -def test_rescale_intensity_gamma_greater_one(): +def test_adjust_gamma_greater_one(): """Verifying the output with expected results for gamma correction with gamma equal to two""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -226,14 +226,14 @@ def test_rescale_intensity_gamma_greater_one(): [144, 150, 156, 163, 169, 176, 182, 189], [196, 203, 211, 218, 225, 233, 241, 249]], dtype=np.uint8) - result = exposure.rescale_intensity_gamma(image, 2) + result = exposure.adjust_gamma(image, 2) assert_array_equal(result, expected) # Test Logarithmic Correction # =========================== -def test_rescale_intensity_log(): +def test_adjust_log(): """Verifying the output with expected results for logarithmic correction with multiplier constant multiplier equal to unity""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -246,11 +246,11 @@ def test_rescale_intensity_log(): [206, 209, 213, 216, 219, 222, 225, 228], [231, 234, 238, 241, 244, 246, 249, 252]], dtype=np.uint8) - result = exposure.rescale_intensity_log(image, 1) + result = exposure.adjust_log(image, 1) assert_array_equal(result, expected) -def test_rescale_intensity_inv_log(): +def test_adjust_inv_log(): """Verifying the output with expected results for inverse logarithmic correction with multiplier constant multiplier equal to unity""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -263,14 +263,14 @@ def test_rescale_intensity_inv_log(): [174, 179, 184, 188, 193, 198, 203, 208], [213, 218, 224, 229, 234, 239, 245, 250]], dtype=np.uint8) - result = exposure.rescale_intensity_log(image, 1, True) + result = exposure.adjust_log(image, 1, True) assert_array_equal(result, expected) # Test Sigmoid Correction # ======================= -def test_rescale_intensity_sigmoid_cutoff_one(): +def test_adjust_sigmoid_cutoff_one(): """Verifying the output with expected results for sigmoid correction with cutoff equal to one and gain of 5""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -283,11 +283,11 @@ def test_rescale_intensity_sigmoid_cutoff_one(): [ 57, 61, 64, 68, 72, 76, 80, 85], [ 89, 94, 99, 104, 108, 113, 118, 123]], dtype=np.uint8) - result = exposure.rescale_intensity_sigmoid(image, 1, 5) + result = exposure.adjust_sigmoid(image, 1, 5) assert_array_equal(result, expected) -def test_rescale_intensity_sigmoid_cutoff_zero(): +def test_adjust_sigmoid_cutoff_zero(): """Verifying the output with expected results for sigmoid correction with cutoff equal to zero and gain of 10""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -300,11 +300,11 @@ def test_rescale_intensity_sigmoid_cutoff_zero(): [254, 254, 254, 254, 254, 254, 254, 254], [254, 254, 254, 254, 254, 254, 254, 254]], dtype=np.uint8) - result = exposure.rescale_intensity_sigmoid(image, 0, 10) + result = exposure.adjust_sigmoid(image, 0, 10) assert_array_equal(result, expected) -def test_rescale_intensity_sigmoid_cutoff_half(): +def test_adjust_sigmoid_cutoff_half(): """Verifying the output with expected results for sigmoid correction with cutoff equal to half and gain of 10""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -317,11 +317,11 @@ def test_rescale_intensity_sigmoid_cutoff_half(): [236, 238, 240, 242, 244, 246, 247, 248], [249, 250, 250, 251, 251, 252, 252, 253]], dtype=np.uint8) - result = exposure.rescale_intensity_sigmoid(image, 0.5, 10) + result = exposure.adjust_sigmoid(image, 0.5, 10) assert_array_equal(result, expected) -def test_rescale_intensity_inv_sigmoid_cutoff_half(): +def test_adjust_inv_sigmoid_cutoff_half(): """Verifying the output with expected results for inverse sigmoid correction with cutoff equal to half and gain of 10""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) @@ -334,5 +334,5 @@ def test_rescale_intensity_inv_sigmoid_cutoff_half(): [ 18, 16, 14, 12, 10, 8, 7, 6], [ 5, 4, 4, 3, 3, 2, 2, 1]], dtype=np.uint8) - result = exposure.rescale_intensity_sigmoid(image, 0.5, 10, True) + result = exposure.adjust_sigmoid(image, 0.5, 10, True) assert_array_equal(result, expected) From 04625ebabf163c831492fbda770828c47fb7d6c1 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 10 Jun 2013 00:32:22 +0800 Subject: [PATCH 13/13] Final stylistic changes in exposure.py --- skimage/exposure/exposure.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py index 6a6430e2..a26e5630 100644 --- a/skimage/exposure/exposure.py +++ b/skimage/exposure/exposure.py @@ -3,8 +3,6 @@ import numpy as np from skimage import img_as_float from skimage.util.dtype import dtype_range, dtype_limits -import skimage.color as color -from skimage.util.dtype import convert from skimage._shared.utils import deprecated @@ -225,8 +223,6 @@ def _assert_non_negative(image): raise ValueError('Image Correction methods work correctly only on ' 'images with non-negative values. Use ' 'skimage.exposure.rescale_intensity.') - else: - return True def adjust_gamma(image, gamma=1, gain=1): @@ -306,7 +302,7 @@ def adjust_log(image, gain=1, inv=False): dtype = image.dtype.type scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) - if inv == True: + if inv: out = (2 ** (image / scale) - 1) * scale * gain return dtype(out) @@ -332,8 +328,9 @@ def adjust_sigmoid(image, cutoff=0.5, gain=10, inv=False): gain : float The constant multiplier in exponential's power of sigmoid function. Default value is 10. - inv : If True, returns the negative sigmoid correction. Defaults to - False. + inv : bool + If True, returns the negative sigmoid correction. Defaults to False. + Returns ------- out : ndarray @@ -348,7 +345,7 @@ def adjust_sigmoid(image, cutoff=0.5, gain=10, inv=False): dtype = image.dtype.type scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) - if inv == True: + if inv: out = (1 - 1 / (1 + np.exp(gain * (cutoff - image/scale)))) * scale return dtype(out)