What's the fastest way to increase color image contrast with OpenCV in python (cv2)?

后端 未结 3 1371
我在风中等你
我在风中等你 2020-12-13 07:50

I\'m using OpenCV to process some images, and one of the first steps I need to perform is increasing the image contrast on a color image. The fastest method I\'ve found so

3条回答
  •  伪装坚强ぢ
    2020-12-13 08:25

    Try this code:

    import cv2
    
    img = cv2.imread('sunset.jpg', 1)
    cv2.imshow("Original image",img)
    
    # CLAHE (Contrast Limited Adaptive Histogram Equalization)
    clahe = cv2.createCLAHE(clipLimit=3., tileGridSize=(8,8))
    
    lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)  # convert from BGR to LAB color space
    l, a, b = cv2.split(lab)  # split on 3 different channels
    
    l2 = clahe.apply(l)  # apply CLAHE to the L-channel
    
    lab = cv2.merge((l2,a,b))  # merge channels
    img2 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)  # convert from LAB to BGR
    cv2.imshow('Increased contrast', img2)
    #cv2.imwrite('sunset_modified.jpg', img2)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Sunset before: Sunset after increased contrast:

提交回复
热议问题