11

There is anyway to convert opencv mat object to base64.

I was using the below url for base64 encoding and decoding:

http://www.adp-gmbh.ch/cpp/common/base64.html

Below is the code snippet:

const unsigned char* inBuffer = reinterpret_cast(image.data);

Amarnath R
  • 973
  • 3
  • 14
  • 33
  • 1
    Possibly related: http://stackoverflow.com/questions/28003981/opencv-cvmat-to-stdifstream-for-base64-encoding – NathanOliver Apr 21 '15 at 12:37
  • **why** are you trying to do this ? encoding binary data , like an image to base64 will lead to horrible bloat. – berak Apr 21 '15 at 12:57
  • To communicate the image data between c++ and java. I could easily convert the image data to base64 using java. whereas converting opencv mat to base64 is a hectic – Amarnath R Apr 21 '15 at 13:31

5 Answers5

15

There you go! (C++11)
Encode img -> jpg -> base64 :

        std::vector<uchar> buf;
        cv::imencode(".jpg", img, buf);
        auto *enc_msg = reinterpret_cast<unsigned char*>(buf.data());
        std::string encoded = base64_encode(enc_msg, buf.size());

Decode base64 -> jpg -> img :

        string dec_jpg =  base64_decode(encoded);
        std::vector<uchar> data(dec_jpg.begin(), dec_jpg.end());
        cv::Mat img = cv::imdecode(cv::Mat(data), 1);

Note that you can change JPEG compression quality by setting the IMWRITE_JPEG_QUALITY flag.

Jean-Christophe
  • 485
  • 4
  • 7
3

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);
Jason
  • 53
  • 8
3

I have created an example for this using Qt5 and OpenCV:

cv::Mat1b image;
this->cameraList[i]->getImage(image);
std::vector<uint8_t> buffer;
cv::imencode(".png", image, buffer);
QByteArray byteArray = QByteArray::fromRawData((const char*)buffer.data(), buffer.size());
QString base64Image(byteArray.toBase64());
base64ImageList.append(base64Image);
Fernando
  • 1,477
  • 2
  • 14
  • 33
0

I was looking for a solution to the same problem. Using Jean-Christophe's answer above, this worked for me:

cv::Mat image = cv::imread("path/to/file");
std::vector<uchar> buffer;
buffer.resize(static_cast<size_t>(image.rows) * static_cast<size_t>(image.cols));
cv::imencode(".jpg", image, buffer);
std::string encoding = base64_encode(buffer.data(), buffer.size());

Also, c++ std does not have a base64_encode implementation so you can look at this answer which aggregated a bunch of implementations.

0

Without using opencv, we can convert the image or file into base 64.Read the file byte by byte, store it in a buffer and base64 encode it. Cheers!

FILE* f = fopen(imagePath, "rb");
    fseek(f, 0, SEEK_END);
    size_t length = ftell(f);
    rewind(f);

    BYTE* buffer = (BYTE*)malloc(length + 2);

    while ((!feof(f))) {
        BYTE c;
        if (fread(&c, 1, 1, f) == 0) break;                                                 //read byte by byte of the PNG image file
        buffer[i++] = (int)c;
    }
    fclose(f);

    string base64String = base64_encode(&buffer[0], i + 1);
Niyas
  • 47
  • 4