The img.shape function returns the shape of an image, that is, its dimensions and the number of color channels. The output of the preceding code is as follows:
pi@pi02 ~/book/code/chapter03 $ python prog1.py
(512, 512, 3)
786432
uint8
If the image is colored, then img.shape returns a triplet containing the number of rows, columns, and channels in the image. Usually, the number of channels is three, representing the red, green, and blue channels. If the image is grayscale, then img.shape only returns the number of rows and columns. Try modifying the preceding code to read the image in grayscale mode and observe the output of img.shape.
The img.size function returns the total number of pixels and img.dtype returns the image data type.
On several occasions, we may be interested in working separately with the red, green, and blue channels. For example, we might want to build a histogram for every channel of an image.
Note
We will work separately with the different channels in Chapter 8, Histograms, Contours, Morphological Transformations, and Performance Measurement.
Here, cv2.split() is used to split an image into three different intensity arrays for each color channel, whereas cv2.merge() is used to merge different arrays into a single multi-channel array, that is, a color image.
In mathematical terms, the negative of an image is the inversion of colors. For a grayscale image, it is even simpler! The negative of a grayscale image is just the intensity inversion, which can be achieved by finding the complement of the intensity from 255. A pixel value ranges from 0 to 255, and therefore, negation involves the subtracting of the pixel value from the maximum value, that is, 255. The code for the same is as follows:
OpenCV provides bitwise logical operation functions for images. We will have a look at the functions that provide the bitwise logical AND, OR, XOR (exclusive OR), and NOT (inversion) functionality. These functions can be better demonstrated visually with grayscale images. I am going to use barcode images in horizontal and vertical orientation for demonstration. Let's have a look at the following code:
A colorspace is a mathematical model used to represent colors. Usually, colorspaces are used to represent the colors in a numerical form and to perform mathematical and logical operations with them. In this book, the colorspaces we mostly use are BGR (OpenCV's default colorspace), RGB, HSV, and grayscale. BGR stands for blue, green, and red. HSV represents colors in Hue, Saturation, and Value format. OpenCV has a function cv2.cvtColor(img,conv_flag) that allows us to change the colorspace of an image (img), while the source and target colorspaces are indicated on the conv_flag parameter.
If you remember, in Chapter 2, Working with Images, Webcams, and GUI, we discovered that OpenCV loads images in BGR format and matplotlib uses the RGB format for images. So, before displaying an image with matplotlib, we need to convert an image from BGR to RGB colorspace.
Take a look at the following code. The program reads the image in color mode using cv2.imread(), which imports the image in the BGR colorspace. Then, it converts it to RGB using cv2.cvtColor(), and finally, it uses matplotlib to display the image:
Let's study a real-life application of this concept. In HSV format, it's much easier to recognize the color range. If we need to track a specific color object, we will have to define a color range in HSV, then convert the captured image in the HSV format, and then check whether the part of that image falls within the HSV color range of our interest. We can use the cv2.inRange() function to achieve this. This function takes an image, the upper and lower bounds of the colors, and then checks the range criteria for each pixel. If the pixel value falls in the given color range, the corresponding pixel in the output image is 0; otherwise it is 255, thus creating a binary mask.
We can use bitwise_and() to extract the color range we're interested in using this binary mask thereafter. Take a look at the following code to understand this concept: