Hello everyone, It’s David Praise and welcome back to Neuraspike, and in this tutorial, I’ll show you how to install OpenCV 4 on your Linux operating system.
Well, I know configuring your development environment can be a hassle, as I recall about four years ago getting started in the field of computer vision; It wasn’t an easy fight, battling with the command line, package managers, and virtual environment all day until I was able to take charge of the installation.
The Two pip OpenCV packages are: opencv-python
and opencv-contrib-python
Before you fire up your command line and hit pip install opencv-python-contrib, be aware you have two options when installing OpenCV on Ubuntu using pip:
- opencv-python: The repository includes only the main modules of the OpenCV library. And trust me, you don’t want to install this package.
- opencv-contrib-python: This repository contains the main modules and the contrib modules. This package is the library I strongly recommend you go ahead and install. It includes all of the OpenCV functionalities required.
Note: You DO NOT want to install both opencv-python
and opencv-contrib-python
on the same system or environment. So pick one.
How to pip install OpenCV on Ubuntu
Before you fire up your command line and hit pip install opencv-python-contrib, be aware you have two options when installing OpenCV on Ubuntu using pip:
- Installing directly into your system site packages.
- Installing into a virtual environment’s site packages (Strongly recommended).
You might ask yourself, what exactly is a virtual environment? Well, here’s a quick explanation.
I remember a time I was working on multiple projects, which used different versions of SKLearn (Machine Learning library). After completing the task, I moved on with the other project that used the same library but a different version because I had updated my Python packages.
As I returned to the previous project I had completed, I noticed things were crashing. I was getting frustrated. Until I realized I was using a more recent version of SKLearn.
To summarize, the point of virtual environments is creating isolated environments. The dependencies packages and specific versions your project needs will be within that container and wouldn’t affect other projects.
So learn from my mistakes :-). Now let’s get started with installing OpenCV on Ubuntu. Fire up your command line and follow along.
First, install pip
You should have pip already installed on your system. However, if you don’t have pip installed, you’ll need to acquire it first with the following commands:
1 2 3 | $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py $ pip install --upgrade pip --user |
Option A: Install OpenCV on your Ubuntu system with pip
If you don’t want to go through the chaos I experienced, you wouldn’t want this method. Use this method only if you have a particular scenario where you don’t want to isolate independent Python environments.
Now let’s pip install opencv-contrib-python on our system:
1 | $ sudo pip3 install opencv-contrib-python |
Depending on your internet connection, you should have OpenCV in your system’s site-packages within minutes, and you’re ready to go.
Option B: Install OpenCV on Ubuntu into a virtual environment with pip
With the second option (highly recommended), you will need to install both the virtualenv and virtualenvwrapper, which lives within our system’s site-packages and manages each virtual environment site-package we will later make.
Note: Some other alternatives are venv or Anaconda (or conda for short).
1 2 | $ pip3 install virtualenvwrapper $ sudo apt install virtualenv |
After installing that, you will need to add a few lines to your ~/.bashrc profile. So on the same terminal still opened, paste each line one by one and press the enter button:
1 2 3 4 | $ echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc $ echo 'WORKON_HOME=$HOME/.virtualenvs' >> ~/.bashrc $ echo 'VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3' >> ~/.bashrc $ echo 'source /home/{username}/.local/bin/virtualenvwrapper.sh' >> ~/.bashrc |
For the command on line 4, you should make sure you replace “{username}” with the username on your system.
Once you have executed all of the above, you should then “source it” in your terminal with the command:
1 | $ source ~/.bashrc |
Congratulations if you made it this far. We are close to getting everything set up. Once you’ve sourced it, you will have access to new terminal commands:
- Create an environment using
mkvirtualenv
envname. - Activate an environment (or make a switch to a different one) with
workon
envname. - Deactivate an environment with
deactivate
envname. - Remove an environment using
rmvirtualenv
envname.
For more information, be sure to refer to the virtual environment’s documentation page.
1 | $ mkvirtualenv opcv -p python3 |
Now, let’s create a virtual environment for OpenCV called opcv (an acronym for OpenCV computer vision) using Python 3.
1 2 | $ pip3 install numpy $ pip3 install opencv-contrib-python |
Once that’s completed, you may proceed to pip install OpenCV into your newly isolated environment with the final step.
1 2 3 4 5 | $ pip3 install pandas $ pip3 install argparse $ pip3 install scipy matplotlib $ pip3 install scikit-learn scikit-image $ pip3 install -U Flask |
You may also install some additional libraries required for computer vision, image processing, and machine learning.
Testing our pip install of OpenCV
Alright, we are done.
The next time you want to continue working with your environment, you should execute:
1 | $ workon opcv |
Given “opcv” is the name of the environment we created earlier.
Once you are done for the day, to deactivate the environment, execute:
1 | deactivate |
How to remove virtual environment
If you’ve messed up something and you are looking to remove the virtual environment created. You can easily perform this operation with the following command. By using the syntax:
1 | rmvirtualenv opcv |
What’s Next?
Now, what’s next? in the following tutorial, I will show you how to load and display images and find spatial dimensions about your images; including the width, height, and the number of channels. Then finally, how to save your image.
Further Reading
We have listed some useful resources below if you thirst for more reading.