Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 its the double conversion is too slow.

Is there any faster way?

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 its the double conversion is too wastefull / slow.

Is there any faster way?