OpenCV 错误:断言失败 (buf.data && buf.isContinu()) in cv::imdecode_, file ..\..\..\..\openCV
OpenCV Error: Assertion failed (buf.data && buf.isContinuous()) in cv::imdecode_, file ........opencvmoduleshighguisrcloadsave.cpp
我正试图在visualstudio 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,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- OpenCV C++.快速计算混淆矩阵
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- 哪些库可以通过Opencv调整曝光率
- 安装opencv失败-粘贴CMakeError.log的内容
- C++中的openCV Mat访问冲突
- OpenCV Android C++ imwrite not found
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- CV_OCL_RUN宏如何在OpenCV(版本3.4.5)的goodFeaturesToTrack实现中工作?
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- C++ OpenCV 卡尔曼滤波器构造函数错误
- 使用OpenCV和覆盆子上的多个网络摄像头拍摄延时摄影,出现多个V4L错误
- 如何使用OpenCV-C++编写*.mp4视频?
- OpenCV 错误:断言失败 (buf.data && buf.isContinu()) in cv::imdecode_, file ..\..\..\..\openCV