Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@Gclabbe
Copy link

Gclabbe commented Jan 22, 2020

@texasStronger
Copy link

texasStronger commented Jan 24, 2020

You could try cloning the entire SD to an img and then burn that onto new cards.

Yes, I know how to perform a clone. But that leads to way to many re configurations for other apps. I simply wanted a pack.sh and unpack.sh script. Guess I can start snooping through makefile to see what gets configured and copied.

@willprice
Copy link
Author

willprice commented Jan 24, 2020

@texasStronger,
I haven't tried it, but probably worth copying the entire ~/opencv dir and seeing whether you can just run sudo make install from the build dir. Alternatively I believe you might be able to make some deb files using cpack, I've tried before but it wasn't particularly straight forward.

Here's a few refs that might be of help

Let us know if you have any luck, if you manage it and it's not too complicated then I might update these script to build deb packages.

@swguy3
Copy link

swguy3 commented Jan 28, 2020

Thank you Will! :-) This worked like a charm. I been searching the net for about 2 weeks on how to install OpenCV successfully.

@Derrick-Stark-1
Copy link

Derrick-Stark-1 commented Feb 7, 2020

[ 28%] Linking CXX shared library ../../lib/libopencv_imgcodecs.so
../../3rdparty/lib/liblibwebp.a: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
make[2]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/build.make:385: lib/libopencv_imgcodecs.so.4.1.2] Error 1
make[1]: *** [CMakeFiles/Makefile2:4933: modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2
make: *** [Makefile:163: all] Error 2

I'm new.
Do you know where the problem? Thanks

@effgee
Copy link

effgee commented Feb 11, 2020

@Derrick-Stark-1

"file not recognized: file format not recognized"

suggests a file corruption or media problem

@effgee
Copy link

effgee commented Feb 11, 2020

I was getting Makefile:163 error, but scrolling back found
"error: 'AVFMT_RAWPICTURE' was not declared in this scope"

which led me to https://stackoverflow.com/questions/46884682/error-in-building-opencv-with-ffmpeg/47005401#47005401
which appears to have solved my issue

@sergge1
Copy link

sergge1 commented Feb 21, 2020

thanks to the author. Also, saved for myself from other repos few pre-compiled OpenCV packages for RPi (I am using 3 B+).
Works well for me and installation in a few minutes without compilation needed.
here https://github.com/sergge1/opencv_rpi

@willprice
Copy link
Author

willprice commented Feb 21, 2020

Hi @sergge1, Could you share with us how you made the deb packages for the RPi? Did you use cpack?

Copy link

ghost commented Mar 2, 2020

Thank you @willprice for this great way to install OpenCV on Raspbian. I only changed OpenCV_version to 4.2.0. Everything works fine on a Raspberry Pi 4 2GB. How it is possible to compile C++ files for example in Geany? Thank you for your time!

@b-cass
Copy link

b-cass commented Mar 14, 2020

Hi willprice,

Thank you for your script

I had compiled and made a debian package with TBB NEON VFPV3 with OPENGL QT5 rasbian buster 10

https://github.com/cyysky/Raspbian-Buster-10-Pre-built-OpenCV-4.1.1-TBB-NEON-VFPV3

Installation
wget https://github.com/cyysky/Raspbian-Buster-10-Pre-built-OpenCV-4.1.1-TBB-NEON-VFPV3/blob/master/opencv-4.1.1_4.1.1-1_armhf.deb
sudo dpkg -i opencv-4.1.1_4.1.1-1_armhf.deb
sudo apt-get -f install
sudo dpkg -i opencv-4.1.1_4.1.1-1_armhf.deb

yoeyat@hotmail.com email me or left comment if anything

Chong
Fortune Machine Computer

Thank you so much guys!!! @cyysky @willprice
I have been trying to install OpenCV from around last 1 month today finally i installed it perfectly on my Raspberry Pi 3B+.

@mplefort
Copy link

mplefort commented Mar 30, 2020

Hi @willprice,

Thank you for this tutorial, after two days of searching for an install of opencv on a Raspberrypi 2, OS: Raspian Buster for python 2.7 I was about to give up. This tutorial solved it and got it installed.

Best,

@achandley
Copy link

achandley commented Apr 2, 2020

Thanks this saved me a lot of time.

A suggestion - It would be helpful to add
-D OPENCV_GENERATE_PKGCONFIG=YES
to the build script as the pkg-config file is not created by default
BTW, the package name is now opencv4 (previously opencv)

@willprice
Copy link
Author

willprice commented Apr 3, 2020

@achandley, that's a good shout, added, thanks 👍

@rss28
Copy link

rss28 commented Apr 10, 2020

thank you very much @willprice

@greencane
Copy link

greencane commented Apr 15, 2020

Thank you very much @willprice. Used these scripts to build on a RPi2. Took 5hr 20min to do the build.

@ejuarezv
Copy link

ejuarezv commented Apr 15, 2020

Hello, thanks for your scripts @willprice.
I just finished the compilation succesfully.... but now i have a trouble.

Scanning dependencies of target example_tapi_clahe
[100%] Building CXX object samples/tapi/CMakeFiles/example_tapi_clahe.dir/clahe.cpp.o
[100%] Linking CXX executable ../../bin/example_tapi_clahe
[100%] Built target example_tapi_clahe

  • popd
    ~/opencv/opencv-4.1.2 /home/pi
  • popd
    /home/pi

when i try to change the path

pi@raspberrypi:~ $ cd ~/opencv/opencv-4.1.2/build
bash: cd: /home/pi/opencv/opencv-4.1.2/build: No existe el fichero o el directorio

I searched them in whole system but nothing. it seems like the opencv folders are erased, i followed the scrip without troubles....do you have any idea???
thx.

@texasStronger
Copy link

texasStronger commented Apr 16, 2020

Used this again. Works. An uninstall script would be nice. I have seen various ways to do this. I may try if I can free up a pi.

@d4n13lbc
Copy link

d4n13lbc commented Apr 27, 2020

Thanks, working with OpenCV 4.3.0

@texasStronger
Copy link

texasStronger commented May 3, 2020

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

david0610 commented May 18, 2020

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

reddy16-ind commented Aug 5, 2020

Thank you very much! @ willprice

@2easy4wizzi
Copy link

2easy4wizzi commented Aug 22, 2020

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

neilinpaemail commented Dec 31, 2020

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

jaskiratsingh2000 commented Jun 12, 2021

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