Installing OpenCV using CMake in Raspberry Pi
In this Tutorial, we will learn procedure to install Open CV on Raspberry Pi using CMake.
You might be here as other tutorials might not have worked for you. This tutorial will definitely help you to install OpenCV on your raspberry pi.
I assure you that this tutorial works and is personally tested by me. This installation is necessary if you're working on OpenCV projects.
Installation of OpenCV is crucial as many things are dependent on this library. The reason it is hard to install is due to the complicated process of installation.
This installation requires focus. If you make one wrong step, your library might not function as you want or might throw a lot of errors while executing.
In this article, I will try to keep the process of installation simple and effective. Let's first look into what is OpenCV?
What is OpenCV?
It is an open-source, computer vision & image processing library. OpenCV is highly optimized and is truly made for practical applications.
It was originally developed by Intel. We have already made another article on how to install OpenCV on raspberry pi. In this tutorial, we will be installing OpenCV using CMake.
We will be installing OpenCV version 4.0.0 as there is a possibility of error in the newer ones. It is tested by me personally and is stable. So now let's move ahead towards the installation Steps.
I would request to use raspberry pi 4 to set up the particular. You can also use a 3B+ or 3B if you want, but the processing power of Raspberry Pi 4 makes things a lot faster.
Installing OpenCV on Raspberry Pi using CMake
Before starting with the installation, you need to configure few things to ensure proper installation of OpenCV.
Step 1: Expanding File System
- Expanding the Filesystem is necessary so that the OpenCV library compiles properly. To expand your file system, go to the terminal and type the following command.
- After typing the above command, you will see a menu like the below image. Just press enter on Expand Filesystem and it will ask you for confirmation, select Yes.
- After that, the Pi will confirm for a reboot and then it will resize the partition.
Note: The mouse won't work on this screen, you need to use the keyboard.
Step 2: Updating System
- Updating is necessary before proceeding with the further installation.
sudo apt-get update && sudo apt-get upgrade
- This should download any latest packages available and install them. This process will take 15-20 minutes.
- Next, we will update the apt-get package.
sudo apt-get update
Step 3: Installing CMake
- CMake is necessary to compile the OpenCV Library. First, we will install snapd for installing the CMake package.
sudo apt install snapd
- Once the installation is completed, you will see something like the below image.
- We can now download and install the CMake package using the below command.
sudo snap install cmake --classic
- Your screen would look like the above image once the installation of CMake is completed.
Step 4: Installing Python
- Now we will install the python 3 development headers using the below command.
sudo apt-get install python3-dev
- My Pi already had it installed so it displays something like this.
Step 5:Getting OpenCV packages
- First, we will download the source code package of OpenCV and compile it on our Raspberry Pi using CMake.
- The next step would be to download the OpenCV Zip file from GitHub. Use the following command to do the same.
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip
- As you can see, we are downloading Open CV version 4.0.0
- OpenCV has some pre-built packages for python which will help us in developing stuff easier called the OpenCV contrib. So let’s also download that by using a similar command that is shown below.
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip
- At this point, you should have downloaded two zip files named “Opencv-4.0.0” and “opencv-contrib-4.0.0” on your home directory. You can check it out just in case if you want to be sure.
Step 6: Installing OpenCV Packages
- Let's unzip the opencv-4.0.0 zip file using the following command.
- Once unzipping is done, it will look like the below image.
- Similarly, also extract the opencv_contrib-4.0.0 using the command line
- Once unzipping is done, you will see something like the below image.
Step 7: Installing NumPy
- OpenCV requires NumPy as a requirement to work. So let’s install it using the below command.
pip install numpy
- Here numpy is already installed, so I see the above message that the requirement is already satisfied.
Step 8: Building Directory
- Now, we would have two directories named “opencv-4.0.0” and “opencv_contrib-4.0.0” in our home directory.
- The next step would be to compile the Open CV library, to do that we need to create a new directory called “build” inside the opencv-4.0.0 directory. Follow the below commands to do the same
cd ~/opencv-4.0.0 mkdir build cd build
Step 9: Instructions for Compiling
- Now, we have to run CMake for OpenCV. This is the place where we can configure how Open CV has to be compiled. Make sure you are in the path “~/opencv-4.0.0/build”. Then copy the below lines and paste in the terminal window.
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.0.0/modules \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D WITH_TBB=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D BUILD_EXAMPLES=OFF ..
- It should get configured without any errors and you should see the text “Configuring done” and “Generating done” as shown below.
- If you get any error in this process then make sure you have typed in the proper path and you have two directories named “opencv-4.0.0” and “opencv_contrib-4.0.0” in the home directory path.
Step 10: Compiling OpenCV
- This would be the most time-consuming step. Again make sure you are in the path “~/opencv-4.0.0/build” and use the following command to compile OpenCV.
- He would start building OpenCV and you would be able to see the progress in percentage. The process would take around 30-40 Minutes and if it gets completely built, you should see a screen like this above.
- The command “make –j4” makes use of all four cores to compile OpenCV. At 99% percentage, some people might find it taking too long for the process to complete wait patiently and it should get finished.
- For me, it did not work even after waiting for 25 minutes and an error came like in the above image so I build it again using “make –j1” and it worked.
- Using make –j1 uses only a single core of pi and it would take a longer time than make j4 but it's stable, so it is recommended to use make j4 and then uses make j1 since most of the compilation would be done by make j4.
- In case your code is not compiling at 100% and it gets an error like above, try using "make -j1". Also if it gets stuck at 63% or in the middle somewhere, delete all the files from the build folder and rebuild again with proper steps.
Step 11: Installing libopencv
- If you have reached this step then, you have sailed through the process. The final step would be to install libopecv using the following command.
sudo apt-get install libopencv-devpython-opencv
Step 12: Testing OpenCV
- Finally, you can check if the library was added successfully by running a simple python command.
Python import cv2
- You should not receive any error when you do this.
- If you get this screen then you can proceed with whatever OpenCV project you have in mind.
In this tutorial, we installed OpenCV on raspberry pi using CMake which is a very complex process. I hope my simple explanation helps you to install it correctly without errors. If you have any issues, post them in the comment section and I will try to help you resolve them.
Until Next Time! Peace Out!
Its cool but its not working for my python3 ??? do u have any solution for python3
Did you follow the above steps? what hardware are you on?