2021-01-07 python opencv adjusts picture brightness and contrast

python opencv adjust picture brightness and contrast

 

Intensity adjustment is to increase/decrease the overall intensity of image pixels. Contrast adjustment refers to that the dark parts of the image become darker, and the brightness becomes brighter, thereby broadening the display accuracy in a certain area.
Create two sliders to adjust contrast and brightness respectively (contrast range: 0 ~ 0.3, brightness 0 ~ 100). Tip: Because the slider has no decimals, it can be set to 0 ~ 300, and then multiplied 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))
# 创建窗口
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()

effect 

Guess you like

Origin blog.csdn.net/qingfengxd1/article/details/112307170