2013年7月19日 星期五

[筆記] 在OpenCV2及OpenCV1抓取網路攝影機的影像 How to get image from webcam in opencv

抓取網路攝影機的影像,更新頻率是每10毫秒抓一次,

可以更改waitKey(10)裡面的值,單位是毫秒

原始碼如下

For OpenCV2
#include < opencv2/highgui/highgui.hpp >
#include < iostream >

using namespace std;
using namespace cv;
            
int main ( int argc, char **argv ){
 VideoCapture capture(1); // -1 any video 0 default 1~99 select
 if(!capture.isOpened()){     
  cout << "could not read file" << endl;  
  return -1;        
 }
 //double rate=capture.get(CV_CAP_PROP_FPS);
 //bool stop(false);
 Mat imgInput;
 namedWindow("Extract frame");
 while(1){
  if(!capture.read(imgInput)) break;
  imshow("Extract frame", imgInput);
  //delay 10 milliseconds
  if(waitKey(10)>=0){
   cout << "stop" << endl;
   capture.release();
   break;
  }
 }
 return 0;
}    
For OpenCV 1

#include < opencv2/highgui/highgui.hpp >
#include < iostream >

using namespace std;
using namespace cv;

int main ( int argc, char **argv ){
 CvCapture* capture = 0;
 Mat frame, frameCopy, image;
 capture = cvCaptureFromCAM( 1 ); //0=default, -1=any camera, 1..99=your camera
 if( !capture )
 {
  cout << "No camera detected" << endl;
 }
 cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
 if( capture )
 {
  cout << "In capture ..." << endl;
  for(;;)
  {
   IplImage* iplImg = cvQueryFrame( capture );
   cvShowImage( "result", iplImg );

   if( waitKey( 10 ) >= 0 )
    break;
  }
 }
 cvReleaseCapture( &capture );
 cvDestroyWindow( "result" );
 return 0;
}    

創用CC,轉貼或引用請註明出處

沒有留言:

張貼留言