c++ - Loading an image from a URL into a variable/opencv Mat -


bool loadimage(string inputname, mat &image) {   bool from_net = true;    if (inputname.find("http") != string::npos)     {       string url = inputname;       if (inputname.find("\"") == 0)         {           url = inputname.substr(1,inputname.length()-2);         }       ofstream myfile;       myfile.open ("test.jpg");       //  create writer handle stream        curl_writer writer(myfile);       // pass easy constructor , watch content returned in file!       curl_easy easy(writer);        // add option easy handle       easy.add(curl_pair<curloption,string>(curlopt_url,url));       easy.add(curl_pair<curloption,long>(curlopt_followlocation,1l));       try {         easy.perform();       } catch (curl_easy_exception error) {         // if want entire error stack can do:         vector<pair<string,string>> errors = error.what();         // otherwise print stack this:         //error.print_traceback();       }       myfile.close();        string inputname = "test.jpg";       image = imread(inputname,1);        if(image.rows == 0 || image.cols == 0)           from_net = false;     }     else     {       image = imread( inputname, 1 );       if (image.total() < 1)                    from_net = false;      }   return from_net;  } 

and works fine application, if change test.txt test.jpg. however, application demands avoid overhead of creating file, reading, writing , closing it. there easy , direct way image data url , write opencv mat ?

i tried 3rd example in above link. reason when receiver.get_buffer(), image remains empty. image dimensions 0x0.

any related appreciated. have never used curlcpp before , so, detailed explanation same appreciated.

thanks.

there simple solution this, bad not noticing earlier. can write data ostringstream. please see code below details.

bool loadimage(string inputname, mat &image) {   bool from_net;   from_net = true;     if (inputname.find("http") != string::npos)     {       string url;       url = inputname;       if (inputname.find("\"") == 0)         {            url = inputname.substr(1,inputname.length()-2);         }    std::ostringstream stream;    curl_writer writer(stream);   // pass easy constructor , watch content returned in file!   curl_easy easy(writer);    // add option easy handle   easy.add(curl_pair<curloption,string>(curlopt_url,url));   easy.add(curl_pair<curloption,long>(curlopt_followlocation,1l));    try {     easy.perform();   } catch (curl_easy_exception error) {     // if want entire error stack can do:     vector<pair<string,string>> errors = error.what();     // otherwise print stack this:     error.print_traceback();   }   string output = stream.str(); // convert stream string   if (output.find("404 not found") != string::npos)     from_net = false;   else   {       vector<char> data = std::vector<char>( output.begin(), output.end() ); //convert string vector    if (data.size() > 0)     {       mat data_mat = mat(data); // create cv::mat datatype vector       image = imdecode(data_mat,-1); //read image memory buffer       if(image.rows == 0 || image.cols == 0)     from_net = false;     }   else     from_net = false;     } }   else     {       image = imread( inputname, 1 );       if (image.total() < 1)                  from_net = false;        }   return from_net; } 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -