编译OPENCV2.0

    OpenCV is a widely used computer vision library for research and development. I’ve tried using instructions for installing it from the official manual, but it failed miserably. It seems that 2.0 code is incompatible with the latest incarnation of ffmpeg. Couple minutes in googling and I had it working.

UPDATE: The following process describes building the latest stable version from the sources. There is a binary version now in a third-party repository that you can install with apt-get.

Here is what needs to be done if you want to build one from sources:

   1. Get all the prerequisites:
      sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev
      sudo apt-get build-dep libswscale-dev swig
   2. Get OpenCV 2.0, and unpack it somewhere
   3. Fix ffmpeg links. Make sure you are root (sudo -s) and do the following:
      mkdir /usr/include/ffmpeg
      ln -s /usr/include/libavcodec/avcodec.h /usr/include/ffmpeg/avcodec.h
      ln -s /usr/include/libavformat/avformat.h /usr/include/ffmpeg/avformat.h
      ln -s /usr/include/libavformat/avio.h /usr/include/ffmpeg/avio.h
      ln -s /usr/include/libavutil/avutil.h /usr/include/ffmpeg/avutil.h
   4. Configure OpenCV. In the folder you’ve unpacked it to run:
      ./configure --prefix=/usr/local/opencv --enable-apps --enable-shared --enable-swscale --enable-gpl --with-swig
      sudo ln -s /usr/include/libswscale/swscale.h /usr/include/ffmpeg/swscale.h
   5. Make and install it
      make
      sudo make installYou could also test it by running make test
   6. Configure your system to look for opencv libraries
      echo /usr/local/opencv/lib > /etc/ld.so.conf.d/opencv.confReload the libraries and make sure opencv shows up now
      ldconfig -v | grep opencvYou may get warnings on unaccessible lib paths, that is ok. You are looking for a line that says “/usr/local/opencv/lib”
   7. Set-up pkg-config
      echo 'export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv/lib/pkgconfig' >> /etc/bash.bashrc
      echo 'export PYTHONPATH=/usr/local/opencv/lib/python2.6/site-packages/opencv' >> /etc/bash.bashrcRestart your terminal to get the new paths
   8. Test by building samples
      cd ~/Programs/OpenCV-2.0.0/samples/c
      . build_all.sh

You’re done.

