将图像从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;
}
如果我将test.txt
更改为test.jpg
,这对我的应用程序来说效果很好。但是,我的应用程序要求我避免创建文件、读取、写入和关闭文件的开销。有没有一种简单直接的方法可以从URL获取图像数据并将其写入openCV Mat?
我还尝试了上面链接中的第三个示例。但是由于某种原因,当我做receiver.get_buffer()
时,图像仍然是空的。我得到的图像尺寸为0X0
.
与此相关的帮助都非常感谢。我以前从未使用过curlcpp,因此,任何详细的解释将不胜感激。
谢谢。
有一个简单的解决方案,我很遗憾没有早点注意到这一点。您可以将数据写入 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;
}
相关文章:
- C++中的openCV Mat访问冲突
- 如何通过引用传递OpenCV MAT
- Read OpenCV Mat 16bit to QImage 8bit Greyscale
- 将本地 OpenCV Mat 变量声明为静态以防止重新分配
- 将 OpenCv Mat 从 C++ 传递给 Python
- 将帧从 h264 流解码到 OpenCV Mat
- 如果函数使用 OPENCV Mat 作为输入,如何编写头文件?
- 无法从Java读取C++中保存的Opencv Mat映像
- 如何通过套接字发送OpenCV Mat
- 如何有效地在OpenCV Mat和GpuMat上进行元素处理?
- 什么是使用 opencv::Mat 优化 c++ 矩阵计算
- openCV Mat 中的值在自定义类构造函数中设置后不会保留
- OpenCV Mat 的逗号分隔初始值设定项是如何用C++实现的?
- Libav AVFrame to Opencv Mat to AVPacket conversion
- 采用OpenCV Mat<doube>并转换为12位值数组。
- 如何将C++数组转换为opencv Mat
- 通过QT WebSocket将OpenCV Mat Image发送到HTML客户端
- OpenCV Mat::row assignment
- 如何在 OpenCV Mat 中保留固定的缓冲区大小
- OPENCV MAT问题:直方图和此循环之间的差异