Introduction to Computer Vision with OpenCV | Part 1

Rafael Messias Grecco
Analytics Vidhya
Published in
4 min readFeb 16, 2021

One of the simplest situations in our daily lives is that you have to look both ways before crossing the street to identify whether it is safe to pass at that moment or not,

At first seems to be a simple situation, in fact it is not, since in this process we are using perhaps one of the most complex functions that our brain is able to perform: Process what we see.

Therefore, teaching a machine to see what we see is a very complex process, due to some factors: We cannot fully reproduce human vision through mathematical equations and we still do not have a complete understanding of how our brain performs this processing.

Thus, we have already found an excellent definition of what Computer Vision: A process that seeks to replicate human vision using machines.

In the first part of the article, we will understand the most elementary concepts and learn some tools to start writing you first Computer Vision script in Python. So, to make it easier for you to understand the following steps, you will need a notion of Python and Google Colaboratory (An online Python programming environment, that is, you will not need to install anything on your machine).

Knowing the OpenCV

OpenCV (Open Source Computer Vision) was initially developed to make Computer Vision a little simpler, and the goal has been achieved, it is currently one of the most complete libraries i know of and available for Python, C++, Java etc.

In this article it will be demonstrated using Python, as already said, so if you want to use your machine yo follow the tutorial, installing it using pip is very simple, with the following command: pip install opencv-python . For those who will use Google Colab, this process is not necessary.

First Code with OpenCV

Firstly what we will do with OpenCV is to open an image, but first necessary to import the library, using import cv2 , then i will import it and some other libraries that we will use throughout the article.

Attention: The second line is only necessary for people who are using the colab.

After importing the libraries we are going to use, we will read an image and view it.

The expected output is an image, in this example u used the following image:

Opening an image to view is an extremely simples process, making use of this library, just inform the image path in quotes in the imread() function and after that use the cv2_imshow() function.

How the machine “Sees” an image

On machines, images are processed in the form of a 3-dimensional numerical matrix, where the height and width of that matrix correspond to the dimensions of the image, and the third dimension corresponds to the image channels, for example, if your image is colored, the number of channels will be 3 (RGB) representing the colors Red, Green, Blue.

Analyzing what has been said, and with the aid of the image, we can conclude each “square” within our matrix represents a pixel and its value corresponds to the intensify of the color at that point.

This can be demonstrated with our code, as the image is treated as matrix by the computer and numpy is an excellent library for performing operations on multidimensional arrays, you can use it to view some information about the image

With the shape method of numpy it is possible to visualize the dimensions of the matrix that represents our image

print(img.shape) -> (512, 512, 3)

The output obtained was exactly what we expected, an array of 3 values, where the first two represent the width and height and the last value represents the number of color channels, as i used a color image, 3 channels were shown.

And we have reached the end of the first part of the article, in the next step we will see how to convert an image to shades of gray and also create our first project to put on github.

If you want to know my repository on github here is the LINK.

--

--

Rafael Messias Grecco
Analytics Vidhya

Electrical engineering student and technology enthusiast. Projects in fields such as Machine Learning, Time Series and Computer Vision. https://www.linkedin.com