AppDividend
Latest Code Tutorials

Python cv2: Understand Image Types and Color Channels

0

Using the Python-OpenCV module, you can transform the image from color to black-white, from black-white to gray, or from RGB to Hue Saturation and Value. Understand Image types and color channels are essential when working with the cv2 module in Python.

We can think of Images in Python are numpy arrays, and using the cv2 module, we can modify the arrays and transform the images into various forms. Let’s deep dive into this topic.

Understand Image Types and Color Channels

To understand image types and color channels, we need to split the original image into three channels. B, G, R. Then, we display the color channels one by one to analyze the images.

First, we will create an empty array of the same original image and then fill the b, g, r color channels to each matrix to create the different versions of the image with their color channels.

To continue, you have to install numpy and opencv-python library in your machine.

Let’s import the numpy and cv2 library.

import numpy as np
import cv2

Now, read a new image using cv2.imread() method. I will use free images available on the Internet.

import numpy as np
import cv2

imgColor = cv2.imread('data.png', 1)
cv2.imshow("Image", imgColor)

Now, we are explicitly set the Window to cv2.moveWindow() with the same image name “Image” and 0, 0. That means, when the cv2 generates an image, it will go to the left corner.

# app.py

import numpy as np
import cv2

imgColor = cv2.imread('data.png', 1)
cv2.imshow("Image", imgColor)
cv2.moveWindow("Image", 0, 0)
print(imgColor.shape)

cv2.waitKey(0)
cv2.destroyAllWindows()

If you run the above file using python3 app.py command, you will get the following output. 

➜  pyt python3 app.py
(271, 529, 3)

 

Understand Image Types and Color Channels

From the output, you can see that the shape of the image is (271, 529, 3). Meaning height is 271, width is 529, and the channel is 3.

Divide the Image Shape

To divide the shape into height, width, and channel, write the following code.

# app.py

height, width, channels = imgColor.shape

We need this step because now we will create an empty numpy array and use these dimensions to construct the arrays.

Split the image into B, G, and R

Python cv2.split() method splits the image into B, G, andcomponents. We will create three different images based on these channels by constructing the arrays.

# app.py

import cv2

imgColor = cv2.imread('data.png', 1)
cv2.imshow("Image", imgColor)
cv2.moveWindow("Image", 0, 0)
print(imgColor.shape)
height, width, channels = imgColor.shape

b, g, r = cv2.split(imgColor)
rgb_split = np.empty([height, width * 3, 3], 'uint8')
rgb_split[:, 0:width] = cv2.merge([b, b, b])
rgb_split[:, width: width * 2] = cv2.merge([g, g, g])
rgb_split[:, width * 2 : width * 3] = cv2.merge([r, r, r])

cv2.waitKey(0)
cv2.destroyAllWindows()

In this code, we have split the image into b, g, r components.

Then created an empty array of the same height and 3x width and this array only contains uint data type values. This array only accepts the pixel values between 0 to 255 because of uint8.

rgb_split[:, 0:width] = cv2.merge([b, b, b]) 
rgb_split[:, width: width * 2] = cv2.merge([g, g, g]) 
rgb_split[:, width * 2 : width * 3] = cv2.merge([r, r, r])

In the above code, we are filling the b, g, r values in the empty array to create three different versions of the image with respective channels.

In the final step, we need to display the image channels. So our final code looks like below.

# app.py

import numpy as np
import cv2

imgColor = cv2.imread('data.png', 1)
cv2.imshow("Image", imgColor)
cv2.moveWindow("Image", 0, 0)
print(imgColor.shape)
height, width, channels = imgColor.shape

b, g, r = cv2.split(imgColor)
rgb_split = np.empty([height, width * 3, 3], 'uint8')
rgb_split[:, 0:width] = cv2.merge([b, b, b])
rgb_split[:, width: width * 2] = cv2.merge([g, g, g])
rgb_split[:, width * 2: width * 3] = cv2.merge([r, r, r])

cv2.imshow("Channels", rgb_split)
cv2.moveWindow("Channels", 0, height)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

 

Python cv2 - Understand Image Types and Color Channels

You can see that first is the original image and then below that there three different channels of that image. So, if you want to split the image into other channels, then use cv2.split() method and assign different components to a new array and merge it to construct the channels.

That is it for Image types and Channels in Python cv2.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.