Computer vision with OpenCV and Python

Contents

This article was published as part of the Data Science Blogathon

Introduction

Data is often defined as raw facts. Information refers to any amount or quantity of data that has been processed and has more value than the raw facts themselves. The key difference is that decisions and actions can be taken based on the study of the information. no Base decisions and actions on data, only information.

41245sources_of_data_collection-6405231

Source: StudiousGuy

Summary

Following our Previous article and Computer Vision, now we are going to further explore the world of Computer Vision in the Python programming language, using the OpenCV Python package. This article will show us how to perform some of the many operations that OpenCV offers in the Python programming language.. In the previous article, We examine the following block of code and now we will look at some more aspects in this article.

import cv2
# load it in GRAYSCALE color mode...
image = cv2.imread("""C:/Users/Shivek/Pictures/487px-OpenCV_Logo_with_text_svg_version.svg.png""", 0)
cv2.imshow('DataPeaker Computer Vision', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
869691_jqofwnwo5anck5ug2ezdgw-5841165

Source: Half

Understanding CV Basics

As you may have seen, in our previous article, we have read (loaded) the image of the OpenCV logo in our system memory, using the built-in method of the OpenCV library, imread (). By using this method, we pass two arguments, namely, a filename and a flag. The file name specified the name and location of the file on your personal computer, while the flag could look like the picture color setting. Remembering it, we remember that we read the image in our memory in a GRAY SCALE color format.

Now, the most crucial aspect to understand OpenCV:

Images are data. When you use the imread method () de OpenCV, you are converting the raw image data to other data type. The new data type is one that everyone at DataPeaker is very familiar with and is A NumPy array comprising integers. Each element of the array represents the intensity of the pixel color and can have one or more elements inside. Since we have loaded our matrix in GRAYS SCALE color format, one will find that each pixel is represented as a single value that can take an equal value and goes from zero (0) to two hundred and fifty five (255 ). As one goes from 0 at higher values, the intensity of a specific pixel increases, what makes it more eye-catching.

0 = Black color.
255 = White color.

Basically, what I am trying to convey is the following (I have omitted the font as it is the same as in the previous article, and also to allow the flow of information):

We start with the following image:

65619487px-opencv_logo_with_text_svg_version-svg_-6639257

Then, we returned the image in GRAY SCALE and got the image in a new format as shown below:

14-1-2178252

Ahora imprimiremos el contenido de la variable que está almacenando nuestra imagen en ESCALA DE GRISES:

# variable image stored our GRAYSCALE image
print(image)

-We receive the output of the above code as follows:

14-2-7855870

Now we will print the variable image type:

print(type(image))

The output will look like this:

14-3-1121119

Basically, OpenCV has transformed our image into a NumPy matrix, in which there are values ​​of 0 a 255 representing the intensity of the pixels, that correspond to the colors that we see in the image GRAY SCALE. Remember that GRAY SCALE images will always return an array in which each pixel has a unique value ranging from 0 (negro) a 255 (White).

Returning the shape of the matrix.

Let's print the NumPy matrix shape to the console.

print(image.shape)

The output will look like this:

14-4-4304073

Our matrix has 600 rows and 487 columns. In image terminology, it would be said that the image has dimensions of 600 pixels (alto) by 487 pixels (width).

Printing the image with pixels

Since OpenCV has transformed the pixels of our image into a NumPy array with integers, we can perform NumPy operations on the array containing the image pixel values ​​and manipulate the array.

Our matrix is ​​two-dimensional. This means it has rows and columns. Let's do some indexing and division on the array, and we will return the content.

cv2.imshow("OF", image[0:100])

cv2.waitKey()

cv2.destroyAllWindows()

If one is familiar with the indexing and cutting technique, you can see that we are trying to cut a portion of our image matrix (NumPy). Again, it is crucial to understand and be aware of the fact that the OpenCV library in the Python programming language renders its images and associated objects as NumPy nd-Arrays.

98808numpy-array-2607932

Source: AI production in India.

Explanation of the code.

The line-by-line explanations for the above code block are as follows:

cv2.imshow (“DataPeaker Computer Vision”, picture[0:100])

The imshow method () used to display an image on the screen using a GUI. But nevertheless, in this particular instance we pass a name for the GUI window, Y just a portion of the pixel matrix, using cut. Specifically, we want to return the first 100 rows (height) from image.

cv2.waitKey()

This will wait infinitely for the GUI window to close, namely, the action / user interaction will close this window. You can pass an integer value as an argument representing the duration (in milliseconds), the GUI window should wait before automatically terminating.

cv2.destroyAllWindows()

The above line of code will terminate all active OpenCV GUI windows / open. You can pass the name of a specific GUI window to end as a string.

The output will look like this:

92081picture1-6384376

Therefore, we have successfully returned the first 100 rows of pixels from our image. Feel free to experiment with the image and see if the pixel in a particular position matches the color found in the image itself..

This concludes my article on Computer vision with Python. Hope you enjoyed reading this article and learned a new concept.

Please, feel free to connect with me on LinkedIn.

Thanks for your time.

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker