I'm trying to make a live stereovision setup using OpenCV in C++ and two webcams. It is possible to seperately get frames from the two webcams. However, when I try to access them simultaneously in threads, I get a runtime error:
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
My code is as follows:
#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;
}
Anybody got any idea what could be causing this? I'm using CLion on Ubuntu 15.10 LTE.
UPDATE 1: I'm using index 1 and 2 for the camera's because I have a built-in camera in my laptop (0) and two USB camera's (1 and 2) plugged in. The USB camera's are the target hardware here.
UPDATE 2: Putting both camera feeds in one thread (see code below) works just fine (assuming your USB ports use separate busses, otherwise you'll get a 'NO SPACE LEFT ON DEVICE' error), but the delay between the two frames is noticeable, which is not ideal for a stereovision setup.
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;
}
Maybe, I have stupid answer. Your code works fine..
Count cameras from zero.
thread cam1 (stream, 0); THIS works
// thread cam2 (stream, 1); I dont have second camera
First of all, I am on windows machine. My cameras are count from 0,1,2 I do not have second web camera.
In the picture i just use your code and paste cam(0) my web camera and for the second camera i use testing rtsp stream.
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.