I have a YUV420_SP_NV21 image represented as a byte array (no headers), taken from an Android preview frame, and I need to decode it into a RGB image.
I've done this in Android apps before, using Java and OpenCV4Android:
convert_mYuv = new Mat(height + height / 2, width, CvType.CV_8UC1);
convert_mYuv.put( 0, 0, data );
Imgproc.cvtColor( convert_mYuv, convert_mRgba, type, channels );
I've tried doing the same in Python:
nmp = np.array(byteArray, dtype=np.ubyte)
RGBMatrix = cv2.cvtColor(nmp, cv2.COLOR_YUV420P2RGB)
, but RGBMatrix remains None.
I'm aware of the possibility to do this myself, I'm familiar with the formula, but would be realy happy to do this the OpenCV way.
How can this be done?
I've also tried cv2.imdecode(), but it failed too, possibly because of me miss-using it.
解决方案
Its been a while since I solved this issue, but I hadn't the time to update this question.
I ended up extracting the YUV420_SP_NV21 byte array to a full scaled YUV image (1280x720 in my case) using numpy, then converting it to RGB using OpenCV.
import cv2 as cv2 # OpenCV import
def YUVtoRGB(byteArray):
e = 1280*720
Y = byteArray[0:e]
Y = np.reshape(Y, (720,1280))
s = e
V = byteArray[s::2]
V = np.repeat(V, 2, 0)
V = np.reshape(V, (360,1280))
V = np.repeat(V, 2, 0)
U = byteArray[s+1::2]
U = np.repeat(U, 2, 0)
U = np.reshape(U, (360,1280))
U = np.repeat(U, 2, 0)
RGBMatrix = (np.dstack([Y,U,V])).astype(np.uint8)
RGBMatrix = cv2.cvtColor(RGBMatrix, cv2.COLOR_YUV2RGB, 3)
My raw data was YUV420_SP_NV21, which is decode like this: YY...YVUVUVU...VU,
but other YUV formats will require subtle changes to the code.