gpt4 book ai didi

c++ - OpenCV 错误:断言失败 (buf.data && buf.isContinuous()) 在 cv::imdecode_,文件 ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp

转载 作者:行者123 更新时间:2023-11-28 02:08:11 27 4
gpt4 key购买 nike

我正在尝试在 visual studio 2010 中使用 opencv 和 curl 从 url 加载图像。运行代码时出现上述错误。该错误是什么意思?如何更正我的代码。这是我的代码

#include "curl/curl.h" // has to go before opencv headers

#include <iostream>
#include <vector>
using namespace std;

#include <opencv2/opencv.hpp>
using namespace cv;

//curl writefunction to be passed as a parameter
// we can't ever expect to get the whole image in one piece,
// every router / hub is entitled to fragment it into parts
// (like 1-8k at a time),
// so insert the part at the end of our stream.
size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata)
{
vector<uchar> *stream = (vector<uchar>*)userdata;
size_t count = size * nmemb;
stream->insert(stream->end(), ptr, ptr + count);
return count;
}

//function to retrieve the image as cv::Mat data type
cv::Mat curlImg(const char *img_url, int timeout=10)
{
vector<uchar> stream;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, img_url); //the img url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); // pass the writefunction
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); // pass the stream ptr to the writefunction
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); // timeout if curl_easy hangs,
CURLcode res = curl_easy_perform(curl); // start curl
curl_easy_cleanup(curl); // cleanup
return imdecode(stream, -1); // 'keep-as-is'
}

int main(void)
{
Mat image = curlImg("http://pimg.tradeindia.com/01063301/b/1/CRO-Oscilloscope.jpg");
// if (image.empty())
// return -1; // load fail

namedWindow( "Image output", CV_WINDOW_AUTOSIZE );
if (!image.empty())
imshow("Image output",image); // here's your car ;)
waitKey(0); // infinite
}

感谢您的帮助。

最佳答案

此错误与传递给 opencv imdecode() 的流的大小有关。尝试检查流的大小并查看其是否正确,即非零或非空。

关于c++ - OpenCV 错误:断言失败 (buf.data && buf.isContinuous()) 在 cv::imdecode_,文件 ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36701051/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com