1. 程式人生 > >在ubuntu下利用opencv開啟攝像頭

在ubuntu下利用opencv開啟攝像頭

測試環境:ubuntu10.04 + opencv2.3.0

在安裝opencv之前要裝上以下依賴庫:

sudo apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev

sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev 

sudo apt-get install libxine1-ffmpeg libxine-dev libxine1-bin 

sudo apt-get install libunicap2 libunicap2-dev 

sudo apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils 

sudo apt-get install swig 

sudo apt-get install libv4l-0 libv4l-dev 

sudo apt-get install python-numpy 

sudo apt-get install libpython2.6 python-dev python2.6-dev #You must install this for python support


原始碼如下所示:

 #include <iostream>
 #include <stdio.h>
 #include <cxcore.h>
 #include <cvaux.h>
 #include <highgui.h>
 #include <cv.h>
 
 int main(int argc,char **argv)
 {
        IplImage * pFrame = NULL;
        CvCapture * pCapture = NULL;
        cvNamedWindow("video",1);
        cvMoveWindow("video",30,0);
        if(argc == 1)
        {
                if(!(pCapture = cvCaptureFromCAM(-1)))
                {
                        fprintf(stderr,"Can not open camera.\n");
                        return -2;
                }
        }
        while(pFrame = cvQueryFrame(pCapture))
        {
                cvShowImage("video",pFrame);
                if(cvWaitKey(2) >= 0)
                {
                        break;
                }
        }
        cvDestroyWindow("video");
        return 0;
 }


 g++ `pkg-config --cflags --libs opencv` open_video.cpp -o test

然後執行  ./test

視訊就出現了!