Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

how to change image illumination in opencv python

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • To display image on a screen, input voltage is needed.
  • This voltage is output as light intensity.
  • In perfect world, input voltage would be linear to output intensity.
  • But the real screen output is close to an exponential curve, the exponent being gamma.

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.

enter image description here

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:

enter image description here

Applying gamma of value 0.5 will yield:

enter image description here

Applying gamma of value 1.5 will yield:

enter image description here

Applying gamma of value 2.5 will yield:

enter image description here

Applying gamma of value 1.0 will yield the same image.

Code was borrowed from this link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...