[英]'Cannot allocate memory' - OpenCV multiple webcam stream
我正在嘗試使用C ++中的OpenCV和兩個網絡攝像頭進行實時的立體視覺設置。 可以從兩個網絡攝像頭分別獲取幀。 但是,當我嘗試同時在線程中訪問它們時,出現運行時錯誤:
VIDIOC_STREAMON: Cannot allocate memory
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/lorre851/Downloads/opencv-3.1.0/modules/highgui/src/window.cpp, line 281
terminate called after throwing an instance of 'cv::Exception'
what(): /home/lorre851/Downloads/opencv-3.1.0/modules/highgui/src/window.cpp:281: error: (-215) size.width>0 && size.height>0 in function imshow
我的代碼如下:
#include <iostream>
#include <thread>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void stream(int camera) {
VideoCapture cap(camera); // open the default camera
if(cap.isOpened()) { // check if we succeeded
while(true) {
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Stream " + to_string(camera), frame);
if (waitKey(30) >= 0) break;
}
}
}
int main() {
thread cam1 (stream, 1);
thread cam2 (stream, 2);
cam1.join();
cam2.join();
return 0;
}
有人知道是什么原因造成的嗎? 我在Ubuntu 15.10 LTE上使用CLion。
更新1:我將索引1和2用於攝像機,因為我在筆記本電腦(0)中插入了一個內置攝像機,並插入了兩個USB攝像機(1和2)。這里是目標USB攝像機。
更新2:將兩個攝像頭饋送放入一個線程中(請參見下面的代碼)就可以很好地工作(假設您的USB端口使用單獨的總線,否則會出現“設備上沒有空格”),但兩幀之間存在延遲值得注意,這對於stereovision設置而言並不理想。
cv::VideoCapture camera0(0);
cv::VideoCapture camera1(1);
if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
cv::Mat3b frame0;
cv::Mat3b frame1;
while(true) {
camera0 >> frame0;
camera1 >> frame1;
if(mat_is_empty(frame0)) cout << "SKIPPED FRAME IN 0";
else cv::imshow("Stream 0", frame0);
if(mat_is_empty(frame1)) cout << "SKIPPED FRAME IN 1";
else cv::imshow("Stream 1", frame1);
int c = cvWaitKey(40);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}
也許我有愚蠢的答案。 您的代碼工作正常。
從零開始計數攝像機。
thread cam1 (stream, 0); THIS works
// thread cam2 (stream, 1); I dont have second camera
首先,我在Windows機器上。 我的相機從0,1,2開始計數,我沒有第二個網絡相機。
在圖片中,我只使用您的代碼並將cam(0)粘貼到我的網絡攝像機中,對於第二個攝像機,我將使用測試rtsp流。
#include <iostream>
#include <thread>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void stream(int camera) {
VideoCapture cap;
if (camera == 0) {
cap.open(0); // open the default camera
}
if (camera == 1) {
cap.open("rtsp://mpv.cdn3.bigCDN.com:554/bigCDN/definst/mp4:bigbuckbunnyiphone_400.mp4"); // open the default camera
}
if (cap.isOpened()) { // check if we succeeded
while (true) {
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Stream " + to_string(camera), frame);
if (waitKey(30) >= 0) break;
}
}
}
int main() {
thread cam1(stream, 0);
thread cam2(stream, 1);
cam1.join();
cam2.join();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.