I'm encountering nearly the same problem, but I'm trying to encode a Mat into jpeg format and then convert it into base64.
The code on that page works fine!
So here is my code:
VideoCapture cam(0);
cam>>img;
vector<uchar> buf;
imencode(".jpg", img, buf);
uchar *enc_msg = new uchar[buf.size()];
for(int i=0; i < buf.size(); i++) enc_msg[i] = buf[i];
string encoded = base64_encode(enc_msg, buf.size());
if you just want to convert a Mat into base64, you need make sure the Mat size and channels. For a CV_8UC1, this will work:
string encoded = base64_encode(img.data, img.rows * img.cols);