Reader small image

You're reading from  Mastering OpenCV 4 with Python

Product typeBook
Published inMar 2019
Reading LevelExpert
PublisherPackt
ISBN-139781789344912
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Alberto Fernández Villán
Alberto Fernández Villán
author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán

Right arrow

Image Basics in OpenCV

Images are a key component in a computer vision project because they provide, in many cases, the input to work with. Therefore, understanding main image concepts is the basic knowledge you need to start coding your computer vision projects. Also, some of the OpenCV library peculiarities, such as the coordinate system or the BGR order (rather than RGB), will be introduced.

In this chapter, you will learn how to start writing your first scripts, which will introduce you to the OpenCV library. At the end of this chapter, you will have enough knowledge to start programming your first computer vision project in OpenCV and Python.

In this chapter, we will cover the following topics:

  • A theoretical introduction to image basics
  • Concepts of pixel, colors, channels, images, and color spaces
  • The coordinate system in OpenCV
  • Accessing and manipulating pixels in OpenCV...

Technical requirements

The technical requirements for this chapter are listed as follows:

  • Python and OpenCV
  • A Python-specific IDE
  • The NumPy and Matplotlib packages
  • Jupyter Notebook
  • Git client

Further details about how to install these requirements can be found in Chapter 1, Setting Up OpenCV. The GitHub repository for Mastering OpenCV with Python, which contains all the supporting project files that are necessary to work through this book from the first chapter to the last, can be accessed at https://github.com/PacktPublishing/Mastering-OpenCV-4-with-Python.

A theoretical introduction to image basics

The main purpose of this section is to provide a theoretical introduction to image basics these will be explained in detail in the next section. First, a quick introduction will be given to underline the importance of some of the difficulties you will encounter when developing the image-processing set in a computer vision project, and then some simple formulation will be introduced in connection with images.

Main problems in image processing

The first concept to introduce is related to images, which can be seen as a two-dimensional (2D) view of a 3D world. A digital image is a numeric representation, normally binary, of a 2D image as a finite set of digital values, which...

Concepts of pixels, colors, channels, images, and color spaces

There are several different color models, but the most common one is the Red, Green, Blue (RGB) model, which will be used to explain some key concepts concerning digital images.

In Chapter 5, Image Processing Techniques, the main color models (also known as color spaces) will be fully explained.

The RGB model is an additive color model in which the primary colors (R, G, B) are mixed together to reproduce a broad range of colors. As we previously stated, in the RGB model, the primary colors are red, green, and blue.

Each primary color, (R, G, B), is usually called a channel, which is commonly represented as an integer value in the [0, 255] range. Therefore, each channel produces a total of 256 discrete values, which corresponds to the total number of bits that are used to represent the color channel value (28...

The coordinate system in OpenCV

To show you the coordinate system in OpenCV and how to access individual pixels, we are going to show you a low-resolution image of the OpenCV logo:

This logo has a dimension of 20 × 18 pixels, that is, this image has 360 pixels. So, we can add the pixel count in every axis, as shown in the following image:

Now, we are going to look at the indexing of the pixels in the form (x,y). Notice that pixels are zero-indexed, meaning that the upper left corner is at (0, 0), not (1, 1). Take a look at the following image, which indexes three individual pixels. As you can see, the upper left corner of the image is the coordinates of the origin. Moreover, y coordinates get larger as they go down:

The information for an individual pixel can be extracted from an image in the same way as an individual element of an array is referenced in Python. In the...

Accessing and manipulating pixels in OpenCV

In this section, you will learn how to access and read pixel values with OpenCV and how to modify them. Additionally, you will learn how to access the image properties. If you want to work with many pixels at a time, you need to create Region of Image (ROI). In this section, you will learn how to do this. Finally, you will learn how to split and merge images.

Remember that in Python, images are represented as NumPy arrays. Therefore, most of the operations that are included in these examples are related to NumPy, so a good understanding about the NumPy package is required to both understand the code included in these examples and to write optimized code with OpenCV.

Accessing and manipulating pixels in OpenCV with BGR images

...

BGR order in OpenCV

We already mentioned that OpenCV uses the BGR color format instead of the RGB one. This can be seen in the following diagram, where you can see the order of the three channels:

The pixel structure of a BGR image can be seen in the following diagram. In particular, we have detailed how to access pixel (y=n, x=1) for clarification purposes:

Initial developers at OpenCV chose the BGR color format (instead of the RGB one) because at the time, the BGR color format was very popular among software providers and camera manufacturers. For example, in Windows, when specifying a color value using COLORREF, they used the BGR format, 0x00bbggrr (https://docs.microsoft.com/es-es/windows/desktop/gdi/colorref). In summary, BGR was chosen for historical reasons.

Additionally, other Python packages use the RGB color format. Therefore, we need to know how to convert an image...

Summary

In this chapter, we looked at the key concepts related to images. Images constitute rich information that's necessary to build your computer vision projects. OpenCV uses the BGR color format instead of RGB, but some Python packages (for example, Matplotlib) use the latter format. Therefore, we have covered how to convert the image from one color format into the other.

Additionally, we have summarized the main functions and options to work with images:

  • To access image properties
  • Some OpenCV functions, such as cv2.imread(), cv2.split(), cv2.merge(), cv2.imshow(), cv2.waitKey(), and cv2.destroyAllWindows()
  • How to get and set image pixels in both BGR and grayscale images

Finally, we included two notebooks, which let you play with all these concepts. Remember that once you have loaded the notebook, you can run it step by step by pressing Shift + Enter or run the notebook...

Questions

  1. What are the main image-processing steps?
  2. What are the three processing levels?
  3. What is the difference between a grayscale image and a black and white image?
  4. What is a pixel?
  5. What is image resolution?
  6. What OpenCV functions do you use to perform the following actions?
    • Load (read) an image
    • Show an image
    • Wait for a keystroke
    • Split the channels
    • Merge the channels
  7. What command do you use to run the Jupyter Notebook?
  8. What color will you get with the following triplets?
    • B = 0, G = 255, R = 255
    • B = 255, G = 255, R = 0
    • B = 255, G = 0, R = 255
    • B = 255, G = 255, R = 255
  9. Suppose that you have loaded an image in img. How do you check whether img is color or grayscale?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering OpenCV 4 with Python
Published in: Mar 2019Publisher: PacktISBN-13: 9781789344912
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán