You can use this function to change the desired brightness or contrast using C ++ in the same way as you do it in Photoshop or other similar photo editing software.
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 def map(x, in_min, in_max, out_min, out_max): return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min)
After that, you need to call the functions by creating a trackbar using cv2.createTrackbar() and call the above functions with the appropriate parameters. To display brightness values โโranging from -255 to +255 and contrast values โโof -127 to +127, you can use this map() function. You can check out the complete Python implementation information here .
Md. Hanif Ali Sohag
source share