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.
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:
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:
# 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:
# 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
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:
By the time we reach gamma=2.0
, the details in the image are fully visible.
Although at gamma=2.5
, the image starts to appear “washed out”:
Let’s give another image a try:
$ python adjust_gamma.py --image example_02.png
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
:
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:
What's next? I recommend PyImageSearch University.
30+ total classes • 39h 44m video • Last updated: 12/2021
★★★★★ 4.84 (128 Ratings) • 3,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 30+ courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 30+ Certificates of Completion
- ✓ 39h 44m on-demand video
- ✓ Brand new courses released every month, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 500+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
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.
Download the Source Code and FREE 17-page Resource Guide
Enter your email address below to get a .zip of the code and a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!
Hello Adrian,
Your Blog posts has got me very interested in OpenCV. Keep posting!
However, I think there is an mistake in the above code, Line 11 and 12 are in each other’s place.
Hey Abhinav — Lines 12 and 12 are actually a single statement that wrap two lines of code.
Is there any way to perform localized gamma correction? For example, in the picture with dog, is there a function that would gamma correct (lighten) the areas that are in the shadows but would essentially ignore the areas that are in the sun since they are already bright?
This might help to “equalize” the picture (for lack of the correct word). Once the picture is equalized, other OpenCV functions could the be used to detect things without having to worry about bright and dark regions negatively impacting the results. What do you think?
Hey Brian — localized Gamma correction is an active area of research. I know there was a paper written about it a few years ago. Otherwise, OpenCV does implement Contrast Limited Adaptive Histogram Equalization (CLAHE) which helps in adjusting local areas of contrast.
Hello Adrian.
First of all, I’d like to thank you for all support that your blog is giving to me. Thus, I’d like to know if you have some explanation about image filters, like the Difference of Gaussians and Histogram Equalization.
Thank you again.
Best Regards
Hey Rossi, thanks for the comment. I discuss Difference of Gaussians and Histogram Equalization in quite a bit of detail inside the PyImageSearch Gurus course. I would suggest starting there.
Thank you Adrian.
Hi Adrian,
Consequent question 🙂
How to do opposite? How to know whether it is necessary to increase or decrease level of gamma? How to know a level of gamma?
In this case you normally wouldn’t unless you have calibrated your camera using a color filter. Gamma correction is almost always a post-processing step after the image has already been captured.
Sorry, I was not clear with my question.
I was talking about some functionality which normalize gamma before pass image for further processing. In your solution you show how to change gamma assuming that client already knows what to do. But in order to know what to do (increase or decrease) it is necessary to analyze image somehow … The question is “how” ?
You would need to visually inspect the image. This is how graphic designers and photographers edit an image. They capture an image, open it in Photoshop (or whatever other photo editing software) and adjust any gamma levels they see fit.
If you were deploying a computer vision product you would inspect the environment and captured images, then build in the gamma correction to the program.
How to save the new image has changed
You can use the
cv2.imwrite
function to write an image to file:cv2.imwrite("foo.png", image)
You can learn more about the basics of OpenCV and computer vision inside Practical Python and OpenCV.
Hi, I like your explanations very clear.
I am doing projects in opencv and python but totally new to it.
Your posts are really helpful.
Thank you.
Thanks Irene — I’m happy to hear you’ve found the PyImageSearch blog helpful 🙂 If you are new to OpenCV and computer vision, be sure to take a look at Practical Python and OpenCV. This book is designed to help beginners learn the fundamentals of OpenCV quickly.
Hi,
Great Post! But when I try to save the Gamma corrected output of the image, it saves both images together in one image just as it is shown above. What changes should I make in the code to display as well as save only the output image?
Remove the
np.hstack
call:cv2.imwrite("output.png", adjusted)
Thanks for this! It is helping our students with a project.
Do you have any recommended articles on color correction / white balance?
Students are considering LUT on an RGB / HSV split, then a merge. The tables would have to be a bit different.
Thanks again!
I’m glad to hear my tutorials are helping your students, that’s great!
I do not have any tutorials on color correction or white balancing. I will try to cover these subjects in the future. The gist is that you need to compute the CDF of both your input and reference images, then transfer the CDF to the input image to correct for the white balance.
hello
how i can i apply gamma technique to gray scale filtered image
You can still use the method in this blog post.
hello
the above code is showing error pls help
the error is as follows:
check.py: error: the following arguments are required: -i/–fogimage
You can resolve this error by reading up on command line arguments.
on command line it is showing error
unrecognized argument : example1.png
pls resolve
I believe your error is due to not utilizing the command line arguments properly. Make sure you read this post.
sir i have downloaded ur code nd apply with my images but it is showing error of unrecognized argument
What is the exact error you are receiving?
if possible pls send me code which is applicable to gray images
Is there any posts which will analyse the image and set a gamma value based on that,rather than user defining it
Hi Adrian
I really liked your explanation. My only doubt is why the lookup table method is faster than the first method?
Which first method are you referring to? As to why a LUT is fast see this sentence:
“OpenCV can then take this table and quickly determine the output value for a given pixel in O(1) time.”
Hello Adrian is it a best method for the correction of images taken at night using cameras
What type of camera are you using? Some cameras include a “smart IR” method that can help perform correction at capture time which would probably be more ideal than trying to perform gamma correction manually.
Is there any posts which will analyse the image and set a gamma value based on that,rather than user defining it
No, I do not have any tutorials on automatically setting the gamma value.
hi
Why did you use inverse gamma instead of applying the normal gamma?
thanks
Hi, is there a way to do this for webcams?
Yes, you can follow this tutorial to access your webcam with OpenCV.
Hello sir, can we do gamma correction to the video stream ? then how to do that ?
You would access your video stream and then apply gamma correction to each individual frame. If you’ve never accessed a video stream before, be sure to refer to Practical Python and OpenCV where I cover the topic in detail.
Hi Dr, is there any ways for us to do auto gamma correction? For example, when the image is too bright, it will make it darker and when the image is too dark, then it will make it brighter?
Hi,
I also need this for my code.Auto correction of gamma. If you find the solution please help me as well.
My use case is I am taking images in the night and so they are dark. I need mechanism to detect if the images are being taken in the dark and auto correct with out any human intervention.
Hi Adrian,
Thanks for the post and your comments reply for different problem which gives kick to my geek of learning OpenCV. But I my domain is C++. If you have any tutorial/Blog based on C++, that will be great for me.
Thanks again
Best Regards
Sorry, I only provide Python tutorials on PyImageSearch.
I was looking for gamma correction but I always ended up on brightness/contrast settings in python. This code seems to work neatly. Thanks for this.
Also, do you know how we can find the image quality score of a printed/written text document image? I tried BRISQUE approach for Opencv and Python, but it seems to not work in this case.. Any suggestions would help 🙂
I have added GAMMA , CLAHE and histogram equalization to as post processing options on my time lapse project. I(Whi works greats and provides amazing videos, especially when contrast corrected)
My question is on how cv2.LUT(image, table) is doing. It appears that the image is converted to a color space where the grey scale is remapped through the computed GAMMA array and then the image is restored to RGB. Is this all being carried out in the LUT object? In any case I get great results.
Is there anyway to know what the gamma value of an image is..?
In your solution you show how to change gamma assuming that client already knows what to do. But in order to know what to do (increase or decrease) it is necessary to analyze image somehow…
Based on gamma , we can make condition which value of gamma suits our case.