Install OpenCV in Linux with video (ffmpeg) support

Faced with the task of running Computer Vision problems in Linux environment, i started googling for the methods.. Though i found the install steps at many places, but my install always failed, getting stuck while doing a make operation on the source files.

Finally, i managed to install it from source.. I am listing the steps here, with a special thanks to all the people, their blogs and google for helping me succeed in installing OpenCV.

  • Download ffmpeg
  • Extract contents to a folder. do a cd to the folder location in the terminal. Build ffmpeg as follows (as root) :
  • su -c
  • ./configure –enable-shared
  • make all
  • make install
  • The –enable-shared option during configuration ensures compatibility between LGPL licence of ffmpeg contents and BSD licence of OpenCV.
  • ADDITIONAL IMPORTANT STEPS (before installing OpenCV)
  • goto the folder where library files of ffmpeg have got installed in your system. you can do a “locate ffmpeg” to know that. for e.g, in my case, its /usr/include/ffmpeg/
  • Now, type in the follwing (as root):
  • ln -s libavcodec/avcodec.h avcodec.h
  • ln -s libavformat/avformat.h avformat.h
  • ln -s libavformat/avio.h avio.h
  • ln -s libavutil/avutil.h avutil.h
  • ln -s libswscale/swscale.h swscale.h
  • Download latest build of OpenCV 2.1 or OpenCV 2.0
  • install swig in your version of linux.. its available in the repos of all the distros. Swig is the application that interacts between different programming languages like C / C++, Python, etc. Swig is extremely essential for succesfull build of OpenCV.
  • yum install swig sip (Fedora)
  • apt-get install swig python-sip (Debian based distros)
  • Extract and build OpenCV as follows :
  • ./configure –enable-shared

ENSURE the summary of this step is somewhat similar to https://aspratyush.files.wordpress.com/2010/04/opencv-configure.png

If not, please install the required dependencies from your package manager (yum / Synaptic) as mentioned in the link. Any missing dependency will lead to some problem or the other.

  • make
  • make install
  • Now, comes the finishing touch stage.. you need to let the system know where the OpenCV library files are located.
  • export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
  • ldconfig

And you are done.. enjoy the OpenCV build by running examples as mentioned in the technical documentation : OpenCV install Guide

OpenCV summary of configure step

Example of playing a file using ffplay command

playing a video file using ffplay

Advertisements

