在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 
 #include 
 #include 
 #include 
 #include 
 #include 
 
 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

视频就出现了!在ubuntu下利用opencv打开摄像头_第1张图片

你可能感兴趣的:(opencv学习)