Ask Your Question
0

Auto adjusting contrast and brightness with OpenCV Python

asked 2018-06-22 03:47:51 -0600

flavienb gravatar image

Hello,

I am looking for a Python version of the following C++ OpenCV script (or another script) that would be able to auto-adjust the brightness and contrast of a photo.

http://answers.opencv.org/question/75510

Have someone already worked on this ?

Thank you and have a great day.

edit retag flag offensive close merge delete

Comments

Have you tried the BUILD IN "CLAHE"

jsxyhelu gravatar imagejsxyhelu ( 2018-06-22 07:09:02 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-26 17:21:16 -0600

hanifalisohag gravatar image

You can use this function to change your desired brightness or contrast using C++ just like the same way you do it on photoshop or other similar photo editing software. You can check details of the python implementation More details about python implementation

def apply_brightness_contrast(input_img, brightness = 255, contrast = 127):
        brightness = map(brightness, 0, 510, -255, 255)
        contrast = map(contrast, 0, 254, -127, 127)

        if brightness != 0:
            if brightness > 0:
                shadow = brightness
                highlight = 255
            else:
                shadow = 0
                highlight = 255 + brightness
            alpha_b = (highlight - shadow)/255
            gamma_b = shadow

            buf = cv2.addWeighted(input_img, alpha_b, input_img, 0, gamma_b)
        else:
            buf = input_img.copy()

        if contrast != 0:
            f = float(131 * (contrast + 127)) / (127 * (131 - contrast))
            alpha_c = f
            gamma_c = 127*(1-f)

            buf = cv2.addWeighted(buf, alpha_c, buf, 0, gamma_c)

        cv2.putText(buf,'B:{},C:{}'.format(brightness,contrast),(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        return buf
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-06-22 03:47:51 -0600

Seen: 6,474 times

Last updated: Apr 26 '19