Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convert 3 channel BGR image to bytes string in C++

I am trying to convert a BGR Mat image to bytes string in C++. So that I can pass it to a python server (along with its spatial dimensions values) where I can reconstruct the image. The code I used is:

//opencvimage is the Mat object (BGR image)
string matAsString(opencvimage.begin<unsigned char>(), opencvimage.end<unsigned char>());

This however extracts only the first channel of the image (I assume it is the Blue channel). How can I get all the 3 channels into the string so that when reconstruct in python:

image = numpy.fromarray(matAsString,dtype=np.uint8).reshape(rows,cols,3)

I get the exact same image?