Anyone who has dealt with image processing in relation to the Raspberry Pi will sooner or later come across the OpenCV library. It provides many very useful features such as face recognition, the creation of depth maps (stereo vision, optical flow), text recognition or even for machine learning. In addition, OpenCV (Open Source Computer Vision) can be integrated into both its C ++ files and its Python scripts.
Especially in terms of feature recognition in images taken by the Raspberry Pi, OpenCV is very helpful.
This advanced tutorial shows how to install OpenCV on the Raspberry Pi and how to integrate it into Python.
For the time being, I would like to recommend everyone a Raspberry Pi Model B to use, because this is a lot more powerful than its predecessor. Even on a Raspberry Pi B + compiling takes about 6 times as long, with a Pi 2 in total “only” takes about an hour.
First of all, we update the package lists:
sudo apt-get update && sudo apt-get upgrade && sudo rpi-update
A reboot is necessary if it has been updated.
sudo reboot
Then you can install all the important tools and libraries needed for OpenCV (installation takes a few minutes).
sudo apt-get install build-essential git cmake pkg-config libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libgtk2.0-dev libatlas-base-dev gfortran
If everything worked, we could clone OpenCV from git. This step also takes a few minutes.
git clone https://github.com/Itseez/opencv.git && cd opencv &&git checkout 3.0.0
Whether version 3.0 or 2.4 of OpenCV is taken is up to you. Depending on the application, one of the versions may be better suited.
Afterwards, OpenCV can be compiled. You can either use Python 2.7 or Python 3+. There are some differences between the versions, especially as some libraries are not (yet) executable with Python 3+. However, this mainly affects smaller libraries, as common libraries (NumPy, SciPy, etc.) usually provide the respective files for both versions.
In this tutorial, I use Python 2.7. If you already have Python installed and want to know which version is installed, you can simply enter python
into the console and get the exact version at the beginning (the command for Python 3+ is python3
). If you do not have a Python installed, you can install it by following the steps below:
sudo apt-get install python2.7-dev
We also need the package management tool pip, which installs NumPy right away:
cd ~ && wget https://bootstrap.pypa.io/get-pip.py && sudo python get-pip.py
Now we can simply install via pip NumPy. NumPy is a library that makes it very easy to perform array operations in Python.
pip install numpy
But now to compile OpenCV. For this purpose, a build folder must be created in which the compiled files land:
cd ~/opencv && mkdir build && cd build cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..
Now you can finally compile. This step takes (depending on Raspberry Pi model) quite a long time (on my Pi 2 about an hour). To use all four cores to compile on the Raspberry Pi 2, type in the following:
make -j4
If the compilation has worked without problems, we can install OpenCV:
sudo make install && sudo ldconfig
Done!
To check if everything worked, you can open the Python console and import the library:
1 2 |
import cv2 cv2.__version__ |
Now you can use all OpenCV functions in your projects. A look at the OpenCV documentation is certainly helpful.
39 Comments
thnks for all this information that help me so much but can you tell me the source of this documentation
I got stuck at the compilation phase.
when I write cmake -D CMAKE_BUILD_TYPE=RELEASE i get the following error:
CMake Error: The source directory “/home/pi/opencv/build/CMAKE_BUILD_TYPE=RELEASE” does not exist.
Specify –help for usage, or press the help button on the CMake GUI.
please help.
Mark, put a on the end of each of the cmake lines (except the last one).
tnx
put a what?
cmake -D CMAKE_BUILD_TYPE=RELEASE
CMake Error: The source directory “/home/pi/opencv/build/CMAKE_BUILD_TYPE=RELEASE” does not exist.
Specify –help for usage, or press the help button on the CMake GUI.
please help
Put a backslash on the end of multi-line commands, except the last line.
Sorry, it got edited out and my attempt to subscribe failed so I was blissfully unaware. (It was only three years ago…)
Mark
did putting the fix your problem ? I have the same problem . I tried with at the end and i dont get a fault but goes to the next line and just get this >
please help.
i am also facing same problem. Any suggestion please
When doing this I get a few errors from some modules
cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local
-D INSTALL_PYTHON_EXAMPLES=ON
-D INSTALL_C_EXAMPLES=ON
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules
-D BUILD_EXAMPLES=ON ..
also after when running make -j4, I get “make *** no targets specified and no makefile found. stop.”
What I’m doing wrong?
install cmake file using the command “sudo apt-get install build-essential cmake unzip pkg-config” to install the make file
It still give “make: *** No targets specified and no makefile found. Stop.”
When running the install line for the important tools and libraries for OpenCV, I get an error about that libtiff4-devis not available, but has been replaced by libtiff5-dev. So, I made that substitution and then got the errors that libgtk2.0-dev has unmet dependencies and lists 3 libraries, then states that libtiff5-dev also has an unmet dependency on libjpeg-dev. All followed by the line: “E: Unable to correct problems, you have held broken packages.” Will this cause problems with the later steps?
hi dianna, i get the same error, how did you fix it?
Where can we get help with the OpenCV compilation for Raspbian? I cannot get these instructions to compile with my Raspberry Pi 3B.
Nevermind. I’m migrating to Ubuntu-Mate and hope that works.
Did Ubuntu-Mate work?
I keep getting this error anytime I try to compile OpenCV … someone please help ?
Generating precomp.hpp.gch/opencv_imgcodecs_RELEASE.gch
In file included from /home/pi/opencv/modules/hal/include/opencv2/hal/defs.h:329:0,
from /home/pi/opencv/modules/core/include/opencv2/core/cvdef.h:59,
from /home/pi/opencv/modules/core/include/opencv2/core.hpp:52,
from /home/pi/opencv/modules/imgcodecs/include/opencv2/imgcodecs.hpp:46,
from /home/pi/opencv/build/modules/imgcodecs/precomp.hpp:45:
/usr/include/c++/6/cmath:45:23: fatal error: math.h: No such file or directory
#include_next
^
compilation terminated.
modules/imgcodecs/CMakeFiles/pch_Generate_opencv_imgcodecs.dir/build.make:62: recipe for target ‘modules/imgcodecs/precomp.hpp.gch/opencv_imgcodecs_RELEASE.gch’ failed
make[2]: *** [modules/imgcodecs/precomp.hpp.gch/opencv_imgcodecs_RELEASE.gch] Error 1
CMakeFiles/Makefile2:5803: recipe for target ‘modules/imgcodecs/CMakeFiles/pch_Generate_opencv_imgcodecs.dir/all’ failed
make[1]: *** [modules/imgcodecs/CMakeFiles/pch_Generate_opencv_imgcodecs.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs….
[ 20%] Linking CXX static library ../../lib/libopencv_stitching_pch_dephelp.a
[ 20%] Linking CXX static library ../../lib/libopencv_test_stitching_pch_dephelp.a
[ 20%] Linking CXX static library ../../lib/libopencv_perf_stitching_pch_dephelp.a
[ 20%] Built target opencv_stitching_pch_dephelp
[ 20%] Built target opencv_test_stitching_pch_dephelp
[ 20%] Built target opencv_perf_stitching_pch_dephelp
Makefile:149: recipe for target ‘all’ failed
make: *** [all] Error 2
I see all tutorials explaining how to install OpenCV by compiling the sources without ever mentioning there is the easy and natural way of sudo apt-get install python-opencv
Not against compiling unless it takes >20 hours on a PiZero, and all top results in google “install opencv on raspberry pi” do not mention the easy way.
Beginners get in a tedious compiling process without ever knowing there is an already compiled package.
parabens cezar, por mais pessoas como você!
abraço
The best advice, thank you Cezar! https://pypi.org/project/opencv-python/ worked well for me on RPi 4 Model B, requiring just an upgrade of numpy to the latest version
E: Package ‘libtiff4-dev’ has no installation candidate
how to solve this Error while installing : “sudo apt-get install build-essential git cmake pkg-config libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libgtk2.0-dev libatlas-base-dev gfortran” this command on Rpi3B.
I cannot get the compile to work. It failed at the make as above.
sudo apt-get install python-opencv does not work either.
Python Test Code
import cv2
cv2 –version
Error message Error: No module named ‘cv2’
Is there anyone who can help please
https://gist.github.com/willprice/abe456f5f74aa95d7e0bb81d5a710b60#file-build-opencv-sh
This script may be useful
I keep getting this error anytime I try to compile OpenCV … someone please help ?
Scanning dependencies of target pch_Generate_opencv_stitching
[ 82%] Generating precomp.hpp
[ 82%] Generating precomp.hpp.gch/opencv_stitching_RELEASE.gch
In file included from /home/pi/opencv-4.0.1/build/modules/stitching/precomp.hpp:90:0:
/home/pi/opencv-4.0.1/modules/core/include/opencv2/core/private.hpp:871:1: fatal error: can’t write PCH file: No space left on device
} // namespace cv
^
compilation terminated.
modules/stitching/CMakeFiles/pch_Generate_opencv_stitching.dir/build.make:62: recipe for target ‘modules/stitching/precomp.hpp.gch/opencv_stitching_RELEASE.gch’ failed
make[2]: *** [modules/stitching/precomp.hpp.gch/opencv_stitching_RELEASE.gch] Error 1
CMakeFiles/Makefile2:23170: recipe for target ‘modules/stitching/CMakeFiles/pch_Generate_opencv_stitching.dir/all’ failed
make[1]: *** [modules/stitching/CMakeFiles/pch_Generate_opencv_stitching.dir/all] Error 2
Makefile:160: recipe for target ‘all’ failed
make: *** [all] Error 2
cmake -D CMAKE_BUILD_TYPE=RELEASE
CMake Error: The source directory “/home/pi/opencv/build/CMAKE_BUILD_TYPE=RELEASE” does not exist.
Specify –help for usage, or press the help button on the CMake GUI.
please help
just use
(cmake ..)
i got is from “https://stackoverflow.com/questions/35784700/cmake-error-when-building-opencv-cmakelists-not-match”
cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-“$cvVersion”
-D INSTALL_C_EXAMPLES=ON
-D INSTALL_PYTHON_EXAMPLES=ON
-D WITH_TBB=ON
-D WITH_V4L=ON
-D OPENCV_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages
-D WITH_QT=ON
-D WITH_OPENGL=ON
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules
-D BUILD_EXAMPLES=ON ..
hey!! I have a raspberry pi 3 model B. I have used it a few times and it was working fine with no problems. I wanted to install opencv for a project and after I followed all the three command sudo apt-get update, sudo apt-get upgrade and sudo rpi-update and all these commands worked with no error. But I am getting a problem while rebooting. After rebooting, I get the rainbow screen and then raspberry logo and the console after which the screen goes blank with nothing but a cursor blinking at the top. The power led stays on without blinking while the act led keeps on blinking and nothing happens.
I am using a 8 GB micro sd card. So, do you have any ideas on what the problem is and how can I solve it?
Use 16gb class 10 micro sd card if not solved try chanfing dapter atleat 12v and 2 amp.
Add backslashes to the end of every line but the last one
You need to keep backslashes clean without any white spaces until end of line.
just use
(cmake ..)
i got is from “https://stackoverflow.com/questions/35784700/cmake-error-when-building-opencv-cmakelists-not-match”
What modifications do I need to make if I want to install to a virtual environment ?
when i try install libtiff4-dev it tells me there is no such thing but libtiff-dev replaces it
will this work?
you are a saviour bro!!! thank you
I get this error when i try cmake -D CMAKE_BUILD_TYPE=RELEASE :
CMake Warning at CMakeLists.txt:1178 (message):
The source directory is the same as binary directory. “make clean” may
damage the source tree
— Configuring incomplete, errors occurred!
See also “/home/pi/opencv/CMakeFiles/CMakeOutput.log”.
See also “/home/pi/opencv/CMakeFiles/CMakeError.log”.
I followed every step and got no error during the process. But still when I try to import cv2 I get
ModuleNotFoundError