1
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; 
}

如果我更改test.txttest.jpg. 但是,我的应用程序要求我避免创建文件、读取、写入和关闭文件的开销。是否有一种简单直接的方法可以从 URL 获取图像数据并将其写入 openCV Mat ?

我还尝试了上述链接中的第三个示例。但是由于某种原因,当我执行 a 时receiver.get_buffer(),图像仍然是空的。我得到的图像尺寸为0X0.

非常感谢与此相关的任何帮助。我以前从未使用过 curlcpp,因此,任何详细的解释都将不胜感激。

谢谢。

4

1 回答 1

1

有一个简单的解决方案,我很遗憾没有早点注意到这一点。您可以将数据写入ostringstream。详情请看下面的代码。

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;
}
于 2015-04-13T17:42:48.923 回答