Latest YouTube Video

Monday, October 5, 2015

OpenCV Gamma Correction

gamma_correction_animated

Did you know that the human eye perceives color and luminance differently than the sensor on your smartphone or digital camera?

You see, when twice the number of photons hit the sensor of a digital camera, it receives twice the signal (a linear relationship). However, that’s not how our human eyes work. Instead, we perceive double the amount of light as only a fraction brighter (a non-linear relationship)! Furthermore, our eyes are also much more sensitive to changes in dark tones than brighter tones (another non-linear relationship).

In order to account for this we can apply gamma correction, a translation between the sensitivity of our eyes and sensors of a camera.

Looking for the source code to this post?
Jump right to the downloads section.

In the remainder of this post I’ll demonstrate how you can implement a super fast, dead-simple gamma correction function using Python and OpenCV.

Gamma correction and the Power Law Transform

Gamma correction is also known as the Power Law Transform. First, our image pixel intensities must be scaled from the range [0, 255] to [0, 1.0]. From there, we obtain our output gamma corrected image by applying the following equation:

O = I ^ (1 / G)

Where I is our input image and G is our gamma value. The output image O is then scaled back to the range [0, 255].

Gamma values < 1 will shift the image towards the darker end of the spectrum while gamma values > 1 will make the image appear lighter. A gamma value of G=1 will have no affect on the input image:

Figure 1: Our original image (left); Gamma correction with G 1 (right), this time the output image is much lighter than the original.

Figure 1: Our original image (left); Gamma correction with G < 1 (center), notice how the gamma adjusted image is much darker than the original image; Gamma correction with G > 1 (right), this time the output image is much lighter than the original.

OpenCV Gamma Correction

Now that we understand what gamma correction is, let’s use OpenCV and Python to implement it. Open up a new file, name it

adjust_gamma.py
 , and we’ll get started:
# import the necessary packages
from __future__ import print_function
import numpy as np
import argparse
import cv2

def adjust_gamma(image, gamma=1.0):
        # build a lookup table mapping the pixel values [0, 255] to
        # their adjusted gamma values
        invGamma = 1.0 / gamma
        table = np.array([((i / 255.0) ** invGamma) * 255
                for i in np.arange(0, 256)]).astype("uint8")

        # apply gamma correction using the lookup table
        return cv2.LUT(image, table)

Lines 2-5 simply import our necessary packages, nothing special here.

We define our

adjust_gamma
  function on Line 7. This method requires a single parameter,
image
 , which is the image we want to apply gamma correction to. A second (optional) value is our
gamma
  value. In this case, we default
gamma=1.0
 , but you should supply whatever value is necessary to obtain a decent looking corrected image.

There are two (easy) ways to apply gamma correction using OpenCV and Python. The first method is to simply leverage the fact that Python + OpenCV represents images as NumPy arrays. All we need to do is scale the pixel intensities to the range [0, 1.0], apply the transform, and then scale back to the range [0, 255]. Overall, the NumPy approach involves a division, raising to a power, followed by a multiplication — this tends to be very fast since all these operations are vectorized.

However, there is an even faster way to perform gamma correction thanks to OpenCV. All we need to do is build a table (i.e. dictionary) that maps the input pixel values to the output gamma corrected values. OpenCV can then take this table and quickly determine the output value for a given pixel in O(1) time.

For example, here is an example lookup table for

gamma=1.2
 :
0 => 0
1 => 2
2 => 4
3 => 6
4 => 7
5 => 9
6 => 11
7 => 12
8 => 14
9 => 15
10 => 17

The left column is the input pixel value while the right column is the output pixel value after applying the power law transform.

Lines 11 and 12 build this lookup table by looping over all pixel values in the range [0, 255]. The pixel value is then scaled to the range [0, 1.0] followed by being raised to the power of the inverse gamma — this value is then stored in the

table
 .

Lastly, all we need to do is apply the

cv2.LUT
  function (Line 15) to take the input
image
  and the
table
  and find the correct mappings for each pixel value — it’s a simple (and yet very speedy) operation!

Let’s continue on with our example:

# import the necessary packages
from __future__ import print_function
import numpy as np
import argparse
import cv2

def adjust_gamma(image, gamma=1.0):
        # build a lookup table mapping the pixel values [0, 255] to
        # their adjusted gamma values
        invGamma = 1.0 / gamma
        table = np.array([((i / 255.0) ** invGamma) * 255
                for i in np.arange(0, 256)]).astype("uint8")

        # apply gamma correction using the lookup table
        return cv2.LUT(image, table)

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
        help="path to input image")
args = vars(ap.parse_args())

# load the original image
original = cv2.imread(args["image"])

Lines 17-21 handle parsing command line arguments. We only need a single switch here,

--image
 , which is the path to where our input image resides on disk. Line 24 takes the path to our image and loads it.

Let’s explore gamma correction by using a variety of gamma values and inspecting the output image for each:

# import the necessary packages
from __future__ import print_function
import numpy as np
import argparse
import cv2

def adjust_gamma(image, gamma=1.0):
        # build a lookup table mapping the pixel values [0, 255] to
        # their adjusted gamma values
        invGamma = 1.0 / gamma
        table = np.array([((i / 255.0) ** invGamma) * 255
                for i in np.arange(0, 256)]).astype("uint8")

        # apply gamma correction using the lookup table
        return cv2.LUT(image, table)

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
        help="path to input image")
args = vars(ap.parse_args())

# load the original image
original = cv2.imread(args["image"])

# loop over various values of gamma
for gamma in np.arange(0.0, 3.5, 0.5):
        # ignore when gamma is 1 (there will be no change to the image)
        if gamma == 1:
                continue

        # apply gamma correction and show the images
        gamma = gamma if gamma > 0 else 0.1
        adjusted = adjust_gamma(original, gamma=gamma)
        cv2.putText(adjusted, "g={}".format(gamma), (10, 30),
                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
        cv2.imshow("Images", np.hstack([original, adjusted]))
        cv2.waitKey(0)

On Line 27 we start by looping over

gamma
  values in the range [0, 3.0] (the
np.arange
  function is non-inclusive), incrementing by 0.5 at each step.

In the case that our

gamma
  value is 1.0, we simply ignore it (Lines 29 and 30) since
gamma=1.0
  will not change our input image.

From there, Lines 33-38 apply gamma correction to our image and display the output result.

To see gamma correction in action, just open up a terminal and execute the following command:

$ python adjust_gamma.py --image example_01.png
Figure 2: When applying gamma correction with G < 1, the output image is will darker than the original input image.

Figure 2: When applying gamma correction with G < 1, the output image is will darker than the original input image.

Notice for

gamma=0.5
  that the gamma corrected image (right) is substantially darker than the input image (left) which is already quite dark — we can barely see any detail on the dog’s face in the original yet, let alone the gamma corrected version!

However, at

gamma=1.5
  the image starts to lighten up and we can see more detail:
Figure 3: As the gamma value reaches 1.0 and starts to exceed it, the image lightens up and we can see more detail.

Figure 3: As the gamma value reaches 1.0 and starts to exceed it, the image lightens up and we can see more detail.

By the time we reach

gamma=2.0
 , the details in the image are fully visible.
Figure 4: Now at gamma=2.0, we can fully see the details on the dogs face.

Figure 4: Now at gamma=2.0, we can fully see the details on the dogs face.

Although at

gamma=2.5
 , the image starts to appear “washed out”:
Figure 5: However, if we carry gamma correction too far, the image will start to appear washed out.

Figure 5: However, if we carry gamma correction too far, the image will start to appear washed out.

Let’s give another image a try:

$ python adjust_gamma.py --image example_02.png
Figure 6: After applying gamma correction with gamma=0.5, we cannot see any detail in this image.

Figure 6: After applying gamma correction with gamma=0.5, we cannot see any detail in this image.

Just like in

example_01.png
 , a gamma value of 0.5 makes the input image appear darker than it already is. We can’t really make out any detail in this image, other than there is sky and what appears to be a mountain range.

However, this changes when we apply gamma correction with

gamma=1.5
 :
Figure 7: Optimal results are obtained near gamma=1.5.

Figure 7: Optimal results are obtained near gamma=1.5.

Now we can see that the image has become much lighter — we can even start to see there are trees in the foreground, something that is not entirely apparent from the original input image on the left.

At

gamma=2.0
  the image starts to appear washed out, but again, the difference between the original image and the gamma corrected image is quite substantial:
Figure 8: But again, we can carry gamma correction too far and washout our image.

Figure 8: But again, we can carry gamma correction too far and washout our image.

Summary

In this blog post we learned about gamma correction, also called the Power Law Transform. We then implemented gamma correction using Python and OpenCV.

The reason we apply gamma correction is because our eyes perceive color and luminance differently than the sensors in a digital camera. When a sensor on a digital camera picks up twice the amount of photons, the signal is doubled. However, our eyes do not work like this. Instead, our eyes perceive double the amount of light as only a fraction brighter. Thus, while a digital camera has a linear relationship between brightness our eyes have a non-linear relationship. In order to account for this relationship we apply gamma correction.

Be sure to download the code to this post and try applying gamma correction to your own photos. Try to go through your photo collection and find images that are either excessively dark or very bright and washed out. Then perform gamma correction on these images and see if they become more visually appealing.

Downloads:

If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 11-page Resource Guide on Computer Vision and Image Search Engines, including exclusive techniques that I don’t post on this blog! Sound good? If so, enter your email address and I’ll send you the code immediately!

The post OpenCV Gamma Correction appeared first on PyImageSearch.



from PyImageSearch http://ift.tt/1KVVEkA
via IFTTT

No comments: