Monday, October 23, 2023

OpenCV and Image Processing - Adjust Hue, Saturation and Value

We have already explained a color image can be split not only to BGR channels but also HSV channels, the former represents blue, green and red channels, the latter represents hue, saturation and value. The hue represents the color of the image, different hue values have different colors; saturation represents the purity of the color, a higher saturation means a more colorful image; while value represents the brightness of the image, a higher value means a brighter image.

In this post we will explain how to adjust hue, saturation and value of an image, three trackbars will be created for the three variables, we will see how the image is changing when users adjust these variables in real-time.

The first thing is to convert the image into HSV using cv2.cvtColor() with cv2.COLOR_BGR2HSV parameter, then split it into hue, saturation and value channels using cv2.split().

Then add weights to hue, saturation and value channels respectively, and merge them back to HSV. Finally convert the HSV to BGR for displaying.

Same as the previous section, cv2.addWeighted() is used to adjust each channel with a weight. The below line of code is to add a weight h_weight to the hue channel,

1 hue = cv2.addWeighted(hue, 1.0, zeros, 0, h_weight)

And add the weights to other channels in the same way.

Here are the codes, the function hue_saturation_value() is added into our ImageProcessing class:

1 def hue_saturation_value(self, hue, saturation,

value, image=None):

2 if image is None:

3 image = self.image

4 hsvImage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

5 h, s, v = cv2.split(hsvImage)

6 zeros = np.zeros(h.shape, h.dtype)

7 h = cv2.addWeighted(h, 1.0, zeros, 0, hue)

8 s = cv2.addWeighted(s, 1.0, zeros, 0, saturation)

9 v = cv2.addWeighted(v, 1.0, zeros, 0, value)

10 result = cv2.merge([h, s, v])

11 result = cv2.cvtColor(result, cv2.COLOR_HSV2BGR)

12 return result

Explanations:

Line 1 Define the function in ImageProcessing class, the parameters are hue, saturation and value.

Line 4 Convert the image into HSV image.

Line 5 Split the HSV image into separate h, s, v channels.

Line 7 - 9 Add the weights to the three channels. The hue, saturation and value are the weights to h, s, and v channels respectively.

Line 10 Merge the h, s, v channels into the HSV image.

Line 11 Convert the HSV image back to BGR image.

This function is called from the HueSaturation.py file, in this example we add a feature to save the image into a file on the local disk drive, in addition to pressing ESC key to exit, when press “s” key the file will be saved to a specified file using cv2.imwrite() function.

1 import cv2

2 import common.ImageProcessing as ip

3

4 def change_hue(value):

5 global hue

6 hue = (value * 2) – 255

7

8 def change_saturation(value):

9 global sat

10 sat = (value * 2) – 255

11

12 def change_value(value):

13 global val

14 val = (value * 2) – 255

15

16 if __name__ == "__main__":

17 hue, sat, val = 0, 0, 0

18 title = "Adjust Hue, Saturation and Value"

19

ip = ip.ImageProcessing(title,

"../res/flower003.jpg")

20

cv2.createTrackbar('Hue', title, 127, 255,

change_hue)

21

cv2.createTrackbar('Saturation', title,

127, 255, change_saturation)

22

cv2.createTrackbar('Value', title, 127,

255, change_value)

23 ip.show("Original")

24

print("Press s key to save image,

ESC to exit.")

25

26 while True:

27

adjusted_image =

ip.hue_saturation_value(hue, sat, val)

28 ip.show(image=adjusted_image)

29 ch = cv2.waitKey(10)

30 if (ch & 0xFF) == 27:

31 Break

32 elif ch == ord('s'):

33 # press 's' key to save image

34 filepath = "C:/temp/flower003.png"

35 cv2.imwrite(filepath, adjusted_image)

36 print("File saved to " + filepath)

37 cv2.destroyAllWindows()

Explanations:

Line 4 - 15 Define three callback functions for trackbars for hue, saturation and value. The value is changing from -255 to 255 for each. You can modify the range base on your needs.

Line 19 Instantiate the ImageProcess class with a title and an image.

Line 20 - 22 Create three trackbars for hue, saturation and value respectively.

Line 23 Show the original image.

Line 27 Call the function we just defined in ImageProcess class with parameters of the adjusted hue, saturation and value.

Line 28 Show the result image.

Line 32 - 36 Save the result image when pressing “s” key.

Execute the code, the result shows the original image and the adjusted image together with three trackbars, as Figure 5.10, for adjusting hue, saturation and value respectively.


The original image and adjusted image are omitted here, feel free to change different images in the codes and observe how they are changing when adjusting the hue, saturation and value by moving the knobs of the trackbars.

Share:

0 comments:

Post a Comment