I have been receiving so many queries from my readers to cover image-based applications of the raspberry pi.
Well, bending to popular demand, I think you can safely consider this as a starting point of me covering more sophisticated image-based applications of Raspberry Pi.
For the select few who are reading this post and don’t know what OpenCV is, here is a quick rundown.
OpenCV is short for Open Source Computer Vision and it is a library that contains programming functions and processes images and videos in order to identify objects, faces, and subtle things like handwriting.
OpenCV and raspberry pi is a very potent combination. Think about it, a credit card-sized computing machine capable of identifying faces, images, and objects.
I don’t know, what is your experience level of working with the Pi but that has to excite you, despite it.
In this article, we are not going to discuss any of those projects but something equally important nevertheless if not more.
Installing OpenCV poses to be a bit of a pickle for many first-timers.
In this article, let me share with you the easiest way to install OpenCV on Raspberry Pi.
Before you go ahead with learning how to install OpenCV on your Pi, here are a few more articles that I think should harness your interest, you can go ahead and open these articles on a new tab or bookmark them for later.
- Ultimate guide on setting up raspberry pi for beginners.
- Best way to learn raspberry pi and move from beginner to intermediate to advanced levels.
- How to stream videos from raspberry pi to your phone easily?
- Can raspberry pi be used for gaming?
- Can raspberry pi be used as a secondary computer?
Two Ways Of Installing OpenCV
Now if you want to deploy your raspberry pi for image processing projects, you need to install OpenCV on it.
So, how to install OpenCV on Raspberry Pi quickly and easily.
The reason I saw it quickly and easily is because there are two ways of installing OpenCV on the Pi.
- Pip Install (take about 30 to 60 seconds)
- Source Installation (can take hours)
In this article, because we are just discussing the easiest way of installing OpenCV on raspberry pi, we are going to discuss the pip installation method.
It is fast, easy to do, and should work on most beginner and intermediate image processing projects.
That being said, there will be certain libraries and algorithms that won’t get installed with Pip.
If your project needs the involvement of the full library, you will have to adopt the second method and build OpenCV from the source.
In any case, if you face problems in building up the complete OpenCV library, let me know in the comments section below.
I will add it in my planner to add an article on building the complete OpenCV library from the source here on Yantraas.
But for most people, this simple way of installing OpenCV should be enough.
You will see further in this article that I will be installing OpenCV using the contrib modules.
If you want an even lighter installation you can do so by installing OpenCV-python should you choose to do it.
So, let’s begin by doing the first obvious thing,
Setting Up Your Raspberry Pi
Of course, in order to be able to do anything with the Pi, you need to set it up first.
I have already created all the necessary posts to help you do just that. Find them below.
- How to properly format your SD card for OS reception for Raspberry Pi?
- How to install Raspbian OS on your SD card?
- One stop guide to getting started with Raspberry Pi for beginners.
- Best way to learn Raspberry Pi for beginners.
If you are a beginner, I highly recommend going through all of the mentioned articles to develop a comprehensive understanding.
For intermediates and people who have had some time elapsed since they last worked with their Pi here is a quick rundown.
Here is everything that you are going to need.
- Raspberry Pi 3B+ or better
- An HDMI Monitor
- A microHDMI to HDMI cable.
- USB Mouse And Keyboard
- MicroSD Card (Recommended 32 GB)
- A Card Reader
- Raspberry Pi Power Supply.
- An Ethernet Cable (For Wired Connection Else No Need)
- Raspberry Pi Case(optional)
Okay so here is what you do once you have each of these things.
- First off connect everything together, except for the microSD card and the power supply. Monitor, keyboard, mouse, ethernet cable, any other optional peripherals, case etc.
- Next, format your SD card properly using this guide. This is very important.
- Install Raspbian on your microSD card. Use this tutorial for guidance.
- Plug the microSD card into its slot in the Pi.
- Power the Pi on and follow on screen instructions till your reach the desktop environment.
Once you have set up and booted into the OS environment, there is one last thing that you need to do before starting the OpenCV installation.
And that is to ensure that your Raspberry Pi uses the full SD card.
This is how to do it.
Open the terminal and type the following command,
This should open a window as shown.
In the window select advanced options and in the next window select A1 Expand Filesystem.
Once that is done reboot your pi using the following command.
Once the restart process is complete, you can make sure that you are now using the maximum usable memory by using this command
Finally, make sure that everything is updated before we move on to the next steps.
Use the following command
sudo apt-get update && sudo apt-get upgrade
Once you have done that we are now ready to go ahead and install OpenCV on Raspberry Pi.
Installing OpenCV On Raspberry Pi
Now that we have properly set up our working environment let’s get pip and use it to move ahead with our installation.
Use the following commands in this order.
mkdir ~/src && cd ~/src wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py
I mentioned in the starting section of this article that if you are not choosing the complete OpenCV libraries, you can further use one of the two ways of installing it.
You can choose to install it for the complete system or you can use a python virtual environment.
If you are looking for portability or if your project demand is as such you can use the python virtual environment and you can use varied libraries with different projects on the same system.
But there isn’t a lot of complications involved in either of the installation process, so I will explain both of them here.
Installing OpenCV for Complete Setup
This is the command that you are going to use
sudo pip install opencv-contrib-python
And that is pretty much it. This will start the installation process.
Installing OpenCV In Python Virtual Environment
When working with python it is usually a good practice to use python virtual environments.
For beginners who aren’t too versed with what it, python virtual environment is nothing but a secluded development or testing environment that you can use for your project which is completely isolated from other operating environments.
In the context of the topic under discussion, the usage of python virtual environment is helpful in the sense that you will be able to manage all the python packages inside your virtual environment with pip.
There are many virtual environments to choose from like conda, pew, pipenv etc. but given the use case, I recommend going with virtualenv and virtualenvwrapper.
Now let’s go ahead and establish a virtual environment, after which we will install OpenCV.
Use these commands in order.
sudo pip install virtualenv virtualenvwrapper
After the installation is complete open the ~/.bashrc file using the following code
When the file opens up, add the following lines of code at the end of the file.
export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/local/bin/virtualenvwrapper.sh
Once you have done that save the file and exit using ctrl+x, y, enter.
You will have to reload the ~/.bashrc file in order to initiate the appended changes of the bash session.
Use this command to do just that,
Once this is done, you can initiate a virtual environment using any name you wish by using the following command.
I am using rpivir.
mkvirtualenv rpivir -p python3
Finally, install OpenCV using the following command.
pip install opencv-contrib-python
And you are basically done.
So that was the easiest way to install OpenCV on raspberry pi for you.
Once again, yes, this particular way of installing OpenCV on raspberry pi is relatively fast and easy but it has a catch.
Many advanced computer vision projects would want you to install and build the library up from the source.
Or in other words, do a get assessment of your project requirements and do a complete install of OpenCV, which the pip installation won’t give you.
So now that you have learned and understood how you can install OpenCV on your Raspberry Pi, you can proceed on with the project that you may have in your mind.
There is a very good chance though that you are a beginner and don’t exactly know what kinds of cool stuff can be accomplished by this potent combo of raspberry pi and OpenCV.
If that indeed is the case, here are some applications that you can subject your raspberry pi loaded with OpenCV to,
- Real Time face Recognition
- Automated Vision Object Tracing
- Traffic Counter Mechanism with Vehicle Speed Detection.
- Hand Gesture Recognition
- Advanced Security System with Image Processing.
- Color Based Robot Navigation.
- Automated Student Attendance.
And many, many more.
You see, I agree this will be a bit advanced for many first-timers. But don’t let that discourage you. In a recent article that I published, I relayed how learning by doing is probably the best way of learning practical electronics and raspberry pi.
So, say for instance you have learned how to install OpenCV on your Raspberry Pi, you can pick one of these projects to move from an intermediate pi user to an advanced one.
In any case, if you aren’t ready to take those steps, I have many articles here on Yantraas that will prepare you for it.
Raspberry Pi FAQs
When it comes to Raspberry Pi or electronics for that matter, the two things that are going to remain true throughout are QUESTIONS and BUGS.
And you can’t avoid it and know that I understand that.
In this article, the topic that we have discussed is the easiest way to install OpenCV on raspberry pi.
But I understand that any and all articles falling under the categories of DIYs, Raspberry Pi, Arduino, electronics, etc. need to be dynamic and organic in nature.
And therefore, I am appending this section of frequently asked raspberry pi questions here.
I will cover some important questions that I feel you would have in some parts of your mind.
But it won’t stop there. If you have any other questions in your mind write them down in the comments section below and if it happens to be an important one that addresses the masses, I will include it in this section.
So here goes,
How to check OpenCV version in Raspberry Pi?
Checking the OpenCV version in raspberry Pi is actually pretty simple.
Just open up your terminal and type,
$ pkg-config --modversion opencv
You should get your OpenCV’s version as the output.
You can also use python to know which version of OpenCV is installed. Use the following commands
$ python >>> import cv2 >>> cv2.__version__
The cv2.__version__is just a string that is used to split the version into its major and minor release form.
Is Raspberry Pi Actually Good For Image Processing?
With the new Raspberry Pi 4 packing advanced hardware and better specifications overall, Raspberry Pi will invite more demanding use cases including image processing. So, keeping older versions in context raspberry pi is indeed a great option for running image processing applications that also demand portability.
Can MATLAB run on raspberry pi?
Although, MATLAB can be installed in a 64-bit Linux OS, Broadcom to date hasn’t released blobs for running a 64-bit Linux Distro. Hence, you won’t be able to install MATLAB directly on the pi however with the R2018b release you can run your MATLAB developed code on the Pi as a standalone executable.
I hope I was able to make this article on the easiest way of installing OpenCV on raspberry pi as comprehensive, insightful, and informative as possible.
I fully understand that working with computer vision, python and the different Linux commands simultaneously on a project can be a bit too advanced for many beginners.
You definitely don’t need to be an absolute expert to work on these kinds of projects but you definitely need to have some groundwork.
You can also look at it this way, if you are an absolute beginner and aren’t ready to work with image processing as yet, you can keep these kinds of projects as a goal or an eventual benchmark you would like to reach.
From there on, the sky will be your limit.
Don’t forget to subscribe to the blog, so that I can send cool stuff like you just read and more directly to your mailbox.
Take care of yourselves and I will see you in the next one!