Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convert Mat to byte[] in C++

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[size] bytes;
   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

Convert Mat to byte[] in C++

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[];
byte[] matToBytes(Mat image)
{
int size = image.rows*image.cols;
byte[size] bytes;
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

Convert Mat to byte[] in C++

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;

byte[] matToBytes(Mat image)
{
   int size = image.rows*image.cols;
   byte[size] bytes;
   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

Convert Mat to byte[] in C++

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[size] bytes;
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

Convert Mat to byte[] in C++

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