Ask Your Question
0

Opencv Java Convert Mat object to bytebuffer

asked 2018-10-25 04:53:49 -0600

jayaise gravatar image

Hi I want to convert Mat Object to bytebuffer Can you please help here

edit retag flag offensive close merge delete

Comments

what does bytebuffer mean exactly, here ? did you mean a byte[] ? or this ?

please be concise !

berak gravatar imageberak ( 2018-10-25 05:02:20 -0600 )edit
1

Its byte[]

I am working on project where we are displaying the videos, I want to add the text to the video. So I am trying to convert byte[] to Mat Object, then add text to it, then again convery Mat object to byte[]. I need help in converting below 2 things

1> byte[] to Mat object, 2> Mat Object to byte[].

How can I achieve it..

jayaise gravatar imagejayaise ( 2018-10-25 07:24:50 -0600 )edit

I want to add the text to the video

there's putText(), look it up.

berak gravatar imageberak ( 2018-10-25 07:56:04 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-10-25 07:52:39 -0600

berak gravatar image

updated 2018-10-27 08:51:04 -0600

ok, you can use Mat.get() to copy all of the pixels into a buffer:

//but first : check !
if (mat.depth() == CvType.CV_8U) { //bytes !
    byte[] buffer = new byte[mat.total() * mat.channels()]; 
    mat.get(0,0,buffer);
} else 
if (mat.depth() == CvType.CV_32F) { //float !
    float[] buffer = new float[mat.total() * mat.channels()]; 
    ... etc

the reverse is simply:

Mat m(height, width, type);
m.put(0,0,buffer);
edit flag offensive delete link more

Comments

Ok I will try this but for reverse Mat m(height, width, type)

How can I get width, height and what should be type

jayaise gravatar imagejayaise ( 2018-10-25 13:28:24 -0600 )edit

@jaysise -- the properties of your original mat, ofc. (where you got the buffer from)

berak gravatar imageberak ( 2018-10-25 13:34:50 -0600 )edit

I have did code as below:

Mat m = new Mat (720, 1280, CvType.CV_8U);
m.put(0,0,vf.rawData.getBytes());
Imgproc.putText(m, "Welcome", new Point(250,100),
  Core.FONT_ITALIC, 4.0 ,new Scalar(222),2);
byte[] buffer = new byte[((int)m.total())*m.channels()];
m.get(0,0,buffer);

I think there is problem with below: byte[] buffer = new byte[((int)m.total())*m.channels()]; m.get(0,0,buffer);

But I am getting error as below:

Corrupt JPEG data: bad Huffman code

Corrupt JPEG data: premature end of data segment

Corrupt JPEG data: bad Huffman code

jayaise gravatar imagejayaise ( 2018-10-27 07:42:41 -0600 )edit

no, unrelated, the error comes from reading (or saving) the image

berak gravatar imageberak ( 2018-10-27 07:53:57 -0600 )edit

this is while saving the image and displaying.

I also tried saving buffer(bytes) back to image.jpg file The image is not clear, its shaky

Also added the text using using putText(), the text is not getting added to image

jayaise gravatar imagejayaise ( 2018-10-27 08:38:13 -0600 )edit

I could resolve Corrupt JPEG data: bad Huffman code issue, Mat m = new Mat (720, 1280, CvType.CV_8U); // I changed this to Mat m = new Mat (720, 1280, CvType.CV_8UC4);

jayaise gravatar imagejayaise ( 2018-10-27 12:57:48 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-10-25 04:53:49 -0600

Seen: 4,635 times

Last updated: Oct 27 '18