简体   繁体   中英

Why OpenCV HSV blue is green?

I am trying to convert blue from BGR to HSV using OpenCV. My code is:

blue = np.uint8([[[255,0,0 ]]])

hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)

lo_square = np.full((10, 10, 3), hsv_blue, dtype=np.uint8) / 255.0

plt.subplot(1, 1, 1)

plt.imshow(hsv_to_rgb(lo_square))
plt.show()

And color that I am getting looks like this:

绘制生成的图像

Why green value is 1? Where did opencv get it from?

For dtype uint8 , h assumes values between 0 and 180 when using cv2.cvtColor(blue, cv2.COLOR_BGR2HSV) , so you should divide it by 180. , not 255. , even though the s and v values are in 0.. 255 ranges.

This works:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import hsv_to_rgb
from cv2 import cv2

blue = np.uint8([[[255, 0, 0]]])
hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
lo_square = np.full((10, 10, 3), hsv_blue, dtype=np.uint8) / [180., 255., 255.]

plt.subplot(1, 1, 1)
plt.imshow(hsv_to_rgb(lo_square))
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM