Ask Your Question
3

Convert Mat to MatOfByte in Android

asked 2012-10-03 23:54:05 -0600

Quill Hon gravatar image

I got a Mat from OpenCV's camera. And I want to convert it to a MatOfByte or a byte[]. But I got runtime error in my code:

Mat mGray = new Mat();    
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
MatOfByte mGrayByte = new MatOfByte(mGray);

I got the error:

10-04 12:50:35.470: E/AndroidRuntime(25086): java.lang.IllegalArgumentException: Incomatible Mat 10-04 12:50:35.470: E/AndroidRuntime(25086): at org.opencv.core.MatOfByte.<init>(MatOfByte.java:29)

The error line is:

MatOfByte mGrayByte = new MatOfByte(mGray);

edit retag flag offensive close merge delete

Comments

For what purpose do you want to use MatOfByte? Maybe there are better solutions.

Rui Marques gravatar imageRui Marques ( 2012-10-04 04:58:08 -0600 )edit

I want to pass the Image to Tesseract-OCR api with byte data array. Because I don't want to convert it to bitmap.

http://fossies.org/dox/tesseract-3.01/group__AdvancedAPI.html#gaa463622111f3b11d8fca5863709cc699

Quill Hon gravatar imageQuill Hon ( 2012-10-05 22:16:44 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2012-10-06 09:46:34 -0600

Rui Marques gravatar image

updated 2012-10-06 09:49:49 -0600

Try it like this.

        MatOfByte matOfByte = new MatOfByte();

        // encoding to png, so that your image does not lose information like with jpeg.
        Highgui.imencode(".png", mGray, matOfByte); 

        byte[] byteArray = matOfByte.toArray();
edit flag offensive delete link more

Comments

1

At least!! something that works! you have saved my project! You must be the Chuck Norris's son! XD

joseFraunhofer gravatar imagejoseFraunhofer ( 2013-03-07 07:24:25 -0600 )edit

Hahah... I am glad you liked.

Rui Marques gravatar imageRui Marques ( 2013-03-07 08:37:18 -0600 )edit
2

answered 2013-03-11 05:51:39 -0600

Andrey Pavlenko gravatar image

updated 2013-03-11 05:53:09 -0600

byte[] data = new byte[ (int) (mGray.total()) ];
mGray.get(0,  0, data);

Note: this works only if mGray is of CV_8UC1 type.

edit flag offensive delete link more

Comments

@Andrey Pavlenko i want to convert a Mat CV_8UC4 to byte[] but don't work any of those solution!

Marwen Z gravatar imageMarwen Z ( 2013-04-07 06:45:43 -0600 )edit

For 8UC4 (or any 8UCx) you need:

 byte[] data = new byte[ (int) (mRgba.total() * mRgba.channels()) ];
 mRgba.get(0,  0, data);
Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-04-08 00:57:30 -0600 )edit

yes i try it but not working, i found another solution is to convert Mat to Bitmap then to Byte[]

Utils.matTobitmap(mRgba,bitmap); ByteArrayOutputStream os = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 100, os); bytes[] data= os.toByteArray(); //the opposite case : BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    How you thnig about it ? it's efficace  !
Marwen Z gravatar imageMarwen Z ( 2013-04-08 04:07:10 -0600 )edit

this introduces extra copying, what are the problems using Mat.get() ?

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-04-09 06:48:34 -0600 )edit

it gives me an empty Byte data

Marwen Z gravatar imageMarwen Z ( 2013-04-09 10:55:26 -0600 )edit

but what does your_mat.toString() returns? your_mat.total()?

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-04-09 11:23:29 -0600 )edit

mat.total=0 mat.channel !=0 mat.toString gives me detail of the mat

Marwen Z gravatar imageMarwen Z ( 2013-04-09 12:17:50 -0600 )edit
1

mat.total==0 means your mat is empty!

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-04-10 07:55:52 -0600 )edit

Question Tools

Stats

Asked: 2012-10-03 23:54:05 -0600

Seen: 11,097 times

Last updated: Mar 11 '13