Pixels in Opencv #165
imsanjoykb
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
OpenCV Getting and Setting Pixels
In the first part of this tutorial, you will discover what pixels are (i.e., the building blocks of an image). We’ll also review the image coordinate system in OpenCV, including the proper notation to access individual pixel values.
From there, we’ll configure our development environment and review our project directory structure.
With our project directory structure reviewed, we’ll implement a Python script, opencv_getting_setting.py. As the name suggests, this allows us to access and manipulate pixels using OpenCV.
We’ll wrap up this tutorial with a discussion of our results.
Let’s get started!
What are pixels?
Pixels are the raw building blocks of an image. Every image consists of a set of pixels. There is no finer granularity than the pixel.
Normally, a pixel is considered the “color” or the “intensity” of light that appears in a given place in our image.
If we think of an image as a grid, each square in the grid contains a single pixel. Let’s look at the example image in Figure 1:
Figure 1: This image is 600 pixels wide and 450 pixels tall for a total of 600 x 450 = 270,000 pixels.
Most pixels are represented in two ways:
Grayscale/single channel
Color
In a grayscale image, each pixel has a value between 0 and 255, where 0 corresponds to “black” and 255 being “white.” The values between 0 and 255 are varying shades of gray, where values closer to 0 are darker and values closer 255 are lighter:
Figure 2: Image gradient demonstrating pixel values going from black (0) to white (255).
The grayscale gradient image in Figure 2 demonstrates darker pixels on the left-hand side and progressively lighter pixels on the right-hand side.
Color pixels, however, are normally represented in the RGB color space — one value for the Red component, one for Green, and one for Blue leading to a total of 3 values per pixel:
Figure 3: The RGB cube.
Other color spaces exist (HSV (Hue, Saturation, Value), Lab*, etc.), but let’s start with the basics and move our way up from there.
Each of the three Red, Green, and Blue colors are represented by an integer in the range from 0 to 255, which indicates how “much” of the color there is. Given that the pixel value only needs to be in the range [0, 255], we normally use an 8-bit unsigned integer to represent each color intensity.
We then combine these values into an RGB tuple in the form (red, green, blue). This tuple represents our color.
To construct a white color, we would completely fill each of the red, green, and blue buckets, like this: (255, 255, 255) — since white is the presence of all colors.
Then, to create a black color, we would completely empty each of the buckets: (0, 0, 0) — since black is the absence of color.
To create a pure red color, we would completely fill the red bucket (and only the red bucket): (255, 0, 0).
Are you starting to see a pattern?
Look at the following image to make this concept more clear:
Figure 4: Here, we have four examples of colors and the “bucket” amounts for each of the Red, Green, and Blue components, respectively.
In the top-left example, we have the color white — each of the Red, Green, and Blue buckets have been completely filled to form the white color.
And on the top-right, we have the color black — the Red, Green, and Blue buckets are now totally empty.
Similarly, to form the color red in the bottom-left, we simply fill the Red bucket completely, leaving the other Green and Blue buckets totally empty.
Finally, blue is formed by filling only the Blue bucket, as demonstrated in the bottom-right.
For your reference, here are some common colors represented as RGB tuples:
Black: (0, 0, 0)
White: (255, 255, 255)
Red: (255, 0, 0)
Green: (0, 255, 0)
Blue: (0, 0, 255)
Aqua: (0, 255, 255)
Fuchsia: (255, 0, 255)
Maroon: (128, 0, 0)
Navy: (0, 0, 128)
Olive: (128, 128, 0)
Purple: (128, 0, 128)
Teal: (0, 128, 128)
Yellow: (255, 255, 0)
Now that we have a good understanding of pixels let’s have a quick review of the coordinate system.
Overview of the image coordinate system in OpenCV
As I mentioned in Figure 1, an image is represented as a grid of pixels. Imagine our grid as a piece of graph paper. Using this graph paper, the point (0, 0) corresponds to the top-left corner of the image (i.e., the origin). As we move down and to the right, both the x and y-values increase.
Let’s look at the image in Figure 5 to make this point more clear:
Figure 5: In OpenCV, pixels are accessed by their (x, y)-coordinates. The origin, (0, 0), is located at the top-left of the image. OpenCV images are zero-indexed, where the x-values go left-to-right (column number) and y-values go top-to-bottom (row number).
Here, we have the letter “I” on a piece of graph paper. We see that we have an 8 x 8 grid with 64 total pixels.
The point at (0, 0) corresponds to the top-left pixel in our image, whereas the point (7, 7) corresponds to the bottom-right corner.
It is important to note that we are counting from zero rather than one. The Python language is zero-indexed, meaning that we always start counting from zero. Keep this in mind, and you will avoid a lot of confusion later on.
Finally, the pixel 4 columns to the right and 5 rows down is indexed by the point (3, 4), keeping in mind that we are counting from zero rather than one.
Configuring your development environment
To follow this guide, you need to have the OpenCV library installed on your system.
Luckily, OpenCV is pip-installable:
$ pip install opencv-contrib-python
If you need help configuring your development environment for OpenCV, I highly recommend that you read my pip install OpenCV guide — it will have you up and running in a matter of minutes.
Having problems configuring your development environment?
Figure 6: Having trouble configuring your development environment? Want access to pre-configured Jupyter Notebooks running on Google Colab? Be sure to join PyImageSearch Plus — you will be up and running with this tutorial in a matter of minutes.
All that said, are you:
Short on time?
Learning on your employer’s administratively locked system?
Wanting to skip the hassle of fighting with the command line, package managers, and virtual environments?
Ready to run the code right now on your Windows, macOS, or Linux system?
Then join PyImageSearch Plus today!
Gain access to Jupyter Notebooks for this tutorial and other PyImageSearch guides that are pre-configured to run on Google Colab’s ecosystem right in your web browser! No installation required.
And best of all, these Jupyter Notebooks will run on Windows, macOS, and Linux!
Project structure
Before we start looking at code, let’s review our project directory structure:
$ tree . --dirsfirst
.
├── adrian.png
└── opencv_getting_setting.py
0 directories, 2 files
We have a single Python script to review today, opencv_getting_setting.py, which will allow us to access and manipulate the image pixels from the image adrian.png.
Getting and setting pixels with OpenCV
Let’s learn how to get and set pixels with OpenCV.
Open the opencv_getting_setting.py file in your project directory structure, and let’s get to work:
Lines 2 and 3 import our required Python packages. We only need argparse for our command line arguments cv2 for our OpenCV bindings.
Beta Was this translation helpful? Give feedback.
All reactions