Ask Your Question
0

YUV (bytes) to HSV ?

asked 2016-04-09 19:20:38 -0600

vegetablejuiceftw gravatar image

updated 2016-04-09 19:21:19 -0600

I have a YUV420p stream from picamera.

I am able to convert it to RGB with this:

def convertYUV(stream,resolution):
fwidth,fheight = resolution
A = np.frombuffer(stream, dtype=np.uint8)
Y = A[:fwidth*fheight]
U = A[fwidth*fheight:fwidth*fheight+(fwidth//2)*(fheight//2)]
V = A[fwidth*fheight+(fwidth//2)*(fheight//2):]
Y = Y.reshape((fheight, fwidth))
U = U.reshape((fheight//2, fwidth//2))
V = V.reshape((fheight//2, fwidth//2))

# lower res to color, raise color to res
# Y = cv2.resize(Y, (fwidth//2,fheight//2), interpolation = cv2.INTER_NEAREST )
U = cv2.resize(U, (fwidth,fheight), interpolation = cv2.INTER_NEAREST )
V = cv2.resize(V, (fwidth,fheight), interpolation = cv2.INTER_NEAREST )
YUV = (np.dstack([Y,U,V])).astype(np.uint8)
RGB = cv2.cvtColor(YUV, cv2.COLOR_YUV2RGB, 3)
# HSV = cv2.cvtColor(RGB, cv2.COLOR_RGB2HSV)
return RGB

and as evident, HSV = cv2.cvtColor(RGB, cv2.COLOR_RGB2HSV), is a simple solution. but the double conversion is too wastefull / slow.

Is there any faster way?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-04-09 23:28:15 -0600

Tetragramm gravatar image

I don't believe so. YUV-RGB is simple, but RGB->HSV is not. Calculating Hue depends explicitly on the values of R, G, and B, so you'd need to more or less convert anyway.

If I may suggest, you can convert from YUV420p to RGB directly in OpenCV using the COLOR_YUV420p2RGB and COLOR_YUV420p2BGR tokens. It's probably much faster that way. OpenCV is pretty well optimized for these calls, especially if you've got Intel IPP, which you almost certainly do.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-09 19:20:38 -0600

Seen: 4,479 times

Last updated: Apr 09 '16