An image, digital image, or still image is a binary representation of visual information, such as drawings, pictures, graphs, logos, or individual video frames. Digital images can be saved electronically on any storage device.
In this post we are going to analysis the building blocks of an image — the pixel. We’ll discuss exactly what a pixel is, how pixels are used to build an image, and then how to access and manipulate pixels in OpenCV. By the end of this post you’ll:
- Have a full understanding of pixel.
- Understand the coordinate system(cartezian) of an image.
- Have the ability to access the Red, Green, and Blue (RGB) values of a pixel Along with set the RGB values of a pixel.
- Have a gentle introduction to extracting regions from an image.
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. like cell of our body, bricks in our home and employee in an organization. Normally, a pixel is considered the “color” appeared in screen which is nothing but an intensity of light that appears in our image.Think image as a grid than each box of that grid is pixel.
A gentle way to say that The pixel (a word invented from “picture element”) is the basic unit of programmable color on a computer display or in a computer image. Think of it as a logical – rather than a physical – unit. The physical size of a pixel depends on how you’ve set the resolution for the display screen.
it is an image of 450 * 450 pixel means it has 450 small rows and 450 columns and in total it is consist of 450 times 450 number of pixels. Most pixels are represented in two ways: grayscale and color. In a grayscale image, each pixel has a value between 0 and 255, where zero is corresponds to “black” and 255 being “white”. The values in between 0 and 255 are varying shades of gray, where values closer to 0 are darker and values closer 255 are lighter:
The grayscale gradient image in Figure above demonstrates darker pixels on the left-hand side and progressively lighter pixels on the right-hand side.
lor 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
Each of the three Red, Green, and Blue colors are represented by an integer in the range 0 to 255, which indicates how “much” of the color there is in short intensity. 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.
This RGB in combined represents as color to us as (red, green, blue) .
To construct a white color, we would fill each of the red, green, and blue buckets completely up, like this: (255, 255, 255) — since white is the presence of all color.
Then, to create a black color, we would empty each of the buckets out: (0, 0, 0) — since black is the absence of color.
To create a pure red color, we would fill up the red bucket (and only the red bucket) up completely: (255, 0, 0) .
Are you starting to see a pattern?
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)
An image histogram is a graphical representation of the number of pixels in an image as a function of their intensity.
Histograms are made up of bins, each bin representing a certain intensity value range. The histogram is computed by examining all pixels in the image and assigning each to a bin depending on the pixel intensity. The final value of a bin is the number of pixels assigned to it. The number of bins in which the whole intensity range is divided is usually in the order of the square root of the number of pixels.
Image histograms are an important tool for inspecting images. They allow you to spot BackGround and grey value range at a glance. Also clipping and Quantization Noise in image values can be spotted immediately.
In the one-dimensional histogram shown here below (as in the other linked examples), the number of pixels in each bin is represented both by the height of a bar, and by a color scale coloring each bar. Higher bars have colors closer to red.
By extending this histogram concept to multiple dimensions we can also make representations of Two Channel Histograms, or more.
As I mentioned in above, 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 upper left corner of the image. As we move down and to the right, both the x and y values increase.
Work with Pixels:
Till Now All we did was load an image off disk, display it, and then write it back to disk in a different image file format. Let’s do something a little more exciting and see how we can access and manipulate the pixels in an image.
lets make a python file call it pixel.py
Similar to our example in the previous basic code handle importing the packages we need along with setting up our argument parser. There is only one command line argument needed, –image , which is the path to the image we are going to work with.
So now after that we have the image loaded, how can we access the actual pixel values?
Remember, OpenCV represents images as NumPy arrays. Conceptually, we can think of this representation as a matrix. In order to access a pixel value, we just need to supply the x and y coordinates of the pixel we are interested in. From there, we are given a tuple representing the Red, Green, and Blue components of the image.
However, it’s important to note that OpenCV stores RGB channels in reverse order. While we normally think in terms of Red, Green, and Blue, OpenCV actually stores them in the order of Blue, Green, and Red.
So why in the world does OpenCV store images in BGR rather than RGB order?
The answer is efficiency. Multi-channel images in OpenCV are stored in row order — meaning that each of the Blue, Green, and Red components are concatenated together in sub-columns to form the entire image. Furthermore, each row of the matrix should be aligned to a 4-byte boundary, one byte for each of the Red, Green, Blue, and Alpha channels. Given our image, the last row of the image comes first in memory, thus we store the components in reverse order.
Now you can play with the coordinate system like print its different portion by running command
$ python pixel.py –image your_image.png
like by using opencv i figure out ross and chandler from the image yeah.
github code: click here.