Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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;
}

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);
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;
}