开发者社区> java牛> 正文

Ubuntu下编译安装OpenCV 2.4.7并读取摄像头[转]

简介: 主要参考: 1、http://www.ozbotz.org/opencv-installation/ 2、http://www.ozbotz.org/opencv-install-troubleshooting/ 开发环境:VMware下Ubuntu+OpenCV2.
+关注继续查看

主要参考:

1、http://www.ozbotz.org/opencv-installation/

2、http://www.ozbotz.org/opencv-install-troubleshooting/

开发环境:VMware下Ubuntu+OpenCV2.4.7

安装过程:

The Installation Procedure

To install and configure OpenCV 2.4.1, complete the following steps. The commands shown in each step can be copy and pasted directly into a Linux command line.

  1. Remove any installed versions of ffmpeg and x264.

    sudo apt-get remove ffmpeg x264 libx264-dev

     

  2. Get all the dependencies for x264 and ffmpeg.

    sudo apt-get update
    sudo apt-get install build-essential checkinstall git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev

     

  3. Download and install gstreamer.

    sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg

     

  4. Download and install gtk.

    sudo apt-get install libgtk2.0-0 libgtk2.0-dev

     

  5. Download and install libjpeg.

    sudo apt-get install libjpeg8 libjpeg8-dev

     

  6. Create a directory to hold source code.

    cd ~
    mkdir src

     

  7. Download and install install x264.
    1. Download a recent stable snapshot of x264 from ftp://ftp.videolan.org/pub/videolan/x264/snapshots/. The exact version does not seem to matter. To write this guide, I used version x264-snapshot-20120528-2245-stable.tar.bz2, but I have used previous versions too.

      cd ~/src

      wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20120528-2245-stable.tar.bz2

      tar xvf x264-snapshot-20120528-2245-stable.tar.bz2

      cd x264-snapshot-20120528-2245-stable

    2. Configure and build the x264 libraries.

      ./configure --enable-static
      make
      sudo make install

      IMPORTANT: If you are running a 64-bit version of Ubuntu, you must configure x264 as shown in the following command:

      ./configure --enable-shared --enable-pic

      The -shared and -pic options might also be required when you compile for some other architectures, such as ARM. You know you need these options if you get the following error when compiling OpenCV:

      [ 25%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/bitstrm.cpp.o
      Linking CXX shared library ../../lib/libopencv_highgui.so
      /usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet' can not be used when making a shared object; recompile with -fPIC
      /usr/local/lib/libavcodec.a: could not read symbols: Bad value

       

     

  8. Download and install install ffmpeg.
    1. Download ffmpeg version 0.11.1 from http://ffmpeg.org/download.html.

      cd ~/src
      wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.bz2
      tar xvf ffmpeg-0.11.1.tar.bz2
      cd ffmpeg-0.11.1

    2. Configure and build ffmpeg.

      ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab

      make
      sudo make install

      IMPORTANT: Just like with x264 in the previous step, you must configure ffmpeg with the -shared and -pic options if you are running a 64-bit version of Ubuntu or some other architectures, such as ARM.

      ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic

     

  9. Download and install install a recent version of v4l (video for linux) from http://www.linuxtv.org/downloads/v4l-utils/. For this guide I used version 0.8.8.

    cd ~/src

    wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.tar.bz2

    tar xvf v4l-utils-0.8.8.tar.bz2
    cd v4l-utils-0.8.8
    make
    sudo make install

     

  10. Download and install install OpenCV 2.4.2.
    1. Download OpenCV version 2.4.2 from http://sourceforge.net/projects/opencvlibrary/files/

      cd ~/src

      wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.2/OpenCV-2.4.2.tar.bz2

      tar xvf OpenCV-2.4.2.tar.bz2

    2. Create a new build directory and run cmake:

      cd OpenCV-2.4.2/
      mkdir build
      cd build
      cmake -D CMAKE_BUILD_TYPE=RELEASE ..

    3. Verify that the output of cmake includes the following text:
      • found gstreamer-base-0.10
      • GTK+ 2.x: YES
      • FFMPEG: YES
      • GStreamer: YES
      • V4L/V4L2: Using libv4l
    4. Build and install OpenCV.

      make
      sudo make install

     

  11. Configure Linux.
    1. Tell linux where the shared libraries for OpenCV are located by entering the following shell command:

      export LD_LIBRARY_PATH=/usr/local/lib

      Add the command to your .bashrc file so that you don’t have to enter every time your start a new terminal. (注:我主要采用将export LD_LIBRARY_PATH=/usr/local/lib加入.bashrc file的方法,.bashrc file为隐藏文件,在主用户目录下,可用shell命令:ls -al查看)

      Alternatively, you can configure the system wide library search path. Using your favorite editor, add a single line containing the text /usr/local/lib to the end of a file named /etc/ld.so.conf.d/opencv.conf. In the standard Ubuntu install, the opencv.conf file does not exist; you need to create it. Using vi, for example, enter the following commands:

      sudo vi /etc/ld.so.conf.d/opencv.conf
      G
      o
      /usr/local/lib
      <Esc>
      :wq!

      After editing the opencv.conf file, enter the following command:

      sudo ldconfig /etc/ld.so.conf

      .

    2. Using your favorite editor, add the following two lines to the end of /etc/bash.bashrc:

      PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
      export PKG_CONFIG_PATH

 

After completing the previous steps, your system should be ready to compile code that uses the OpenCV libraries. The following example shows one way to compile code for OpenCV(这里最好先进行重启):

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

 

以上按照过程中,可能会遇到其他问题,许多都可参考《OpenCV Installation Troubleshooting Guide》,里面有详细的问题描述及解答,非常清楚。

最后g++编译的代码main.cpp如下:

复制代码
#include <cv.h> #include <opencv/highgui.h> #include<stdio.h> #include<opencv2/opencv.hpp> using namespace cv;  int main(int argc, char *argv[]) {       CvCapture* pCapture = cvCreateCameraCapture(0);       cvNamedWindow("Video", 1);        while(1)       {           IplImage* pFrame=cvQueryFrame( pCapture );           if(!pFrame)break;           cvShowImage("Video",pFrame);           char c=cvWaitKey(33);           if(c==27)break;       }       cvReleaseCapture(&pCapture);       cvDestroyWindow("Video");       return 0; }
复制代码

读取摄像头结果如下图:

More Information

若转载请注明出处!若有疑问,请回复交流!

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Python+Opencv读取高帧率USB摄像头问题
Python+Opencv读取高帧率USB摄像头问题
79 0
ubuntu删除OpenCV
ubuntu删除OpenCV
39 0
使用Python+Opencv从摄像头逐帧读取图片保存在本地
使用Python+Opencv,从摄像头的实时视频流中逐帧读取图片,保存到本地
114 0
Ubuntu编译安装OpenCV
Ubuntu编译安装OpenCV
17 0
ubuntu 20.04 GPU 装darknet(未编译opencv)
ubuntu 20.04 GPU 装darknet(未编译opencv),本人博客,从csdn搬运
222 0
Ubuntu环境下ROS安装自己OPENCV版本(下)
Ubuntu环境下ROS安装自己OPENCV版本(下)
706 0
Ubuntu环境下ROS安装自己OPENCV版本(上)
Ubuntu环境下ROS安装自己OPENCV版本(上)
275 0
学习Opencv库(一)——基本读写函数的介绍!
OpenCV是一个开源的计算机视觉库,里面封装了很多图像处理方面的优秀算法:例如图片轮廓边缘检测,特征点提取…,可以在Windows,Linux,Mac等平台使用;虽然OpenCv是用 C++ 编写的,并且现在主要接口也是 C++ 语言的但,现在该库也同时提供大量面向 python、matlab以及Java。
254 0
+关注
java牛
善于分享,乐于分享
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载