openCV打开摄像头的两个方法

方法一:

利用cvCreateCameraCapture()函数,只有一个设备的情况下默认为-1

程序如下

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv ) { 
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    CvCapture* capture = cvCreateCameraCapture(-1); 
	IplImage* frame;
    while(1){
        frame = cvQueryFrame( capture);
        if( !frame ) break;
        cvShowImage( "Example1", frame );
        char c = cvWaitKey(33);
        if( c == 27 ) break;
    }
    cvReleaseCapture( &capture);	
	cvDestroyWindow( "Example1" );
}


 

方法二:

利用cvCaptureFromCAM() ,原函数形式是IplImage* cvCaptureFromCAM(int index)

程序如下

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv ) { 
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
	CvCapture* capture = cvCaptureFromCAM(0);
	IplImage* frame;
    while(1){
        frame = cvQueryFrame( capture);
        if( !frame ) break;
        cvShowImage( "Example1", frame );
        char c = cvWaitKey(33);
        if( c == 27 ) break;
    }
    cvReleaseCapture( &capture);	
	cvDestroyWindow( "Example1" );
}


 

 

你可能感兴趣的:(c,c,opencv,机器视觉,摄像头)