M. Akar
M. Akar

Reputation: 1865

How can I convert Otaliastudios Frame object into OpenCV Mat object?

I am using com.otaliastudios.cameraview.CameraView component from otaliastudios Library. I need to convert some of the frames to Mat objects to process using OpenCV. How can I convert Otaliastudios Frame object into OpenCV Mat object?

Edit: The frame class I am using is located here: github.com/natario1/CameraView/blob/master/cameraview/src/main/java/com/otaliastudios/cameraview/Frame.java

How can I know which Image format this is? Does it make a difference?

Upvotes: 0

Views: 503

Answers (1)

TomD88
TomD88

Reputation: 751

You need to know the source format of your phone camera frame.

The Frame object contains a byte[] data field.This field is probably in the same ImageFormat of your camera. The two most common formats are NV21 and YUV_420_888.

The YUV format is composed by a component that is the luminance Y and another component that is called chrominance (U-V).

Typically the relation (and consequently the real bit/byte size) of these two components is defined by methods that reduce the chrominance component because human eyes are more sensible to luminance variations than color variations (see Chroma Subsampling). The reduction is expressed by a set of numbers like 4:2:0.

In this case the part related to chrominance is half the size of luminance. So the byte size of a Frame has probably a part for the luminance that is equal to width X height bytes and a part of chrominance that is width X (height/2) bytes. This means that the byte size is heavily dependent on the image format that you are acquiring and you have to modify the mat size and choose the CvType according to this.

You have to allocate a mat that has the same size of your frame and put the data into it(from this answer):

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

....

mYuv.put(0, 0, data);

And then you have your Mat. If you need also to convert to an RGB format check the bottom of this page.

Hope this will help you.

Upvotes: 2

Related Questions