No way to convert from YUV420sp to RGB565 with OpenCV?
I want to do real time processing with Android. My code is derived from this http://www.stanford.edu/class/ee368/Android/HelloViewfinder/Project.zip
They use a Java function decodeYUV420RGB(mRGBData, mYUVData, mImageWidth, mImageHeight) for conversion. But I want to use OpenCV instead. In the original code a bitmap is created in onPreviewFrame
mDrawOnTop.mBitmap = Bitmap.createBitmap(mDrawOnTop.mImageWidth,
mDrawOnTop.mImageHeight, Bitmap.Config.RGB_565);
and they use Java code to decode YUV to RGB_565 in onDraw
// Convert from YUV to RGB -> I think it must be RGB_565 because Bitmap is created in RGB_565
if (mState == STATE_ORIGINAL)
decodeYUV420RGB(mRGBData, mYUVData, mImageWidth, mImageHeight);
else
decodeYUV420RGBContrastEnhance(mRGBData, mYUVData, mImageWidth, mImageHeight);
// Draw bitmap
mBitmap.setPixels(mRGBData, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight);
I tried a lot of combinations of this code (do comments and withdraw comments) in onDraw to convert YUV420sp to RGB_565 with OpenCV but nothing works:
// Convert from YUV to RGB
// http://stackoverflow.com/questions/11336315/converting-nv21-to-rgb-using-opencv-in-android?rq=1
// http://stackoverflow.com/questions/9647897/convert-android-preview-frame-to-opencv-mat
if( mYUV_Mat != null ) mYUV_Mat.release();
//mYUV_Mat = new Mat( newImageWidth, newImageHeight, CvType.CV_8UC1 );
//mYUV_Mat = new Mat( newImageHeight, newImageWidth, CvType.CV_8UC1 );
//mYUV_Mat = new Mat( newImageWidth, newImageHeight, CvType.CV_8UC4 );
mYUV_Mat = new Mat( mImageHeight, mImageWidth, CvType.CV_8UC1 );
//mYUV_Mat = new Mat( mImageWidth, mImageHeight, CvType.CV_8UC1 );
//mYUV_Mat = new Mat();
mYUV_Mat.put( 0, 0, mYUVData );
Mat mRgba_Mat = new Mat();
//Mat mRgba_Mat = new Mat(newImageWidth,newImageHeight,CvType.CV_8UC4);
//Mat mRgba_Mat = new Mat(mImageWidth,mImageHeight,CvType.CV_8UC4);
//Mat mRgba_Mat = mYUV_Mat.clone();
// http://stackoverflow.com/questions/11336315/converting-nv21-to-rgb-using-opencv-in-android?rq=1
//Imgproc.cvtColor( mYUV_Mat, mRgba_Mat, Imgproc.COLOR_YUV2RGB_NV21, 4 );
// https://groups.google.com/forum/?fromgroups=#!topic/android-opencv/l_QbOn24tuA
//Imgproc.cvtColor( mYUV_Mat, mRgba_Mat, Imgproc.COLOR_YUV420sp2RGB, 4 );
Mat mTemp = new Mat();
//Imgproc.cvtColor( mYUV_Mat, mRgba_Mat, Imgproc.COLOR_YUV420sp2RGB );
Imgproc.cvtColor( mYUV_Mat, mTemp, Imgproc.COLOR_YUV420sp2RGBA );
//Imgproc.cvtColor( mTemp, mRgba_Mat, Imgproc.COLOR_RGBA2BGR565 );
//Imgproc.cvtColor( mRgba_Mat, mRgba_Mat, Imgproc.COLOR_BGR, 4);
// Draw Bitmap New:
//Bitmap mBitmap = Bitmap.createBitmap( newImageWidth, newImageHeight, Bitmap.Config.ARGB_8888 );
//Bitmap mBitmap = Bitmap.createBitmap( mImageWidth, mImageHeight, Bitmap.Config.ARGB_8888 );
//Bitmap mBitmap = Bitmap.createBitmap( newImageHeight, newImageWidth, Bitmap.Config.ARGB_8888 );
//Bitmap mBitmap = Bitmap.createBitmap( mRgba_Mat.height(), mRgba_Mat.width(), Bitmap.Config.ARGB_8888 );
//***
//Bitmap mBitmap = Bitmap.createBitmap( mRgba_Mat.width(), mRgba_Mat.height(), Bitmap.Config.ARGB_8888 );
//Bitmap mBitmap = Bitmap.createBitmap( mRgba_Mat.height(), mRgba_Mat.width(), Bitmap.Config.ARGB_8888 );
//Bitmap mBitmap = Bitmap.createBitmap( mRgba_Mat.width(), mRgba_Mat.height(), Bitmap.Config.RGB_565 );
//Bitmap mBitmap = Bitmap.createBitmap( mRgba_Mat.height(), mRgba_Mat.width(), Bitmap.Config.RGB_565 );
Utils.matToBitmap(mRgba_Mat, mBitmap);
mRgba_Mat.release();
Rect src = new Rect(0, 0, mImageWidth, mImageHeight);
Rect dst = new Rect(marginWidth, 0, canvasWidth-marginWidth, canvasHeight);
canvas.drawBitmap(mBitmap, src, dst, mPaintBlack);
How can I convert YUV420sp to RGB_565 with OpenCV?