RGB to HSV Conversion: How to Calculate HSV and Lower/Upper Red Ranges
According to RGB value to calculate HSV value:
First, divide the r, g, b values by 255 to get their normalized values:
r = 250/255 = 0.98 g = 53/255 ≈ 0.21 b = 52/255 ≈ 0.20
Then, find the maximum and minimum values:
max_value = max(r, g, b) = 0.98 min_value = min(r, g, b) = 0.20
Calculate the brightness value (Value):
V = max_value = 0.98
Calculate the saturation (Saturation):
If max_value = min_value, then S = 0 (grayscale image); otherwise, calculate saturation:
S = (max_value - min_value) / max_value = (0.98 - 0.20) / 0.98 ≈ 0.80
Calculate the hue (Hue):
If max_value = min_value, then H = 0 (grayscale image); otherwise, calculate the hue according to the following formula:
if r = max_value: H = (g - b) / (max_value - min_value) elif g = max_value: H = 2 + (b - r) / (max_value - min_value) else: H = 4 + (r - g) / (max_value - min_value)
Convert H to degrees:
H = H * 60° if H < 0: H = H + 360°
In this case, r = max_value, so:
H = (g - b) / (max_value - min_value) = (0.21 - 0.20) / (0.98 - 0.20) ≈ 0.64
H = H * 60° ≈ 38.4°
Therefore, the HSV value corresponding to RGB value (250, 53, 52) is (V ≈ 0.98, S ≈ 0.80, H ≈ 38.4°).
For lower_red and upper_red, they are usually used to identify red areas in the image. Since red spans the 0-degree boundary in the HSV color space, two ranges need to be defined separately. A common approach is to set lower_red to (0, 50, 50) and upper_red to (10, 255, 255) and (170, 50, 50) to (180, 255, 255). Specific implementations may vary depending on the application scenario.
原文地址: https://www.cveoy.top/t/topic/n7mV 著作权归作者所有。请勿转载和采集!