Ask Your Question
1

Convert Mat to byte[] in C++

asked 2014-05-19 04:25:50 -0600

Leena gravatar image

updated 2016-01-07 05:55:49 -0600

Hello,

For a project I have to return a byte[] in C++ in order to be implement by wrapper like java. I am not very experimented with byte and I don't know what it's the best approach. I tried this code :

typedef unsigned char byte;

byte[] matToBytes(Mat image)
{
   int size = image.rows*image.cols;
   byte bytes[size];
   std::memcpy(bytes,image.data,size * sizeof(byte);
}

And I must be able to reconvert this byte[] in Mat. So I tried this code too :

Mat BytestoMat(byte[] bytes,int width,int height)
{
    Mat image = Mat(height,width,CV_8UC3,bytes);
    return image;
}

Do you think it a good approach or is there a better approach ? I saw there is function like imread and imwrite in opencv but i don't if it can do the same thing.

Best regards

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2014-05-19 04:56:00 -0600

berak gravatar image

updated 2014-05-19 04:57:38 -0600

your matToBytes function will not work, because you're returning the address of a locally allocated variable. this will go out of scope, and leave you with a dangling pointer .

byte * matToBytes(Mat image)
{
   int size = image.total() * image.elemSize();
   byte * bytes = new byte[size];  // you will have to delete[] that later
   std::memcpy(bytes,image.data,size * sizeof(byte));
}

your BytestoMat function suffers from a similar problem. if your bytes[] go out of scope, the Mat.data pointer is invalid.

Mat bytesToMat(byte * bytes,int width,int height)
{
    Mat image = Mat(height,width,CV_8UC3,bytes).clone(); // make a copy
    return image;
}
edit flag offensive delete link more

Comments

Thanks It works greats ^^ But I have two another questions : To reconvert byte * to Mat do I must always know the size and the number of channel of the image ? On a java side, once I manage to convert a byte * to byte[] can I create a Bitmap image without knonwing the size of the image too ?

Leena gravatar imageLeena ( 2014-05-19 05:09:17 -0600 )edit

hmm, not like this. no way telling the shape from the pixels only.

berak gravatar imageberak ( 2014-05-19 05:21:15 -0600 )edit

I am sorry but I don't understand your answer. I will receive byte * without knowing what is the image in input (dim image,gray or color image etc...) so I was just wondering is there a way to do this. Anyway thanks again for your answers

Leena gravatar imageLeena ( 2014-05-19 05:52:33 -0600 )edit

maybe we need to know a bit more about your context here.

can't you pass a Mat (which contains all the nessecary information) instead of the bytes[] ?

berak gravatar imageberak ( 2014-05-19 06:04:35 -0600 )edit

The context : I will create a application C++ wich will do some image processing and return a image. This application will be a library .dll or .so in order to be used by other langage like Java trough JNA. The main function of the library is : byte* doSomeImageProcessing(string "pathToConf", byte* data);. My superior doen'st want to see a single object of opencv outside the library, that is why I was thinking of using byte* in return and create a Bitmap from this byte*.

Leena gravatar imageLeena ( 2014-05-19 07:10:23 -0600 )edit

i see ;) maybe pass more info into the func, and try to convert to bitmap on the jni side already ? (no idea if that's possible, haven't done any jni on android)

Bitmap doSomeImageProcessing(string "pathToConf", byte* data, int width, int height, int type);.

berak gravatar imageberak ( 2014-05-19 07:18:35 -0600 )edit

Yeah I think you are right, If we pass a image in input we should know it's size too and type too. I will look to a way to pass a direct Bitmap in jni, thanks again for you help :)

Leena gravatar imageLeena ( 2014-05-19 07:28:42 -0600 )edit
0

answered 2015-06-24 21:03:41 -0600

pursang gravatar image

I just wanted to add something to berak's answer:

I encountered this problem as well, but had to return a jbyteArray (I'm using JNI and want a byte[] to return to the Java part). So I added this to obtain a jbyteArray out of the byte * bytes:

jbyteArray resultByteArray = env->NewByteArray(size);
env->SetByteArrayRegion(resultByteArray, 0, size, bytes);
env->ReleaseByteArrayElements(yuv, _yuv, 0);

return resultByteArray;
edit flag offensive delete link more

Comments

what is bytes here ?? and ther is no byte data type in c++ ????

Pulkit gravatar imagePulkit ( 2016-12-26 23:30:47 -0600 )edit

typedef unsigned char byte;

using the above line byte will be defined. :)

BlacKKnighT74 gravatar imageBlacKKnighT74 ( 2019-12-23 02:00:39 -0600 )edit

Question Tools

Stats

Asked: 2014-05-19 04:25:50 -0600

Seen: 47,692 times

Last updated: Jun 24 '15