I have two cameras that I need to display video from at the same time, either in separate windows or in the same window. However, using the following code, only one of the camera feeds (camera(1)) displays. Could someone point out what needs to be changed in my code, or link to other code that would achieve the desired effect?
N.B. This is NOT for stereo vision.
int main()
{
//initialize and allocate memory to load the video stream from camera
CvCapture *capture1 = cvCaptureFromCAM(0);
if( !capture1 ) return 1;
//create a window with the title "Video1"
cvNamedWindow("Video1");
while(true) {
//grab and retrieve each frames of the video sequentially
IplImage* frame1 = cvQueryFrame( capture1 );
if( !frame1 ) break;
//show the retrieved frame in the "Video1" window
cvShowImage( "Video1", frame1 );
//wait for 40 milliseconds
int c = cvWaitKey(40);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if((char)c==27 ) break;
}
//initialize and allocate memory to load the video stream from camera
CvCapture *capture2 = cvCaptureFromCAM(1);
if( !capture2 ) return 1;
//create a window with the title "Video2"
cvNamedWindow("Video2");
while(true) {
//grab and retrieve each frames of the video sequentially
IplImage* frame2 = cvQueryFrame( capture2 );
if( !frame2 ) break;
//show the retrieved frame in the "Video2" window
cvShowImage( "Video2", frame2 );
//wait for 40 milliseconds
int c = cvWaitKey(40);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if((char)c==27 ) break;
}
//destroy the opened window
cvDestroyWindow("Video1");
cvDestroyWindow("Video2");
//release memory
cvReleaseCapture( &capture1 );
cvReleaseCapture( &capture2 );
return 0;
//VideoCapture1();
//VideoCapture2();
}
Just to clarify, are you using C or C++? If you are using C++, please use C++ interfaces of OpenCV.
Example below works for me:
#include <opencv2/opencv.hpp>
int main()
{
//initialize and allocate memory to load the video stream from camera
cv::VideoCapture camera0(0);
cv::VideoCapture camera1(1);
if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
while(true) {
//grab and retrieve each frames of the video sequentially
cv::Mat3b frame0;
camera0 >> frame0;
cv::Mat3b frame1;
camera1 >> frame1;
cv::imshow("Video0", frame0);
cv::imshow("Video1", frame1);
//wait for 40 milliseconds
int c = cvWaitKey(40);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With