1 | initial version |
First, your image is only 20MB, not 20GB; so your memory should be enough.
The problem is that the Mat class is only a header; the image data is kept elsewhere and managed separately (and you don't send it).
So instead of sending the Mat variable, send a (text) header containing the image size (img.rows, img.cols), image type and buffer size; then send the img.data as binary data. At the client side you should be able to recreate the Mat variable (Mat img(rows,cols,type,data);
).
A simpler and more robust solution is to encode the image data to a buffer and send this buffer:
std::vector<uchar> buf;
std::vector<int> compression;
compression.push_back(CV_IMWRITE_JPEG_QUALITY);
compression.push_back(80);
cv::imencode(".JPG",img,buf,compression);
Then send the buf
vector over the network. On the client side call img = imdecode(buf);
.
This solution will also compress the image, so you'll gain in bandwidth. The example above uses JPG format with a compression factor of 80. You can increase the quality according to your needs, or use other formats, like lossless grayscale PNG (attention, PNG encoding for large images takes more time).