Home > OS >  Why OpenCV HSV blue is green?
Why OpenCV HSV blue is green?

Time:01-06

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:

Plotted generated image

Why green value is 1? Where did opencv get it from? (Note that the converted colour value is visible in the graphic, [0, 1, 0.824] and the value of hsv_blue is array([[[120, 255, 255]]], dtype=uint8))

CodePudding user response:

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()
  •  Tags:  
  • Related