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.
** 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.
The environment uses Google Colaboratory. The Python version is below.
import platform
print("python " + platform.python_version())
# python 3.6.9
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)
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)
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)
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.
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