4

我有两台摄像机,需要同时显示来自不同窗口或同一窗口的视频。但是,使用以下代码,仅显示一个摄像头馈送 (camera(1))。有人可以指出我的代码中需要更改的内容,或者链接到可以达到预期效果的其他代码吗?

注意这不适用于立体视觉。

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();


}
4

3 回答 3

11

澄清一下,您使用的是 C 还是 C++?如果您使用 C++,请使用 OpenCV 的 C++ 接口。

下面的例子对我有用:

#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;
}
于 2012-12-18T05:33:31.983 回答
3

试试这个:

    CvCapture *capture1 = cvCaptureFromCAM(0);
    CvCapture *capture2 = cvCaptureFromCAM(1);

    if( !capture1 ) return 1;
    if (!capture2) return 1 ; 
    cvNamedWindow("Video1");
    cvNamedWindow("Video2") ;

    while(true) {
        //grab and retrieve each frames of the video sequentially 
        IplImage* frame1 = cvQueryFrame( capture1 );
        IplImage* frame2 = cvQueryFrame( capture2 );

        if( !frame1 || !frame2 ) break;

        cvShowImage( "Video1", frame1 );
        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;
    }
于 2012-12-18T03:47:05.043 回答
2

将 frame1 和 frame2 的声明放在 while 循环之外

我在创建 WebCam 类时遇到了同样的问题。对于内部声明帧和捕获的单个实例工作正常,但多个实例需要构造函数同时传递帧和捕获指针。

openCV 貌似不能多线程一些重要的设施

顺便说一句,使用 X11,要使多线程与 cvWaitKey() 一起工作,您需要链接 libX11.so.6 并调用 XInitThreads(); 作为 main() 中的第一条指令

希望这对戴夫莱恩有所帮助。

于 2014-02-02T15:49:59.443 回答