0

Using open cv I am getting descriptors in cv::Mat format. Converting cv::Mat to stringstream is happening with this method:

/// Serialize a cv::Mat to a stringstream
stringstream serialize(Mat input)
{
    // We will need to also serialize the width, height, type and size of the matrix
    int width = input.cols;
    int height = input.rows;
    int type = input.type();
    size_t size = input.total() * input.elemSize();

    // Initialize a stringstream and write the data
    stringstream ss;
    ss.write((char*)(&width), sizeof(int));
    ss.write((char*)(&height), sizeof(int));
    ss.write((char*)(&type), sizeof(int));
    ss.write((char*)(&size), sizeof(size_t));

    // Write the whole image data
    ss.write((char*)input.data, size);

    return ss;
}

Now i want to convert this stringstream into base64 encoded. How to make it possible in direct method.

Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
Raja S
  • 133
  • 11
  • I usually use [this piece of code](http://www.adp-gmbh.ch/cpp/common/base64.html) to perform base64 coding/encoding. But can't you just send the YAML/XML? – Miki Oct 30 '15 at 10:37
  • Also, in your serialization you should take care of the case when image is not continuous. Look [here](http://stackoverflow.com/a/32357875/5008845) for reference, at `matwrite` method – Miki Oct 30 '15 at 10:49
  • But i referred the post they mentioned base 64 is best option.. thats why am converting.. which one is best.. xml or yaml or base64??? – Raja S Oct 30 '15 at 11:09
  • i just want to send the descriptors(cv::Mat format) to webserver.. kindly suggest.. am using opencv 3.0 – Raja S Oct 30 '15 at 11:10
  • Hi i just want to convert stringstream format to base64 encode.. but you mention from string to base64 conversion.. – Raja S Oct 30 '15 at 11:14
  • I don't know enough about webservers or networks to recommend base64 or YAML/XML. Probably first is more compact, second easier. However, improve your `serialize` function as in the link, and encode to base64 with the code of the first link. That's it – Miki Oct 30 '15 at 11:14
  • `ss.str()` returns ta string – Miki Oct 30 '15 at 11:15
  • Hi i dont want to convert it into string.. i just want this stringstream to be converted directly to the base64 encode.. do you know any base64 lib to convert it.. i have one link but i cant connect it to the project.. Here is the link for the library.. http://libb64.sourceforge.net/... Got this link from http://stackoverflow.com/questions/28003981/opencv-cvmat-to-stdifstream-for-base64-encoding – Raja S Oct 30 '15 at 11:38
  • Since that lib is in ANSI C, then it's unlikely that it works directly on `stringstream`. What's the matter to get the string from the stringstream? – Miki Oct 30 '15 at 11:45
  • So you are suggesting that convert mat to string and string to base64 to send data to the webserver.. am i right??? so here is a method to convert mat to string... // cvmat to nsstring.. - (NSString*) NSStringFromCvMat:(cv::Mat)mat { std::stringstream ss; ss << mat; return [NSString stringWithCString:ss.str().c_str() encoding:NSASCIIStringEncoding]; } – Raja S Oct 30 '15 at 11:50
  • and from string to base64 conversion have to use your link http://www.adp-gmbh.ch/cpp/common/base64.html \ – Raja S Oct 30 '15 at 11:51
  • No, I'm suggesting to serialize the matrix with stringstream and encode the string you get from the stringstream. You can use that link for decoding, or whatever you prefer. – Miki Oct 30 '15 at 11:53
  • // cvmat to nsstring.. - (NSString*) NSStringFromCvMat:(cv::Mat)mat { std::stringstream ss; ss << mat; return [NSString stringWithCString:ss.str().c_str() encoding:NSASCIIStringEncoding]; } Is this way right??? getting nsstring from cvmat.. then encode the string.. – Raja S Oct 30 '15 at 11:57
  • use your `serialize` function to serialize to stringstream – Miki Oct 30 '15 at 11:58
  • Don't know objectiveC, but something like: `std::string s = serialize(mat).str(); std::string encoded = encode_base64(s);` where `encode_base64` is your encoding function (as in the link, for example) – Miki Oct 30 '15 at 12:00
  • Ya that is done.. Here it is.. std::stringstream ss; ss = serialize(descriptors1); next what i have to do?? – Raja S Oct 30 '15 at 12:01

1 Answers1

0

You can encode the string obtained by the stringstream like:

Mat m;
...
std::string s = serialize(m).str();
std::string encoded = base64_encode(s.c_str());
// send "encoded" to server

The code for base64_encode can be found, for example, here.

Miki
  • 40,887
  • 13
  • 123
  • 202
  • - (void) frameCaptured:(cv::Mat) frame{ cv::Mat greyMat; cv::cvtColor(frame, greyMat, CV_BGR2GRAY); // UIImage *finaleImg = [self UIImageFromCVMat:greyMat]; orb -> detect(greyMat, keypoints1); orb -> compute(greyMat, keypoints1, descriptors1); drawKeypoints(greyMat, keypoints1, outImage); std::string s = serialize(descriptors1).str(); std::string encoded = base64_encode(reinterpret_cast(s.c_str()), s.length()); NSLog(@"%s",encoded.c_str()); } – Raja S Nov 04 '15 at 11:45
  • Here am getting data from camera and creating descriptors in mat format.. now am serializing the mat format and encoded into base64 format.. – Raja S Nov 04 '15 at 11:46
  • Is this right?? Then how can i send it through the socket...?? – Raja S Nov 04 '15 at 11:49
  • I can't test your code. You should know if it works ok or not. The question is about getting base64 encoding, and this is what my answer is about. Sockets and network related stuff should go in another question. – Miki Nov 04 '15 at 12:07
  • For serialization am using this method...// Serialize a cv::Mat to a stringstream – Raja S Nov 05 '15 at 06:24
  • std::stringstream serialize(cv::Mat input) {// We will need to also serialize the width, height, type and size of the matrix int width = input.cols; int height = input.rows; int type = input.type(); size_t size = input.total() * input.elemSize(); // // Initialize a stringstream and write the data std::stringstream ss; ss.write((char*)(&width), sizeof(int)); ss.write((char*)(&height), sizeof(int)); ss.write((char*)(&type), sizeof(int)); ss.write((char*)(&size), sizeof(size_t)); ss.write((char*)input.data, size); return ss; } – Raja S Nov 05 '15 at 06:25