Create CV::Mat von Y Plane of YUV_420_888

asked 2016-10-26 06:15:39 -0500

Vintez gravatar image

I am working with android and the c++ OpenCV Libraries.

In Android I request (while Tracking) the latest Image of the TextureView which shows the Preview from the Camera:

private final ImageReader.OnImageAvailableListener mOnImageAvailableTrack =
        new ImageReader.OnImageAvailableListener(){

            @Override
            public void onImageAvailable(ImageReader reader) {
                if(mTaskfinished) {
                    mTaskfinished = false;
                    Log.w(TAG, "Open Framepuller Task");
                    Framepuller fPuller = new Framepuller();
                    fPuller.execute(reader.acquireLatestImage());
                }
            }
        };

Framepuller is a AsyncTask which converts the Images Y Plane into a Byte array.

Image img = params[0];
Image.Plane Y = img.getPlanes()[0];
ByteBuffer byteBuffer = Y.getBuffer();
byte[] data = new byte[byteBuffer.remaining()];
byteBuffer.get(data);

int iWidth = img.getWidth();
int iHeight = img.getHeight();
img.close();

The Height and Width and the Data is moved to the JNIInterface I got and converted into a unsigned char*.

int len = env->GetArrayLength(data);
unsigned char* buf = new unsigned char[len];
env->GetByteArrayRegion(data, 0, len, reinterpret_cast<jbyte*>(buf));

So now my Question is, how do I assign the ImageData from buf to a cv::Mat? can I just set the Mat like this:

cv::Mat img(width, height, CV_8UC1, imageData);

or

//Read somewhere that the rows/cols have to be adjusted like that if passing YUV data to MAT.
cv::Mat img(height + height/2, width, CV_8UC1, imageData);

What I achieve is, that in the Mat lays a grayscale Image of the YUV_420_888 I get from my Camera Preview.

edit retag flag offensive close merge delete