Skip to content

Instantly share code, notes, and snippets.

@willprice
Last active February 8, 2023 21:27
Show Gist options
  • Save willprice/abe456f5f74aa95d7e0bb81d5a710b60 to your computer and use it in GitHub Desktop.
Save willprice/abe456f5f74aa95d7e0bb81d5a710b60 to your computer and use it in GitHub Desktop.
Install OpenCV 4.1.2 for Raspberry Pi 3 or 4 (Raspbian Buster)

Install OpenCV 4.1.2 on Raspbian Buster

$ chmod +x *.sh
$ ./download-opencv.sh
$ ./install-deps.sh
$ ./build-opencv.sh
$ cd ~/opencv/opencv-4.1.2/build
$ 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.2
pushd ~/opencv/opencv-$OPENCV_VERSION
mkdir -p build
pushd build
MEM="$(free -m | awk /Mem:/'{print $2}')" # Total memory in MB
# RPI 4 with 4GB RAM is actually 3906MB RAM after factoring in GPU RAM split.
# We're probably good to go with `-j $(nproc)` with 3GB or more RAM.
if [[ $MEM -ge 3000 ]]; 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
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 WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_EXECUTABLE=$(which python2) \
..
make -j "$NUM_JOBS"
popd; popd
#!/usr/bin/env bash
set -ex
OPENCV_VERSION=4.1.2
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
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()
@d4n13lbc
Copy link

Thanks, working with OpenCV 4.3.0

@texasStronger
Copy link

Running a new OS and OpenCV 4.3.0 install and build on a Raspberry PI 4B 4GB. I had an error in install-deps.sh which made me have to run:
sudo apt update && sudo apt full-upgrade -y
I thought my system was already updated. The problem was with some vlc-bin dependencies. The install-deps.sh then ran successfully. Then build started complaining. I realized that I had to remove CMakeCache.txt. Build is running now.

@david0610
Copy link

root@raspberrypi:/home/pi/Desktop/opencv# ./build-opencv.sh
bash: ./build-opencv.sh: No such file or directory
i don't know why i write ./build-opencv.sh . It can't find it.
please help me

@cyysky
Copy link

cyysky commented May 21, 2020

this is my opencv 4.3.0 debian package for direct installation, you can get into package build instruction there is in this git also.
thank your script to enable this git repo to happen.
https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python

Installation Step
wget https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python/raw/master/opencv_4.3.0-1_armhf.deb

sudo dpkg -i opencv_4.3.0-1_armhf.deb # This will install fail for dependency
sudo apt-get -f install # Auto install dependency package
sudo dpkg -i opencv_4.3.0-1_armhf.deb # Now start install

sudo apt-get install tesseract-ocr # Optional : tesseract-ocr

@reddy16-ind
Copy link

Thank you very much! @ willprice

@2easy4wizzi
Copy link

great help :) 👍

@thelwyn
Copy link

thelwyn commented Sep 16, 2020

this is my opencv 4.3.0 debian package for direct installation, you can get into package build instruction there is in this git also.
thank your script to enable this git repo to happen.
https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python

Installation Step
wget https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python/raw/master/opencv_4.3.0-1_armhf.deb

sudo dpkg -i opencv_4.3.0-1_armhf.deb # This will install fail for dependency
sudo apt-get -f install # Auto install dependency package
sudo dpkg -i opencv_4.3.0-1_armhf.deb # Now start install

sudo apt-get install tesseract-ocr # Optional : tesseract-ocr

@cyysky Smooth! Thank you very much! And thank you @willprice for setting the path!

@thelwyn
Copy link

thelwyn commented Sep 20, 2020

this is my opencv 4.3.0 debian package for direct installation, you can get into package build instruction there is in this git also.
thank your script to enable this git repo to happen.
https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python
Installation Step
wget https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python/raw/master/opencv_4.3.0-1_armhf.deb
sudo dpkg -i opencv_4.3.0-1_armhf.deb # This will install fail for dependency
sudo apt-get -f install # Auto install dependency package
sudo dpkg -i opencv_4.3.0-1_armhf.deb # Now start install
sudo apt-get install tesseract-ocr # Optional : tesseract-ocr

@cyysky Smooth! Thank you very much! And thank you @willprice for setting the path!

I spoke a little too fast. I could install but then got an "Illegal instruction" error when trying to use it, because it was not appropriately compiled for Raspberry Pi Zero (my device). Following instructions on pyimagesearch, I could finally build it successfully. for thos who want to do it for RPi Zero, needs to remove these 2 flags:
-D ENABLE_NEON=ON
-D ENABLE_VFPV3=ON
and be patient... it took 20h to build openCV on the RPi zero. Don't forget to increase the swap to 2G and just 'make' instead of 'make -j4'

@cyysky I wish I could make a package like you did, it's very convenient for install for less experiences people!

@cyysky
Copy link

cyysky commented Sep 20, 2020

@cyysky
Copy link

cyysky commented Oct 19, 2020

I just compiled latest opencv 4.5.0 debian package for raspberry pi 3/4b buster python 2 and 3, can check out at https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python install follow the readme instruction

@neilinpaemail
Copy link

Thank you willprice for the steps. Everything worked on Raspberry Pi3B+ and increasing swap.
Question, I am now trying to use gstreamer as input source into opencv( cv2). I have a remote Pi 0w capturing video/frames and can see on my local Pi3B+ outside of cv2, but wondering how to provide that input into cv2 as the source file/image to work on. I have Googled and see a few comments about cv2 having to support gstreamer. Does your version/steps support this and could you possibly provide an example? I can provide more info if you need it as well. Thanks again.

@jaskiratsingh2000
Copy link

Do I need to clone anything before running the above commands or run those commands directly?

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