This error occurs because the OpenCV version being used does not support the 'COLOR_GRAY2HSV' conversion.

To fix this, you can either upgrade to a newer version of OpenCV that supports this conversion or use a different method to convert the image to HSV.

One alternative method is to first convert the image to BGR using 'cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)', then convert it to HSV using 'cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)'. Finally, extract the V channel and convert it back to grayscale using 'cv2.split' and 'cv2.merge'.

Here's an example code snippet:

# Convert image to BGR
bgr_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

# Convert BGR to HSV
hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)

# Extract V channel
h, s, v = cv2.split(hsv_image)

# Randomly adjust V channel
v += np.random.randint(-10, 10)
v = np.clip(v, 0, 255).astype(np.uint8)

# Merge channels back to HSV image
hsv_image = cv2.merge([h, s, v])

# Convert back to grayscale
image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2GRAY)
OpenCV 'COLOR_GRAY2HSV' Error: Solution and Explanation

原文地址: https://www.cveoy.top/t/topic/mQaQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录