Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert android preview frame to OpenCV Mat

I'm trying to capture an image from the camera preview and convert it to a Mat object using OpenCV. I registered the callbak method public void onPreviewFrame(byte[] data, Camera camera) so I receive all preview frames from the camera, but I can't get it to convert to OpenCV Mat object.

I'm currently using the following code to convert the preview data to a Mat object:

Mat previewFrameMat = new Mat(1, data.length, CvType.CV_8U);
previewFrameMat.put(0, 0, data);

Is this the correct way to do it ? Am I using the correct image type (CvType.CV_8U) ?

like image 243
Muzikant Avatar asked Mar 10 '12 16:03

Muzikant


1 Answers

Have you seen how OpenCV samples do this?

mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, 
    getFrameWidth(), CvType.CV_8UC1);

....

mYuv.put(0, 0, data);

Full source can be found in the Android package from SourceForge or here: http://code.opencv.org/projects/opencv/repository/entry/trunk/opencv/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1View.java

like image 168
Andrey Kamaev Avatar answered Oct 21 '22 02:10

Andrey Kamaev