I tried "gamma correction" of the image with Python + OpenCV

Introduction

Image processing does not always provide beautiful images. It may contain dark objects or, conversely, images that are too bright. There is a method called gamma correction as a method of adjusting the brightness of an image.

This time, we will use Python to perform gamma correction of the image by OpenCV.

What is gamma correction?

** Gamma correction (or gamma conversion) ** is simply ** a method of adjusting the brightness of an image **.

The image generally contains dark and bright areas. In such a case, the feature of gamma correction is not to adjust the brightness at the same ratio for the entire image, but to adjust the brightness according to each pixel value. The gamma correction formula is as follows.

y = 255\times\Bigl(\frac{x}{255}\Bigr)^{1/\gamma}

x is the input pixel value and y is the output pixel value. It is called gamma correction because the output changes depending on the value of $ \ gamma $ (gamma). If $ \ gamma $ is greater than 1, it will be brighter, and if it is less than 1, it will be darker.

The graph of gamma correction is as follows.

image.png

Preparation

The environment uses Google Colaboratory. The Python version is below.

import platform
print("python " + platform.python_version())
# python 3.6.9

Let's display the image

Now let's write the code.

First, import OpenCV.

import cv2

In addition, import the following to display the image in Colaboratory.

from google.colab.patches import cv2_imshow

Prepare a sample image as well. This time, we will use the free image from Pixabay.

Now, let's display the prepared sample image.

img = cv2.imread(path) #path specifies where the image is placed
cv2_imshow(img)

image.png

Now let's brighten the image using gamma correction. Create a gamma correction function in advance.

import numpy as np

def create_gamma_img(gamma, img):
  gamma_cvt = np.zeros((256,1), dtype=np.uint8)
  for i in range(256):
    gamma_cvt[i][0] = 255*(float(i)/255)**(1.0/gamma)
  return cv2.LUT(img, gamma_cvt)

Let's display the gamma-corrected image side by side with the original image.

img_gamma = create_gamma_img(2, img)
imgs = cv2.hconcat([img, img_gamma])
cv2_imshow(imgs)

image.png

Make $ \ gamma $ greater than 1 to make it brighter. I tried it as 2 above.

Now, let's change the value of $ \ gamma $ and display the gamma-corrected image.

img1 = create_gamma_img(0.33, img)
img2 = create_gamma_img(0.5, img)
img3 = create_gamma_img(0.66, img)
img4 = create_gamma_img(1.5, img)
img5 = create_gamma_img(2, img)
img6 = create_gamma_img(3, img)
imgs_1 = cv2.hconcat([img1, img2, img3])
imgs_2 = cv2.hconcat([img4, img5, img6])
imgs = cv2.vconcat([imgs_1, imgs_2])
cv2_imshow(imgs)

image.png

From the upper left, the value of $ \ gamma $ was increased. The upper row is darker than the original image, and the lower row is brighter than the original image.

Summary

This time, I used Python to perform gamma correction (gamma conversion) on the image using OpenCV.

If you need to adjust the brightness of the image, try gamma correction.

For more details on gamma correction (gamma conversion), refer to the following.

-(99) OpenCV # 4: Adjust the image to make it easier to see with gamma correction

Recommended Posts

I tried "gamma correction" of the image with Python + OpenCV
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to find the entropy of the image with python
I tried using the image filter of OpenCV
I tried "morphology conversion" of images with Python + OpenCV
I tried non-photorealistic rendering with Python + opencv
I tried playing with the image with Pillow
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
I tried to improve the efficiency of daily work with Python
I tried to make an image similarity function with Python + OpenCV
I tried hundreds of millions of SQLite with python
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried to correct the keystone of the image
[Python + OpenCV] Whiten the transparent part of the image
How to crop the lower right part of the image with Python OpenCV
I tried to get the authentication code of Qiita API with Python.
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried fp-growth with python
I tried scraping with Python
Image editing with python OpenCV
I tried gRPC with Python
I tried scraping with python
Extract the table of image files with OneDrive & Python
I tried to extract features with SIFT of OpenCV
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to solve the problem with Python Vol.1
Python OpenCV tried to display the image in text.
I tried hitting the API with echonest's python client
I tried to summarize the string operations of Python
[Python] I tried to judge the member image of the idol group using Keras
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I want to crop the image along the contour instead of the rectangle [python OpenCV]
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to simulate how the infection spreads with Python
I tried the accuracy of three Stirling's approximations in python
[Python] Using OpenCV with Python (Image Filtering)
I tried using the Python library from Ruby with PyCall
I tried to find the average of the sequence with TensorFlow
I tried trimming efficiently with OpenCV
[Python] Using OpenCV with Python (Image transformation)
I wrote the basic grammar of Python with Jupyter Lab
I tried to put out the frequent word ranking of LINE talk with Python
I tried web scraping with python.
I tried running Movidius NCS with python of Raspberry Pi3
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried using GrabCut of OpenCV
I evaluated the strategy of stock system trading with Python.
I tried face recognition from the video (OpenCV: python version)
[Python] I tried to visualize the follow relationship of Twitter
I liked the tweet with python. ..
[Python] I tried collecting data using the API of wikipedia
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
Find image similarity with Python + OpenCV