Convert 3 channel BGR image to bytes string in C++ [closed]

asked 2018-04-02 23:06:07 -0500

abhijit gravatar image

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?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2018-04-03 10:51:55.971509


why do you think, you need a string , to send a (binary) image over the wire ?

berak gravatar imageberak ( 2018-04-03 00:29:56 -0500 )edit

Am integrating it with python using pybind11. So it's easier to transfer via byte string

abhijit gravatar imageabhijit ( 2018-04-03 01:24:39 -0500 )edit

if at all, rather use:

string matAsString(opencvimage.datastart, opencvimage.dataend);

and then, careful, this is NOT unicode (which python might expect, idk.)

maybe it's better, to imencode() it into a png, and send that over the wire

berak gravatar imageberak ( 2018-04-03 01:28:17 -0500 )edit

Thanks the string matAsString(opencvimage.datastart, opencvimage.dataend); works

abhijit gravatar imageabhijit ( 2018-04-03 04:13:34 -0500 )edit