Tagged with: computer vision, tech, ubuntu.
44 Comments

    *



      ABC says:
      November 8, 2009 at 2:31 pm

      This still gives me the error:

      make[2]: *** [lib_highgui_la-cvcap_ffmpeg.lo] Error 1

      Reply
          o


            alex says:
            November 10, 2009 at 7:28 pm

            hmm…I’ve expanded the post a bit. try to see if it helps.

            Reply
                +


                  ROBSON DA SILVA SIQUEIRA says:
                  April 3, 2010 at 8:52 am

                  Verify if one of the links in /usr/include/ffmpeg are broken.
                  ls -l /usr/include/ffmpeg

                  Im my case, one of the librarys was not installed, and the link become broke.

                  I resinstall and it’s ok.

                  Reply
    *


      Shern Shiou says:
      November 9, 2009 at 2:18 am

      Hi there, thanks for the guide.
      I found 2 confused statement there.
      1. sudo mkdir /usr/include/ffmpegsudo apt-get install libswscale-dev swig
      Can you put a space between ffmpeg and sudo?

      2. sudo ln -s /usr/include/libavformat/avutil.h /usr/include/ffmpeg/avutil.h
      avutil.h resides in libavutil not libavformat.

      Reply
    *


      Artur says:
      November 14, 2009 at 8:36 am

      Thanks!It works for me.

      Reply
    *


      Jiang says:
      November 14, 2009 at 11:32 am

      Met the sample problem! Can’t cmake following the OpenCV instructions as the declaration of “img_convert” has been removed from “avcodec.h”.

      Thank you, Alex!

      Reply
    *


      zorooo says:
      November 20, 2009 at 4:09 am

      hello,

      i think “mkdir /usr/include/ffmpegsudo” should be “mkdir /usr/include/ffmpeg/”.

      thanks a lot for your manual!

      greets zorooo

      Reply
    *


      Mario says:
      November 27, 2009 at 3:41 am

      I got this error on Step 6.. I am kind of new in Linux so I dont know what should I do…

      root@mario-laptop:~/Dokumente/OpenCV-2.0.0# ldconfig -v | grep opencv
      /sbin/ldconfig.real: Can’t stat /lib/i486-linux-gnu: No such file or directory
      /sbin/ldconfig.real: Can’t stat /lib64: No such file or directory
      /sbin/ldconfig.real: Cannot stat /usr/local/lib/libhighgui.so: No such file or directory
      /sbin/ldconfig.real: Cannot stat /usr/local/lib/libcxcore.so: No such file or directory
      /sbin/ldconfig.real: Cannot stat /usr/local/lib/libcv.so: No such file or directory
      /sbin/ldconfig.real: Cannot stat /usr/local/lib/libcvaux.so: No such file or directory
      /sbin/ldconfig.real: Cannot stat /usr/local/lib/libml.so: No such file or directory
      /usr/local/opencv/lib:

      Reply
          o


            alex says:
            November 27, 2009 at 12:35 pm

            You’re good, the warnings are unrelated to opencv

            Reply
    *


      Silveira Neto says:
      November 27, 2009 at 10:48 am

      I got some errors too on make:
      highgui/cvcap_ffmpeg.cpp: In member function ‘virtual IplImage* CvCapture_FFMPEG::retrieveFrame(int)’:
      highgui/cvcap_ffmpeg.cpp:578: error: ‘img_convert’ was not declared in this scope
      highgui/cvcap_ffmpeg.cpp: In member function ‘virtual bool CvVideoWriter_FFMPEG::writeFrame(const IplImage*)’:
      highgui/cvcap_ffmpeg.cpp:1077: error: ‘img_convert’ was not declared in this scope
      highgui/cvcap_ffmpeg.cpp: In member function ‘virtual bool CvVideoWriter_FFMPEG::open(const char*, int, double, CvSize, bool)’:
      highgui/cvcap_ffmpeg.cpp:1213: warning: ‘AVFormatContext* av_alloc_format_context()’ is deprecated (declared at /usr/include/ffmpeg/avformat.h:873)
      highgui/cvcap_ffmpeg.cpp:1213: warning: ‘AVFormatContext* av_alloc_format_context()’ is deprecated (declared at /usr/include/ffmpeg/avformat.h:873)

      Reply
          o


            alex says:
            November 27, 2009 at 12:39 pm

            Looks like your ffmpeg is still incompatible. Did you link it per step 3?

            Reply
                +


                  Silveira Neto says:
                  November 27, 2009 at 4:25 pm

                  This time I’m archiving compiling it. But on step 6, something like:

                  /sbin/ldconfig.real: Cannot stat /lib/i486-linux-gnu: No such file or directory
                  /sbin/ldconfig.real: Cannot stat /lib64: No such file or directory
                  /usr/local/opencv/lib:

                  (yes, in last line after : was in blank). diyblPic

                  Reply
                      #


                        Silveira Neto says:
                        November 27, 2009 at 4:45 pm

                        It seems to work now. I just had to close and open my terminal. But anyway, what should be the output on the grep at step 6?

                        Reply
                      #


                        Alex says:
                        November 30, 2009 at 3:13 am

                        I am having the same issue however closing and re-opening terminal is not fixing the issue, any ideas? Great guide tho! Much thanks

                        Reply
                            *


                              alex says:
                              December 2, 2009 at 7:45 pm

                              A line starting with /usr/local/opencv/lib is a correct output. Fixed the guide.

          o



            Sebastian says:
            December 15, 2009 at 3:18 pm

            Just in case anyone is still stuck on this error: It showed up for me because I did not have the packages libswscale-dev and swig installed. Appearantly there is no package called “build-dep” in as mentioned in the first command so apt-get install just stopped there and did not install the two successive packages.

            Reply
                +


                  alex says:
                  December 15, 2009 at 7:57 pm

                  Thanks, I’ve fixed the post to reflect the correct command.

                  Reply
    *


      Problems Installing OpenCV in Ubuntu Karmic (9.10) - Alex Sleat says:
      December 1, 2009 at 6:47 am

      [...] I found a great guide on getting OpenCV to install on Ubuntu Karmic (9.10) – http://ivkin.net/2009/11/installing-opencv-2-0-on-ubuntu-9-10-karmic-koala/ [...]

      Reply
    *


      Io-oI says:
      December 2, 2009 at 6:55 pm

      Man, I just installed Ubuntu yesterday so im newbie. I get error when I “sudo make install”.

      The errors are:

      libtool: install: error: cannot install `libcv.la’ to a directory not ending in /usr/local/lib
      make[2]: *** [install-libLTLIBRARIES] Error 1
      make[2]: Leaving directory `/media/42981C43981C37BD/OpenCV-2.0.0/src’
      make[1]: *** [install-am] Error 2
      make[1]: Leaving directory `/media/42981C43981C37BD/OpenCV-2.0.0/src’
      make: *** [install-recursive] Error 1

      Reply
          o


            alex says:
            December 2, 2009 at 7:44 pm

            Your libtool might be too old. Try running
            make DESTDIR= install
            instead. Otherwise I don’t know.

            Reply
                +


                  Io-oI says:
                  December 2, 2009 at 10:34 pm

                  I try but no, same error. Also I verify and I got the latest libtool version.

                  Reply
    *


      Pramod says:
      December 3, 2009 at 11:35 pm

      Evertything went fine until running sudo make install. Though it tells that libraries have been installed, at the end it gives this error message :

      /usr/bin/install -c -m 644 opencv/cv.h opencv/cv.hpp opencv/cvaux.h opencv/cvaux.hpp opencv/cvcompat.h opencv/cvstreams.h opencv/cxmat.hpp opencv/cvtypes.h opencv/cvver.h opencv/cvvidsurv.hpp opencv/cvwp_w_picpath.h opencv/cxcore.h opencv/cxcore.hpp opencv/cxerror.h opencv/cxflann.h opencv/cxmisc.h opencv/cxoperations.hpp opencv/cxtypes.h opencv/highgui.h opencv/highgui.hpp opencv/ml.h ‘/usr/local/OpenCV-2.0.0/include/opencv’
      /usr/bin/install: `opencv/cv.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cv.h’ are the same file
      /usr/bin/install: `opencv/cv.hpp’ and `/usr/local/OpenCV-2.0.0/include/opencv/cv.hpp’ are the same file
      /usr/bin/install: `opencv/cvaux.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvaux.h’ are the same file
      /usr/bin/install: `opencv/cvaux.hpp’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvaux.hpp’ are the same file
      /usr/bin/install: `opencv/cvcompat.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvcompat.h’ are the same file
      /usr/bin/install: `opencv/cvstreams.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvstreams.h’ are the same file
      /usr/bin/install: `opencv/cxmat.hpp’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxmat.hpp’ are the same file
      /usr/bin/install: `opencv/cvtypes.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvtypes.h’ are the same file
      /usr/bin/install: `opencv/cvver.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvver.h’ are the same file
      /usr/bin/install: `opencv/cvvidsurv.hpp’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvvidsurv.hpp’ are the same file
      /usr/bin/install: `opencv/cvwp_w_picpath.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cvwp_w_picpath.h’ are the same file
      /usr/bin/install: `opencv/cxcore.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxcore.h’ are the same file
      /usr/bin/install: `opencv/cxcore.hpp’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxcore.hpp’ are the same file
      /usr/bin/install: `opencv/cxerror.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxerror.h’ are the same file
      /usr/bin/install: `opencv/cxflann.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxflann.h’ are the same file
      /usr/bin/install: `opencv/cxmisc.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxmisc.h’ are the same file
      /usr/bin/install: `opencv/cxoperations.hpp’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxoperations.hpp’ are the same file
      /usr/bin/install: `opencv/cxtypes.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/cxtypes.h’ are the same file
      /usr/bin/install: `opencv/highgui.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/highgui.h’ are the same file
      /usr/bin/install: `opencv/highgui.hpp’ and `/usr/local/OpenCV-2.0.0/include/opencv/highgui.hpp’ are the same file
      /usr/bin/install: `opencv/ml.h’ and `/usr/local/OpenCV-2.0.0/include/opencv/ml.h’ are the same file
      make[2]: *** [install-pkgincludeHEADERS] Error 1
      make[2]: Leaving directory `/usr/local/OpenCV-2.0.0/include’
      make[1]: *** [install-am] Error 2
      make[1]: Leaving directory `/usr/local/OpenCV-2.0.0/include’
      make: *** [install-recursive] Error 1
      root@pramod-laptop:/usr/local/OpenCV-2.0.0# make test
      make: *** No rule to make target `test’. Stop.

      Any hunch as to what the problem could be?

      Regards
      Pramod

      Reply
    *


      Gijs Molenaar says:
      December 11, 2009 at 4:14 am

      I’ve made OpenCV 2.0 packages for Ubuntu karmic and uploaded them to a PPA:

      http://gijs.pythonic.nl/blog/2009/dec/11/ubuntu-karmic-packages-opencv-20/

      Reply
    *


      eton says:

 原文地址 http://ivkin.net/2009/11/installing-opencv-2-0-on-ubuntu-9-10-karmic-koala/