Skip to content

Instantly share code, notes, and snippets.

@cyysky
Forked from willprice/README.md
Last active September 30, 2019 16:29
Show Gist options
  • Save cyysky/70fabe484d9bbf05f5871dce2c67aa6f to your computer and use it in GitHub Desktop.
Save cyysky/70fabe484d9bbf05f5871dce2c67aa6f to your computer and use it in GitHub Desktop.
Install OpenCV 4.1.1 for Raspberry Pi 3 or 4 (Raspbian Buster) with Optional OpenGL QT5 support

Install OpenCV 4.1.1 on Raspbian Buster

$ chmod +x *.sh
$ ./download-opencv.sh
$ ./install-deps.sh
$ ./build-opencv.sh
$ cd ~/opencv/opencv-4.1.1/build
## For success python compile need to add -latomic manually to link.txt      
#https://github.com/opencv/opencv/issues/15192
# add -latomic to modules/python3/CMakeFiles/opencv_python3.dir/link.txt
$ make
$ sudo make install

Check you can run test.py using both python 2 and 3 to verify that OpenCV python bindings were successfully installed

$ wget "https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png" -O lenna.jpg
$ python2 test.py lenna.jpg
$ python3 test.py lenna.jpg

WARNING: Users of boards with 1GB of memory

Compiling is very memory intensive, you will likely need to increase your swap size. Assuming you have a reasonably large SD card (>16GB to be safe), follow the procedure below to increase your swap size from the default 100MB to 2GB

$ sudo dphys-swapfile swapoff
$ sudo sed -i 's:CONF_SWAPSIZE=.*:CONF_SWAPSIZE=2048:g' /etc/dphys-swapfile
$ sudo reboot
#!/usr/bin/env bash
set -ex
OPENCV_VERSION=4.1.1
pushd ~/opencv/opencv-$OPENCV_VERSION
mkdir -p build
pushd build
RPI_VERSION=$(awk '{print $3}' < /proc/device-tree/model)
if [[ $RPI_VERSION -ge 4 ]]; then
NUM_JOBS=$(nproc)
else
NUM_JOBS=1 # Earlier versions of the Pi don't have sufficient RAM to support compiling with multiple jobs.
fi
# -D ENABLE_PRECOMPILED_HEADERS=OFF
# is a fix for https://github.com/opencv/opencv/issues/14868
# -D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic
# is a fix for https://github.com/opencv/opencv/issues/15192
# -D WITH_QT=ON delete this line if wanna compile without OpenGL and QT5 Support
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-$OPENCV_VERSION/modules \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_DOCS=ON \
-D BUILD_EXAMPLES=ON \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_OPENMP=ON \
-D ENABLE_NEON=ON \
-D ENABLE_LTO=ON \
-D WITH_OPENCL=ON \
-D WITH_GSTREAMER=ON \
-D CPU_BASELINE=NEON \
-D ENABLE_VFPV3=ON \
-D WITH_OPENGL=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_QT=ON \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D CMAKE_SHARED_LINKER_FLAGS=-latomic \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_EXECUTABLE=$(which python2) \
..
## For success python compile need to add -latomic manually to link.txt
#https://github.com/opencv/opencv/issues/15192
# add -latomic to modules/python3/CMakeFiles/opencv_python3.dir/link.txt
#make -j "$NUM_JOBS"
popd; popd
#!/usr/bin/env bash
set -ex
OPENCV_VERSION=4.1.1
cd ~
mkdir -p opencv && pushd opencv
wget -O "opencv-${OPENCV_VERSION}.tar.gz" "https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.tar.gz"
wget -O "opencv_contrib-${OPENCV_VERSION}.tar.gz" "https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.tar.gz"
tar -xvf "opencv-${OPENCV_VERSION}.tar.gz"
tar -xvf "opencv_contrib-${OPENCV_VERSION}.tar.gz"
popd
#!/usr/bin/env bash
set -ex
sudo apt-get purge -y libreoffice*
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
# For some reason I couldn't install libgtk2.0-dev or libgtk-3-dev without running the
# following line
# See https://www.raspberrypi.org/forums/viewtopic.php?p=1254646#p1254665 for issue and resolution
sudo apt-get install -y devscripts debhelper cmake libldap2-dev libgtkmm-3.0-dev libarchive-dev \
libcurl4-openssl-dev intltool
sudo apt-get install -y build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libgtk2.0-dev libgtk-3-dev \
libatlas-base-dev libblas-dev libeigen{2,3}-dev liblapack-dev \
gfortran \
python2.7-dev python3-dev python-pip python3-pip python python3
# GStreamer
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y libavresample*
# Install QT5 Optional for OpenGL support
sudo apt-get install -y qt5-default
sudo pip2 install -U pip
sudo pip3 install -U pip
sudo pip2 install numpy
sudo pip3 install numpy
import numpy as np
import cv2
import sys
if len(sys.argv) < 2:
print("USAGE: {} img-file".format(sys.argv[0]))
sys.exit(1)
# Load an color image in grayscale
img = cv2.imread(sys.argv[1],0)
cv2.imshow('image',img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment