c++ - 使用 opencv 同时显示多个摄像头提要?

标签 c++ opencv camera

我有两个摄像头,需要同时在不同的窗口或同一窗口中显示视频。但是,使用以下代码,仅显示一个摄像头源 (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();


}

最佳答案

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

下面的例子对我有用:

#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;
}

关于c++ - 使用 opencv 同时显示多个摄像头提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13925867/

相关文章:

c++ boost进程间交换(复制)非共享和共享字符串 vector

c++ - 导致访问冲突的 realloc 实现问题

c++ - 如何正确解除分配或删除 C++ vector ?

android - 当 fragment 包含相机预览时 ViewPager 滑动滞后

C++继承、赋值

c++ - OpenCV C++ 中的映射函数

c++ - 当brew安装opencv3时,我得到警告,该公式没有--with-contrib选项

python - Python-OpenCv FindContours

android - 如何在 Android 上更改相机曝光?

android - 相机预览仅在 Android 4.2.2(S4,全高清屏幕)上显示不需要的图 block