19 thoughts on “Install OpenCV in Linux with video (ffmpeg) support

  1. The

    ./configure –enable-shared –enable-swig

    command does not work for the OpenCV stage.
    It gives an error thus:
    “Directory ./configure is not known”

    Did you actually try these steps?
    If yes, were you able to display AVI video from OpenCV code?

    Also for some reason, ffplay was not installed after I finished installing ffmpeg without errors.

    Any clues?

  2. i hope you have done a cd (change directory) to the OpenCV folder.. also, configure is a script file. search the OpenCV folder you extracted if the configure file is present there. if not, re-download opencv and redo the steps..
    Also note that the command is

    ./configure –enable-shared –enable-swig

    (double minus sign before the enable keyword) and you have to use it ONLY after installing swig.
    yup.. i have actually installed opencv and ffmpeg together, and had to contend with repeatedly getting “error during make” of opencv for almost a week due to absence of swig.. once i figured that out, install was a cakewalk.. and yes, avi file does display.
    ffplay.. i’ll get back to you.. its an issue about which i have put a query in quite a few forums.

  3. Thanks for the quick response!

    Yes I did cd in.

    I did “yum install swig” and it told me my swig
    was up-to-date.

    Here is my exact input and output.

    [root@myFedora OpenCV-2.1.0]# ./configure –enable-shared –enable-swig
    bash: ./configure: No such file or directory
    [root@myFedora OpenCV-2.1.0]#

    I did use the zipped file in your link above and it has no “configure” script inside the folder.

    How will re-downloading the tar-zip file help?
    Should I download it from a different location than
    you provided above?

    Thanks,
    Ad.

  4. Though it shouldnt be a problem with version 2.1, maybe the configure file hasnt been included in the update.. try downloading version 2.0 from Sourceforge link below :
    OpenCV-2.0.0@sourceforge
    its a 12.8 MB file if i remember correctly.. before doing a configure, check again if the configure file is there in the extracted folder. if problem persists, let me know.. i’ll mail you the configure file.

  5. Thanks for the help. I went much further 🙂

    I installed OpenCV 2.0 you provided and it installed
    without errors.

    Unfortunately when I tried to run my video display example (which I have tested on Mac OS X) these are the errors I get:

    [me@myFedora code]$ gcc -I/usr/local/include/opencv -L/usr/local/lib -lhighgui -lcv -lcxcore -lml -lcvaux just_display_video.c -o tst

    [me@myFedora code]$ ./tst WNDSURF1.AVI

    ./tst: error while loading shared libraries: libhighgui.so.4: cannot open shared object file: No such file or directory

    [me@myFedora code]$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

    [me@myFedora code]$ ./tst WNDSURF1.AVI

    Displaying the the (WNDSURF1.AVI) video!
    Original video size is(width=156,height=120).
    OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file highgui/window.cpp, line 100
    terminate called after throwing an instance of ‘cv::Exception’
    Aborted (core dumped)

    [me@myFedora code]$

    Apparently some low-level function for displaying
    video is not supported.
    How to I check what GTK I have, and how do I
    make sure the configure script calls the latest GTK?

    Thanks for your time 🙂
    Ad.

  6. It is now almost working!!!

    1– I went back and uninstalled the OpenCV-2.0.0.
    2– Installed the suspected missing dependencies.
    3– Re configured OpenCV-2.0.0.
    4– Reinstalled OpenCV-2.0.0.
    5– Recompiled my video displaying example.
    6– Played the example on 2 AVI videos.
    7– My print statements show that they are paying
    OK BUT I cannot see the displayed videos!!!

    I suspect SELinux security is blocking the display somehow. Why would my example be able to accurately count the video frames as it is playing the video but I cannot visually see the displayed video? Any clues anybody?

    Below are the actual commands I ran for the steps above:

    1—————————————————
    # cd OpenCV-2.0.0
    # make uninstall
    # make clean all

    2————————————————–
    #yum install gtk+-devel.i686 gimp-devel.i686 gimp-devel-tools.i686 gimp-help-browser.i686 libjpeg-devel.i686 gstreamer-devel.i686 xine-lib-devel.i686 jasper-devel.i686 OpenEXR-devel.i686 libavc1394-devel.i686 libraw1394-devel.i686 unicap-devel.i686 ucview-devel.i686 libdc1394-devel.i686

    3————————————————–
    # ./configure –enable-shared –enable-swig

    By the way, “–enable-swig” is unrecognized.

    4————————————————–
    # make
    # make install

    5————————————————–
    [me@myFedora code]$ gcc -I/usr/local/include/opencv -L/usr/local/lib -lhighgui -lcv -lcxcore -lml -lcvaux just_display_video.c -o tst

    6————————————————–
    [me@myFedora code]$ ./tst WNDSURF1.AVI
    ./tst: error while loading shared libraries: libhighgui.so.4: cannot open shared object file: No such file or directory

    7a—-Temporary Fix shared library unfound—-

    [me@myFedora code]$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

    7b—————-Play first video—————–
    [me@myFedora code]$ ./tst WNDSURF1.AVI
    Displaying the the (WNDSURF1.AVI) video!
    Original video size is(width=156,height=120).
    Finshed displaying video(WNDSURF1.AVI), with 81 frames.

    7c—————–Play 2nd video—————–
    [me@myFedora code]$ ./tst SAMPLE.AVI
    Displaying the the (SAMPLE.AVI) video!
    Original video size is(width=320,height=240).
    Finshed displaying video(SAMPLE.AVI), with 105 frames.

    BUT why can’t I actually see the playing video?
    I am running Fedora 12 running on VirtualBox on a MacBook Pro Host on Leopard (Mac OS X 10.5). Any hints welcome.

  7. 1) i have updated the post a bit.. a few header files of ffmpeg need to have a symbolic link created.
    You may have to rebuild OpenCV after creating the above links.
    2) Also, gtk2.0 and swig are dependencies of OpenCV. Once present, doing an explicit enable is not
    needed as such.
    3) As far as SELinux is concerned, reconfigure it to be in Permissive mode, though that shouldnt
    be a problem as such.
    4) avi files are working perfectly fine for me.. let me know if problem still persists.

  8. I found out how to install ffplay as well as ffmpeg.
    You just have to do:

    make all
    make install all

    instead of:

    make
    make install

    ————–
    By the way though my OpenCV still works OK and I can capture video from camera and save it to an AVI file, I still cannot see the video playing!

    I tested it on a stand alone Fedora 12 laptop and still no video displayed during the play.
    I can however play the saved video after that via ffplay.

    Any clues?

    Thanks,
    Ad.

  9. what error are you getting.. please elaborate, or provide a sample output of the make / make install from terminal..

  10. Am not really sure why the build of ffmpeg fails to install ffplay, eventhough during install steps we replace:

      make – make all
      make install – make install all

    I did a “yum install ffmpeg” and that seems to have resolved ffplay issue.. refer the pic uploaded to the blog post.

    As for needing ffmpeg for OpenCV, you dont need ffplay explicitly.. OpenCV will use ffmpeg to decode and play
    the file provided, provided the dependencies are satisfied, which you get to know as a summary upon doing a
    ./configure for opencv installation..refer the updated blog post for dependencies needed..

  11. Hi i followed your steps & after ./configure –enable-shared I got :Use ffmpeg: no

    Please help me to enable ffmpeg.I have installed ffmpeg by your steps & a ‘whereis ffmpeg’ says ‘/usr/bin/ffmpeg’

    Below is complete stacktrace
    Video I/O ———————
    Use QuickTime / Mac OS X: no
    Use xine: no
    Use gstreamer: no
    Use ffmpeg: no
    Use dc1394 & raw1394: no
    Use dc1394_v2 & raw1394: no
    Use v4l: yes
    Use v4l2: yes
    Use unicap: no

  12. Linking error may occur due to file permissions problem. follow the below steps as “su”
    Do a cd to the ffmpeg tar unzipped folder, then

      make uninstall
      make clean all
      Then reinstall ffmpeg
      Link the files (libavutil, etc) as mentioned..
      if you cannot find the files in some folder, do a whereis and then cd to that folder and link the files.
      Only when these files get linked,does configure of opencv stage detects ffmpeg properly..
      then install opencv.

    This should work..Let me know if u still face a problem..

  13. I just installed fedora 16 x86_64 on an Asus system. I followed ur steps for installing open cv 2.0.0. It worked smoothly till the configuration of open cv then i got this error message

    root@keat OpenCV-2.0.0# make
    make all-recursive
    make all-recursive
    make[1]: Entering directory `/home/Keat/OpenCV-2.0.0′
    Making all in 3rdparty
    make[2]: Entering directory `/home/Keat/OpenCV-2.0.0/3rdparty’
    make[2]: Nothing to be done for `all’.
    make[2]: Leaving directory `/home/Keat/OpenCV-2.0.0/3rdparty’
    Making all in src
    make[2]: Entering directory `/home/Keat/OpenCV-2.0.0/src’
    /bin/sh ../libtool –tag=CXX –mode=compile g++ -DHAVE_CONFIG_H -I. -I.. -DNDEBUG -I../include/opencv -O3 -g -march=x86-64 -ffast-math -fomit-frame-pointer -MT cxprecomp.lo -MD -MP -MF .deps/cxprecomp.Tpo -c -o cxprecomp.lo `test -f ‘cxcore/cxprecomp.cpp’ || echo ‘./’`cxcore/cxprecomp.cpp
    libtool: compile: g++ -DHAVE_CONFIG_H -I. -I.. -DNDEBUG -I../include/opencv -O3 -g -march=x86-64 -ffast-math -fomit-frame-pointer -MT cxprecomp.lo -MD -MP -MF .deps/cxprecomp.Tpo -c cxcore/cxprecomp.cpp -fPIC -DPIC -o .libs/cxprecomp.o
    In file included from ../include/opencv/cxcore.h:2123:0,
    from cxcore/_cxcore.h:60,
    from cxcore/cxprecomp.cpp:43:
    ../include/opencv/cxcore.hpp:169:13: error: ‘ptrdiff_t’ does not name a type
    In file included from ../include/opencv/cxcore.hpp:2243:0,
    from ../include/opencv/cxcore.h:2123,
    from cxcore/_cxcore.h:60,
    from cxcore/cxprecomp.cpp:43:
    ../include/opencv/cxoperations.hpp:1916:15: error: ‘ptrdiff_t’ does not name a type
    In file included from ../include/opencv/cxcore.hpp:2243:0,
    from ../include/opencv/cxcore.h:2123,
    from cxcore/_cxcore.h:60,
    from cxcore/cxprecomp.cpp:43:
    ../include/opencv/cxoperations.hpp:2465:31: error: ‘ptrdiff_t’ does not name a type
    In file included from ../include/opencv/cxcore.hpp:2244:0,
    from ../include/opencv/cxcore.h:2123,
    from cxcore/_cxcore.h:60,
    from cxcore/cxprecomp.cpp:43:
    ../include/opencv/cxmat.hpp: In member function ‘void cv::Mat::locateROI(cv::Size&, cv::Point&) const’:
    ../include/opencv/cxmat.hpp:356:5: error: ‘ptrdiff_t’ was not declared in this scope
    ../include/opencv/cxmat.hpp:356:5: note: suggested alternatives:
    /usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/x86_64-redhat-linux/bits/c++config.h:1738:20: note: ‘std::ptrdiff_t’
    /usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/x86_64-redhat-linux/bits/c++config.h:1738:20: note: ‘std::ptrdiff_t’
    ../include/opencv/cxmat.hpp:356:15: error: expected ‘;’ before ‘delta1’
    ../include/opencv/cxmat.hpp:358:9: error: ‘delta1’ was not declared in this scope
    ../include/opencv/cxmat.hpp:367:31: error: ‘delta2’ was not declared in this scope
    make[2]: *** [cxprecomp.lo] Error 1
    make[2]: Leaving directory `/home/Keat/OpenCV-2.0.0/src’
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory `/home/Keat/OpenCV-2.0.0′
    make: *** [all] Error 2

    any sort of help will be highly appreciated.

  14. To be frank, am not really sure why this error is coming up.. i’ll try doing a custom install myself and try figuring out. In my experience, doing a OpenCV 2.3 install is much easier, smoother and bug-free than v2.0, unless of course it ends up breaking other parts of your system. With rapid changes to modules like ffmpeg / libv4l2 , etc.. some parts of your OpenCV codes may not work as expected in case you install previous versions..

    in the meantime, in case you want to try out OpenCV 2.3, you can follow the link :
    https://aspratyush.wordpress.com/2011/07/11/install-opencv-2-3-with-video-support-in-fedora-15/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s