 # Contents Outline # Numpy & OpenCV In Action

Apr 22, 2020 5 minutes read

Numpy is a powerful free source python library, which is widely used mainly for mathematical and statistical calculations. This is because many of its operations are based on vector calculations using multidimensional arrays. Also, other important libraries like Pandas and OpenCV make use of Numpy.

In this post we will see a little bit of the efficiency of Numpy when selecting the data presented in rows and columns. Additionally, we will talk about OpenCV which is widely used in image processing, machine learning and computer vision. Also, we will understand how it relates to Numpy. Finally, we will work on basic operations with OpenCV and how we can make use of Numpy in image processing.

REQUIREMENTS

To start working with Numpy and OpenCV we must previously have python installed in the computer. In the link below you will find the download page to get Python.

Once Python is installed, we can proceed with getting Numpy and OpenCV as follows in the steps below.

To get Numpy just use this command "pip install numpy" in the command line of your computer. In the link below, you can find a brief description.

Similarly for OpenCV, we can use this command "pip install opencv-python". Also, you can see the link below for a brief description.

FIRST STEPS WITH NUMPY

The first step is to import the library using the keyword "import". As follows.

`import numpy as np`

Already imported Numpy , we can create a list in which we will find numbers between 0-10 that denotes the score of a survey for a new product launched in the market. The idea is to select the scores using Python and Numpy and see the difference in selection.

-Selection of unique data

-Column selection

-Column selection but not complete

As we can see Python makes a lot of use of for looping for its selection. This takes time and resources to take one data at a time. On the other hand, Numpy uses vectors to make a faster selection. Likewise its mathematical operations are based on vectorization.

-Basic operations with Numpy

Below we download a dataset that presents the number of deaths from Malaria by year and continent. From this data, we will see how to extract the information to be analyzed. Next, we'll apply basic Numpy operations.

-First, we import the csv where the information is contained as a list.

-Transform the list into a Numpy array.

-Then, we extract the Malaria deaths that occurred in 2000 for the different continents.

-We can check this by doing it with Python and then see the difference.

-As we can see, we got the same values. However, these values are strings. Now we'll convert them to integers

-Now that they are whole, we can add up the values and know the total number of malaria deaths in the year 2000.

-Also, get the average.

-Maximum and minimum value

Let's talk a little bit about OpenCV

OpenCV is a powerful library for working with computer vision, machine learning and image processing among many other applications. A very interesting point of this library, is that it makes use of Numpy to handle all its vector structure. This makes it much easier to use certain complex operations since Numpy handles math, statistics and vectors very well. Another plus point is the integration with other Numpy based libraries like Scipy and Matplotlib.

Next, we will see how to import OpenCV and read an image from this library and display it with Matplotlib. Based on OpenCV,Numpy and Matplotlib we will develop each item listed below.

-Import OpenCV and Matplotlib.

-read an image with OpenCV in grayscale and RGB filter.
-Check the type and dimensions of the image (grayscale and RGB).
Para la imagen de escalas de grises:

In this section we can see that a grayscale image is a 2-dimensional Numpy array. Which is of type "Int8". which means that it is composed of values between 0-255. These two values can be compared with the color Black for 0 and White for 255 in the color range.

On the other hand an image with RGB filters (red,green,blue) is a 3-dimensional Numpy array.

Now that we know this, we can work with the images as we did with the 2-dimensional Numpy arrays.

But first, let's see what the images look like.

For the RGB image, we can see how each filter looks.
Now let's play with the 2-dimensional images.

-We can show the top half of the grayscale image.

Also with Numpy we can rotate the images.

Additionally, there is an option to rotate the image 90 degrees.

Finally, let's take the negative out of an image with Numpy.

Arithmetic operations with images using Numpy and OpenCV

Let's import a new image and adjust its size to the same as the first imported image with a shape of (600,571).

Let's see the addition of images with OpenCV.

Now with Numpy.

When using Numpy the results are based on module operations and the images do not look very good. On the contrary, using openCV is much better because it is based on saturation at either 255 or 0 depending on the operation. To better understand this concept of module and saturation, simply understand that Numpy takes the module out of the operation either by addition or subtraction. However, OpenCV uses saturation because it always tries to approximate 255 when it is addition or 0 when it is subtraction, so the images look cleaner.

For subtraction with Numpy we can see as follows below.