Python OpencV adjusts image brightness and contrast
Degree adjustment is to increase/decrease the overall intensity of the image pixels. Contrast adjustment refers to the image becoming darker in the dark and brighter in the light, thus widening the display accuracy in a certain area. Create two sliders to adjust contrast and brightness respectively (contrast range: 0 to 0.3, brightness 0 to 100). Tip: Since the slider doesn’t have decimals, you can set it to 0 ~ 300 and multiply it by 0.01
code
import cv2
import numpy as np
alpha = 0.3
beta = 80
img_path = "7MeansDenoising/1_1.bmp"
img = cv2.imread(img_path)
img2 = cv2.imread(img_path)
def updateAlpha(x) :
global alpha, img, img2
alpha = cv2.getTrackbarPos('Alpha'.'image')
alpha = alpha * 0.01
img = np.uint8(np.clip((alpha * img2 + beta), 0.255))
def updateBeta(x) :
global beta, img, img2
beta = cv2.getTrackbarPos('Beta'.'image')
img = np.uint8(np.clip((alpha * img2 + beta), 0.255))
Create window
cv2.namedWindow('image')
cv2.createTrackbar('Alpha'.'image'.0.300, updateAlpha)
cv2.createTrackbar('Beta'.'image'.0.255, updateBeta)
cv2.setTrackbarPos('Alpha'.'image'.100)
cv2.setTrackbarPos('Beta'.'image'.10)
while (True):
cv2.imshow('image', img)
if cv2.waitKey(1) = =ord('q') :break
cv2.destroyAllWindows()
Copy the code
The effect