Ask Your Question

How to get Full HSV Hue Value Range(0-360 or 0-1 in double) in Python?

asked 2019-01-17 04:48:16 -0500

thepate94227 gravatar image

updated 2019-01-17 04:54:58 -0500

I want to edit images and for that i need the Hue Value from the image. I first read the image with cv2.imread function. But when i use cv2.cvtColor function with cv2.COLOR_BGR2HSV or cv2.COLOR_BGR2HSV_FULL function to convert the image, i get a value range for hue from 0-179 or from 0-255. So i lose information about the image. If i convert the image for example in matlab, i get a double range from 0-1, so i don't lose information about the image.

I tried to read the image with cv2.imread and the flags= cv2.IMREAD_ANYDEPTH, but then i can't use the cv2.COLOR_BGR2HSV_FULL convert function...

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-01-17 05:03:00 -0500

berak gravatar image

updated 2019-01-17 05:05:10 -0500

the trick here is to convert your image to float32 (float64 is not supported), before the HSV conversion:

ocv = np.float32(ocv)
hsv = cv2.cvtColor(ocv, cv2.COLOR_BGR2HSV_FULL)

# check results:
mm = cv2.minMaxLoc(hsv[:,:,0])
print(mm, im.dtype)

((0.0, 358.7050476074219, (0, 0), (54, 33)), dtype('float32'))

(for the opencv logo above. you can see, -- max hue is ~360, as expected ;)

edit flag offensive delete link more


Thank you! :) I will try it now

thepate94227 gravatar imagethepate94227 ( 2019-01-17 05:07:10 -0500 )edit

It works, but i have new problems now... one of them: the cv2.merge function doesn't work anymore. if i split the HSV_FULL picture in h, s, v, pick the h channel, use kmeans on it and want to merge the result with cv2.merge, i get the error: TypeError: mv data type = 6 is not supported

thepate94227 gravatar imagethepate94227 ( 2019-01-18 06:37:10 -0500 )edit

@thepate94227, -- new problem, new question ?

you'll also have to show, what you're doing, and explain, hw you came up with data type = 6 (float32 should be 5)


a = np.ones((3,3),np.float64)
b = cv2.merge([a,a,a])

no problem, here !

berak gravatar imageberak ( 2019-01-18 06:50:59 -0500 )edit

I will show my code in my question...

thepate94227 gravatar imagethepate94227 ( 2019-01-18 06:56:16 -0500 )edit

ask a new question, show the input to kmeans , and the way down to cv2.merge()

berak gravatar imageberak ( 2019-01-18 06:57:30 -0500 )edit

Ok, i will ask a new question

thepate94227 gravatar imagethepate94227 ( 2019-01-18 06:59:33 -0500 )edit
thepate94227 gravatar imagethepate94227 ( 2019-01-18 07:29:32 -0500 )edit

Question Tools

1 follower


Asked: 2019-01-17 04:48:16 -0500

Seen: 2,763 times

Last updated: Jan 17 '19