Friday, October 20, 2023

OpenCV and Image Processing - Adjust Contrast and Brightness of an Image

We already saw how to adjust the brightness and contrast for a webcam, however unfortunately OpenCV doesn’t provide functions to adjust the brightness and contrast for an image. But OpenCV official document recommends an alternative way to do it, see the link at:

https://docs.opencv.org/4.7.0/d3/dc1/tutorial_basic_linear_transform.html

We dot not intend to deep dive into the algorithms or theories, instead, we focus on the implementation of the techniques using OpenCV with Python. However, in order to make things clear, sometimes we will summarize, or quote from other sources, the related algorithms or theories.

To summarize the techniques of contrast and brightness adjustment, the below formula from OpenCV official document depicts the calculation of the contrast and brightness, 


f ( i , j ) is the original image and g( i , j ) is the result. α changes the contrast, greater than 1 for higher contrast, less than 1 for lower contrast. It should be greater than 0. β changes the brightness, values vary from -127 to +127. ( i , j ) indicates the coordinates of the image pixels.

The OpenCV official document provides an example to explain how to use the Python array and matrix operations to implement the above formula. Alternatively, OpenCV provides cv2.addWeighted() function which is designed for blending two images with weight added on each, we will use this function to implement the above formula to adjust the contrast and brightness.

cv2.addWeighted(image, contrast, zeros, 0, brightness)

The function cv2.addWeighted() implements the below formula:

g( i , j ) = α1 f1 ( i , j ) + α2 f2 ( i , j ) + β

g( i , j ) is the result, f1( i , j ) is the image and passed to cv2.addWeighted() as the first parameter, α1 is the second parameter which is contrast; because only one image is interested in this example, so f2( i , j ) is an all-zero image, and α2 is also a zero, therefore the third and fourth parameters of the function are an all-zero array and a zero value. β is the last parameter, which is brightness, this value will be added to the result.

Now add a new contrast_brightness() function to the class ImageProcessing, which we have defined in previous posts.

45

def contrast_brightness(self, contrast,

brightness, image=None):

46

# contrast:

# between 0 and 1: less contrast;

47 # > 1: more contrast;

48 # 1: unchanged

49 # brightness: -127 to 127;

50 # 0 unchanged

51 if image is None:

52 image = self.image

53 zeros = np.zeros(image.shape, image.dtype)

54

result = cv2.addWeighted(image, contrast,

zeros, 0, brightness)

55 return result

Line 45 Define contrast_brightness() function, specify contrast and brightness as parameters, and optionally an image.

Line 53 Create an all-zero array, because cv2.addWeighted() requires two images, we don’t use the second one, so pass the all-zero array to it.

Line 54 Call cv2.addWeighted() function to apply the weights to the image.

Below is the code to adjust the brightness and contrast using the function just created above. Two trackbars are added for users to adjust the brightness and contrast.

1 import cv2

2 import common.ImageProcessing as ip

3

4 def change_brightness(value):

5 global brightness

6 brightness = value - 128

7

8 def change_contrast(value):

9 global contrast

10 contrast = float(value)/100

11

12 if __name__ == "__main__":

13 brightness = 0

14 contrast = 1.0

15 title = "Adjust Brightness and Contrast"

16 ip = ip.ImageProcessing(title,

"../res/flower003.jpg")

17 cv2.createTrackbar('Brightness', title, 128,

255, change_brightness)

18 cv2.createTrackbar('Contrast', title, 100,

300, change_contrast)

19 20 while True:

21 adjusted_image = ip.contrast_brightness(

contrast, brightness)

22 ip.show(image=adjusted_image)

23 if cv2.waitKey(10) & 0xFF == 27:

24 break

25 cv2.destroyAllWindows()

Line 2 Import the ImageProcessing class.

Line 4 - 6 Define the callback function for the trackbar to adjust the brightness.

Line 8 - 10 Define the callback function for the trackbar to adjust the contrast.

Line 12 Main entrance.

Line 16 Instantiate the ImageProcessing object.

Line 17 - 18 Create two trackbars for brightness and contrast.

Line 21 – 22 Call contrast_brightness(), the function we just defined, to adjust brightness and contrast.

Execute the code, the result shows the image together with two trackbars, as Figure shown below, one is for adjusting brightness and another for contrast:


Use the mouse to drag the two trackbars to change the brightness and contrast values, the image will be changed accordingly in real-time. The range of contrast in trackbar is from 0 to 300, and the default is 100. In the callback function change_contrast(), the value is divided by 100, therefore the contrast range is from 0.0 to 3.0, default is 1.0.

Feel free to play with the codes by changing the image files and adjusting the two trackbars and observe how the image is changing accordingly.

Share:

0 comments:

Post a Comment