簡體   English   中英

OpenCV錯誤:cv :: imdecode_,文件.. \\ .. \\ .. \\ .. \\ opencv \\ modules \\ highgui \\ src \\ loadsave.cpp中的斷言失敗(buf.data && buf.isContinuous())

[英]OpenCV Error: Assertion failed (buf.data && buf.isContinuous()) in cv::imdecode_, file ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp

我試圖在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()的流的大小有關。 嘗試檢查流的大小,並查看其是否正確,即非零或非空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM