简体   繁体   English

将图像从URL加载到变量/ opencv Mat中

[英]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 a writer to handle the stream

      curl_writer writer(myfile);
      // Pass it to the easy constructor and watch the content returned in that file!
      curl_easy easy(writer);

      // Add some option to the 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 you want to get the entire error stack we can do:
        vector<pair<string,string>> errors = error.what();
        // Otherwise we could print the stack like 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 this works fine for my application, if I change test.txt to test.jpg . 如果我将test.txt更改为test.jpg ,那么这对于我的应用程序就可以正常工作。 However, my application demands that I avoid the overhead of creating the file, reading, writing and closing it. 但是,我的应用程序要求我避免创建文件,读取,写入和关闭文件的开销。 Is there an easy and direct way to get the image data from the URL and write it to an openCV Mat ? 是否有一种简单直接的方法来从URL获取图像数据并将其写入openCV Mat?

I also tried the 3rd example in the above link. 我还在上面的链接中尝试了第三个示例。 But for some reason when I do a receiver.get_buffer() , the image remains empty. 但是由于某些原因,当我执行receiver.get_buffer() ,图像仍然为空。 I get image dimensions as 0X0 . 我得到的图像尺寸为0X0

Any help related to this is really appreciated. 与此有关的任何帮助都非常感谢。 I have never used curlcpp before and so, any detailed explanation for the same would be much appreciated. 我以前从未使用过curlcpp,因此,对其进行任何详细说明将不胜感激。

Thanks. 谢谢。

There is a simple solution to this, my bad for not noticing this earlier. 有一个简单的解决方案,我很早没有注意到这一点。 You can write the data to ostringstream. 您可以将数据写入ostringstream。 Please see the code below for 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 it to the easy constructor and watch the content returned in that file!
  curl_easy easy(writer);

  // Add some option to the 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 you want to get the entire error stack we can do:
    vector<pair<string,string>> errors = error.what();
    // Otherwise we could print the stack like this:
    error.print_traceback();
  }
  string output = stream.str(); // convert the stream into a 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 into a vector 
  if (data.size() > 0)
    {
      Mat data_mat = Mat(data); // create the cv::Mat datatype from the vector
      image = imdecode(data_mat,-1); //read an image from 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM