From 391b2edad6f12116191bb7bb27f86ffb787549ac Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 16 Apr 2013 06:38:11 +0530 Subject: [PATCH] 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()