Getting started with OpenCV and Python

Photo by Pop & Zebra on Unsplash

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

The license implies that it is free to use for both academic and commercial use. It supports a couple of programming languages namely: python, java, c and C++. On the other hand, it supports Windows, Linux, Mac Os and even the Android operating systems.

The whole series will take you through setting up OpenCV and python on your machine, carrying out some simple tasks on images and videos, and lastly, we will advance to detecting objects.

Python as most of you hopefully already know, is

a programming language that let’s you work quickly and integrate systems more effectively.

Installation!

This tutorial will majorly cater for Linux, Windows, and MacOS for now.

Setting up on windows

Install OpenCV

I assume you have python on your machine. Incase you need help with that here you go.

Run the command below in your terminal to install python OpenCV.

$ pip install opencv-python

Test your OpenCV installation on the python emulator.

> import cv2

Your result should be as shown below.

Your OpenCV is installed!

Setting up on mac

At this point I will assume you already have Command Line Tools and Homebrew already installed on your computer.The links to the same are attached above.

To install python:

brew install python python3

Installing OpenCV:

brew install opencv3 --with-contrib --with-python3

It’s always a good idea to test your installation.

> import cv2

Your result should be as shown below.

Your OpenCV is installed!

Setting up on linux

Python comes pre-installed in linux, therefore, we will go direct to openCV installation.

Install OpenCV

$ sudo apt-get install libopencv-dev python-opencv
And it’s a wrap

Now that we are done with installation, let’s start coding.

Simple operations on images using OpenCV

Using OpenCV, we can read an image, display it and after we are done with our operations for the day, save it.

Let’s get right into it!

Read you image

Reading an Image in OpenCV

We will use the OpenCV function cv2.imread(). The function cv2.imread() requires two arguments: the first is the path to the image itself and the second specifies the way the image should be read.

What are some of these specifications?

Here we go:

cv2.IMREAD_COLOR — this flag in particular is used to load a color image. It neglects the image transparency and is the default flag. It is mostly for 8-bit images that don’t have the alpha channel.
cv2.IMREAD_GRAYSCALE — it is responsible for loading our images in grayscale.
cv2.IMREAD_UNCHANGED — It loads an image using alpha channel(RGBA).

You can also pass integers 1, 0, -1 respectively in place of the flags.

It is always a good idea to try printing the image so it doesn’t give us an error when the path or image is wrong. Otherwise you will end up with TypeErrors and have no idea where you went wrong.

Note: This is one place you could go wrong. Always make sure that the path to the image and the image specified exists and that you used the right extension for the image.

import cv2
# Load an color image in grayscale
image = cv2.imread(‘watch.jpg’,0)

That line alone is all that is required of us to read an image. Now let’s get to out next step.

Display your image

Displaying an image with OpenCV

Now that we can read an image, our next step will be to display it.

cv2.imshow(‘image’,image) # the title and path to image
cv2.waitKey(0) # Waits for the next key to be pressed
cv2.destroyAllWindows()

cv2.waitkey(0) — in milliseconds. It waits for a key press to do an action.

cv2.destroyAllWindows() — It destroys all windows created.

cv2.destroyWindow() — destroys the exact window name passed as an argument.

Save you image

Saving an image with OpenCV

Now that we have our image. Probably changed our image to grayscale, we need to store it. OpenCV’s cv2.imwrite function enables you to save images to the specified path.

cv2.imwrite(‘watchgray.png’,image)

The above command will save our image in the working directory.

That’s all folks!

Putting it all together

We can create a simple code snippet that loads an image in grayscale, displays it, and at the end, saves it.

Reading, Displaying and Saving an image

There are many more operations you can carry out on images. You can even create your favorite memes.