Added a function for Gamma Correction

This commit is contained in:
Ankit Agrawal
2013-04-16 06:38:11 +05:30
parent 43bb55c6dd
commit 391b2edad6
3 changed files with 73 additions and 2 deletions
+3 -1
View File
@@ -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']
+40 -1
View File
@@ -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
+30
View File
@@ -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()