OpenCV2 多摄像头读取视频实现

//编程环境:VS2012,Opencv2.4.6
#include 
#include 

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
	
		VideoCapture cap1;
		VideoCapture cap2;
		cap1.open(0);
		cap2.open(1);
		if(!cap1.isOpened())
		{
			
			return -1;
		}
		if(!cap2.isOpened())
		{
			
			return -2;
		}



		double w = 320, h = 240;
		cap1.set(CV_CAP_PROP_FRAME_WIDTH,w);
	    cap1.set(CV_CAP_PROP_FRAME_HEIGHT,h);
	    cap2.set(CV_CAP_PROP_FRAME_WIDTH,w);
	    cap2.set(CV_CAP_PROP_FRAME_HEIGHT,h);



		Mat frame1,frame2;
		bool stop = false;
		namedWindow("Video1");
		namedWindow("Video2");
		while(!stop)
		{
			cap1>>frame1;
			/*if (frame1.empty())
			{
				
				return -1;
			}*/
			imshow("Video1",frame1);



			cap2>>frame2;
			/*if (frame2.empty())
			{
				
				return -2;
			}*/
			imshow("Video2",frame2);
						
			if(waitKey(30) >=0)
			stop = true;
		}
		return 0;
	
	
}

C 风格代码  OpenCV1 实现,较为鲁棒,环境同上

#include 
using namespace cv;


int main(int argc, char* argv[])
{
	
    CvCapture* capture1 = cvCreateCameraCapture( 0 );
    CvCapture* capture2 = cvCreateCameraCapture( 1 );
	
	
	

    double w = 320, h = 240;
	
	

    cvSetCaptureProperty ( capture1, CV_CAP_PROP_FRAME_WIDTH,  w );  
    cvSetCaptureProperty ( capture1, CV_CAP_PROP_FRAME_HEIGHT, h );
    cvSetCaptureProperty ( capture2, CV_CAP_PROP_FRAME_WIDTH,  w );  
    cvSetCaptureProperty ( capture2, CV_CAP_PROP_FRAME_HEIGHT, h );
	
	
    cvNamedWindow( "Camera_1", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "Camera_2", CV_WINDOW_AUTOSIZE );
	

    IplImage* frame1;
    IplImage* frame2;
	
	
    while(1)
    {
        frame1 = cvQueryFrame( capture1 );
        //if( !frame1 ) break;
        cvShowImage( "Camera_1", frame1 );
		

        frame2 = cvQueryFrame( capture2 );
        //if( !frame2 ) break;
        cvShowImage( "Camera_2", frame2 );
		


        int key = cvWaitKey(30);
        if( key == 27 ) break;
    }
	
    cvReleaseCapture( &capture1 );
    cvReleaseCapture( &capture2 );
	
    cvDestroyWindow( "Camera_1" );
    cvDestroyWindow( "Camera_2" );
	

    return 0;
}



你可能感兴趣的:(算法,opencv,计算机视觉)