How to change image highlighting in opencv python

I am reading an image in python opencv, now I need to change the highlight on this image darker or easier, which method should I use to enable this?

+6
source share
3 answers

I think you can do it with opencv. Here is my suggestion

import cv2 import numpy as np img1 = cv2.imread('abc.jpg') a = np.double(img1) b = a + 15 img2 = np.uint8(b) cv2.imshow("frame",img1) cv2.imshow("frame2",img2) cv2.waitKey(0) cv2.destroyAllWindows() 

Here I increased the brightness of the image. If you use subtraction, which will become darker.

+4
source

I know I'm late, but I would suggest using gamma correction .

Now what is gamma correction ?

I will explain in terms of layman:

  • An input voltage is required to display the image on the screen.
  • This voltage is output as light intensity.
  • In an ideal world, the input voltage will be linear with respect to the output intensity.
  • But the real conclusion to the screen is close to an exponential curve, gamma .

Because the computer screen applies the gamma value to the image on the screen, the process of applying inverse gamma to counteract 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 a gamma with a value of 0.5 will give:

enter image description here

Applying a gamma with a value of 1.5 will give:

enter image description here

Applying a gamma with a value of 2.5 will give:

enter image description here

Applying a gamma value of 1.0 will result in the same image.

The code was borrowed from this link.

+15
source

A short remark complementary to the answer to Jer Luke. Make sure both arrays are of type np.uint8 . The name of the function cv.LUT means "lookup table". This means that every pixel from image is replaced by a value from table .

You can convert both arrays:

 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)]) return cv2.LUT(image.astype(np.uint8), table.astype(np.uint8)) 

Or make sure that the image array is cast to a valid type before passing it to the adjust_gamma() function. The image is easy to convert to float when applying various transformations and forget to restore the actual type before adjusting the gamma.

+3
source

Source: https://habr.com/ru/post/1262183/


All Articles