Tuesday, December 11, 2012

Install OpenCV in Ubuntu 12.04

Copy the following script to gedit and save as opencv.sh . Open terminal.
chmox +x opencv.sh
./opencv.sh

This will complete opencv installation

arch=$(uname -m)
if [ "$arch" == "i686" -o "$arch" == "i386" -o "$arch" == "i486" -o "$arch" == "i586" ]; then
flag=1
else
flag=0
fi
echo "Installing OpenCV 2.4.3"
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get install libopencv-dev
sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install libtiff4-dev libjpeg-dev libjasper-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
sudo apt-get install python-dev python-numpy
sudo apt-get install libtbb-dev
sudo apt-get install libqt4-dev libgtk2.0-dev
echo "Downloading x264"
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/
echo "Installing x264"
if [ $flag -eq 1 ]; then
./configure --enable-static
else
./configure --enable-shared --enable-pic
fi
make
sudo make install
cd ..
echo "Downloading ffmpeg"
wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.bz2
echo "Installing ffmpeg"
tar -xvf ffmpeg-0.11.1.tar.bz2
cd ffmpeg-0.11.1/
if [ $flag -eq 1 ]; then
./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
else
./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
fi
make
sudo make install
cd ..
echo "Downloading v4l"
wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.tar.bz2
echo "Installing v4l"
tar -xvf v4l-utils-0.8.8.tar.bz2
cd v4l-utils-0.8.8/
make
sudo make install
cd ..
echo "Downloading OpenCV 2.4.3"
wget -O OpenCV-2.4.3.tar.bz2 http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download
echo "Installing OpenCV 2.4.3"
tar -xvf OpenCV-2.4.3.tar.bz2
cd OpenCV-2.4.3
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE ..
make
sudo make install

sudo su
sudo echo “/usr/local/lib” >> /etc/ld.so.conf
sudo ldconfig
echo "OpenCV 2.4.3 ready to be used"


Special Thanks for the original writer. This is a little modified version of it

14 comments:

  1. Thanks for eh script... currently executing

    ReplyDelete
  2. how to run the chmox +x opencv
    ./opencv, it's show error ???

    ReplyDelete
    Replies
    1. should this be?:
      chmod +x opencv.sh
      ./opencv.sh

      Delete
    2. Sorry! Thanks for the correction.
      I think only in some ubuntu versions it works without .sh

      Delete
  3. You are an angel!!! :) Thanks! :D

    ReplyDelete
  4. While running this I got an error when trying to make opencv:

    [ 10%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o
    In file included from /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg.cpp:45:0:
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:849:10: error: use of enum ‘CodecID’ without previous declaration
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:849:52: error: invalid type in declaration before ‘;’ token
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:901:46: error: ‘CodecID’ has not been declared
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp: In function ‘AVStream* icv_add_video_stream_FFMPEG(AVFormatContext*, int, int, int, int, double, int)’:
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:934:23: error: invalid conversion from ‘int’ to ‘AVCodecID’ [-fpermissive]
    In file included from /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg.cpp:45:0:
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp: In member function ‘bool CvVideoWriter_FFMPEG::open(const char*, int, double, int, int, bool)’:
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1280:5: error: ‘CodecID’ was not declared in this scope
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1280:13: error: expected ‘;’ before ‘codec_id’
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1325:10: error: ‘codec_id’ was not declared in this scope
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1345:13: error: ‘codec_id’ was not declared in this scope
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1374:48: error: ‘codec_id’ was not declared in this scope
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp: At global scope:
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1590:58: error: ‘CodecID’ has not been declared
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1637:73: error: ‘CodecID’ has not been declared
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp: In static member function ‘static AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext*, int, int, int, int, double, AVPixelFormat)’:
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1653:19: error: invalid conversion from ‘int’ to ‘AVCodecID’ [-fpermissive]
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp: In member function ‘bool OutputMediaStream_FFMPEG::open(const char*, int, int, double)’:
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1762:5: error: ‘CodecID’ was not declared in this scope
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1762:13: error: expected ‘;’ before ‘codec_id’
    /home/edthomason/Desktop/OpenCV-2.4.3/modules/highgui/src/cap_ffmpeg_impl.hpp:1784:37: error: ‘codec_id’ was not declared in this scope
    At global scope:
    cc1plus: warning: unrecognised command line option "-Wno-unnamed-type-template-args" [enabled by default]
    cc1plus: warning: unrecognised command line option "-Wno-delete-non-virtual-dtor" [enabled by default]
    cc1plus: warning: unrecognised command line option "-Wno-narrowing" [enabled by default]
    make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make: *** [all] Error 2


    Any ideas?

    ReplyDelete
    Replies
    1. Try to remove and reinstall ffmpeg. Find more details in ffmpeg site.

      Delete
  5. [unexpected end of file] error :(

    ReplyDelete
    Replies
    1. make sure you have copied the entire script, ending with the line that says 'echo "OpenCV 2.4.3 ready to be used"'

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hi

    I'm at the point where we have to save the file as opencv.sh, where do we save it? when i enter chmod +x opencv.sh
    ./opencv.sh i get an error

    ReplyDelete
  8. In your home folder :D Atleast that worked for me

    ReplyDelete