Getting Started OpenCV Python

3 Rookie Mistakes People Make Installing OpenCV | Avoid It!

Pinterest LinkedIn Tumblr

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:

  1. opencv-python: The repository includes only the main modules of the OpenCV library. And trust me, you don’t want to install this package.
  2. 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:

  1. Installing directly into your system site packages. 
  2. 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:

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:

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).

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:

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:

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.

Now, let’s create a virtual environment for OpenCV called opcv (an acronym for OpenCV computer vision) using Python 3.

Once that’s completed, you may proceed to pip install OpenCV into your newly isolated environment with the final step.

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:

Given “opcv” is the name of the environment we created earlier.

Once you are done for the day, to deactivate the environment, execute:

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:

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.

Write A Comment