Skip to content

Instantly share code, notes, and snippets.

@jbienkowski311
Last active February 4, 2020 00:59
Show Gist options
  • Save jbienkowski311/ce12c83672fc7c519ed8586832145eb0 to your computer and use it in GitHub Desktop.
Save jbienkowski311/ce12c83672fc7c519ed8586832145eb0 to your computer and use it in GitHub Desktop.
OpenCV 3.1 installation script for Raspberry Pi (Raspbian Jessie)
#!/bin/bash
# full update
sudo apt-get update
sudo apt-get -y upgrade
sudo rpi-update
# reboot required
read -r -p "You may need to reboot your RPi. Would you like to do that? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
sudo reboot
fi
# install build git cmake and some others
sudo apt-get install -y build-essential checkinstall git cmake wget unzip
# ffmpeg dependencies
sudo apt-get install -y 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 libsdl1.2-dev libvpx-dev
# install image codecs
sudo apt-get install -y libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
# gstreamer
sudo apt-get install -y 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
# install video codecs
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev v4l-utils
sudo apt-get install -y libxvidcore-dev libx264-dev x264
# GTK to support OpenCV GUI
sudo apt-get install -y libgtk2.0-dev libqt4-dev libqt4-opengl-dev
# matrix operations optimization
sudo apt-get install -y libatlas-base-dev gfortran
# install python 2 and 3 and some python libs with pip
sudo apt-get install -y python2.7-dev python3-dev python-pip
sudo apt-get install -y python-tk python-numpy python3-tk python3-numpy python-qt4
mkdir ~/source
cd ~/source
# ffmpeg from git repo
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg/
# build ffmpeg
./configure --enable-gpl --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-libvpx --enable-pic
make
sudo make install
sudo ldconfig -v
# opencv compilation
sudo apt-get autoremove libopencv-dev python-opencv
# download opencv
cd ~/source
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip opencv.zip
cd opencv-3.1.0/
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D INSTALL_C_EXAMPLES=OFF ..
make -j $(nproc)
sudo make install
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig -v
@mcajkovs
Copy link

is it possible to build opencv against ffmpeg from raspbian repositories? thx

@jbienkowski311
Copy link
Author

is it possible to build opencv against ffmpeg from raspbian repositories? thx

You can, however the precompiled version of ffmpeg is missing a lot of dependencies that are necessary (or you may need them somewhere in future). Therefore I would suggest compiling everything from source. Last but not least, the precompiled versions distributed with Debian are a bit outdated.

@GForceSquared
Copy link

Hi, first I want to say thank you for creating such an easy guide for compiling OpenCV with FFmpeg on the Raspberry Pi. I began following your instructions but I had an error when I got to the step
make -j $(nproc)
The error code that I received was this:
/home/pi/source/opencv-3.1.0/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function ‘bool OutputMediaStream_FFMPEG::open(const char*, int, int, double)’: /home/pi/source/opencv-3.1.0/modules/videoio/src/cap_ffmpeg_impl.hpp:2207:41: warning: ignoring return value of ‘int avformat_write_header(AVFormatContext*, AVDictionary**)’, declared with attribute warn_unused_result [-Wunused-result] avformat_write_header(oc_, NULL); ^ modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:206: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o' failed make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1 CMakeFiles/Makefile2:2557: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2 Makefile:160: recipe for target 'all' failed make: *** [all] Error 2

I have a Raspberry Pi 3B+ running the latest version of Raspbian and everything is up to date. I am very confused about how to solve this issue and I would appreciate your help!
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment