# YUV (bytes) to HSV ?

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 close merge delete

Sort by ยป oldest newest most voted

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.

more

Official site

GitHub

Wiki

Documentation