I am reading a image in python opencv, now I need to change the illumination on this image to be darker or lighter, what kind of method I should use to enable this?
Increasing the Brightness That means the current value of a pixel will be (B. G, R). To increase the brightness, we have to add some scalar number with it such as (B, G, R) + (10, 10, 10) or (B, G, R) + (20, 20, 20) or whatever number you want.
To change the contrast, multiply the pixel values with some constant. For example, if multiply all the pixel values of an image by 2, then the pixel's value will be doubled, and the image will look sharper.
This change can be done by either multiplying or dividing (means to multiply each pixel with value < 1) the pixel values of the image, by any constant. To increase the contrast levels of the image, simply multiply a constant positive value to each and every image pixel.
I know I am late, but I would suggest using gamma correction.
Now what is gamma correction?
I will make it clear in layman's terms:
Since the computer screen applies a gamma value to the image on screen, the process of applying inverse gamma to counter this effect is called gamma correction.
Here is the code for the same using OpenCV 3.0.0 and python:
import cv2
import numpy as np
def adjust_gamma(image, gamma=1.0):
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
x = 'C:/Users/524316/Desktop/stack/test.jpg' #location of the image
original = cv2.imread(x, 1)
cv2.imshow('original',original)
gamma = 0.5 # change the value here to get different result
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("gammam image 1", adjusted)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here is the original image:
Applying gamma of value 0.5 will yield:
Applying gamma of value 1.5 will yield:
Applying gamma of value 2.5 will yield:
Applying gamma of value 1.0 will yield the same image.
Code was borrowed from this link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With