[Python In-depth] Image handling in Python with OpenCV (2)

A Ydobon
4 min readOct 13, 2019

--

Photo by Hybrid on Unsplash

Before, we have checked how to read image data and split that into three different RGB channels. RGB is the most widely used and popular image color channel but, there are some others too. Now, we will use HSV channel to detect pink color in our image. HSV stands for Hue, Saturation, and Value.

Let’s import dependencies and read an image.

import numpy as npimport matplotlib.pyplot as pltimport cv2

> OpenCV is imported under the name ‘cv2’ in the very last line.

ball = cv2.imread(‘./image_file_path.jpg’)tongue = cv2.cvtColor(ball, cv2.COLOR_BGR2RGB)

> By cv2.imread function, we read an image, and save it under the name ‘ball’. After that, we transform the image into RGB color channel.

OpenCV reads data in BGR way, so if an image has red background in RGB color channel image, then after reading it through ‘cv2’, then the background would be in blue color. Therefore, we transform the BGR channeled image into RGB way using cv2 innate function ‘cv2.COLOR_BGR2RGB’

In prior posting, [Python In-depth] Image handling in Python with OpenCV (1), we have studied how to split RGB channel of an image by using cv2 innate functions. We have used a tiger image, and the RGB-split image is like below.

RGB split image (Red-Green-Blue)

Now, we will convert the RGB to HSV(Hue, Saturation, and Value) channel using a few lines of codes.

# convert from RGB to HSVhsv = cv2.cvtColor(tongue, cv2.COLOR_RGB2HSV)h = hsv[:,:,0]s = hsv[:,:,1]v = hsv[:,:,2]

> To deploy HSV channels, we used cv2.COLOR_RGB2HSV function. To detach the hue channel from the HSV, we put 0 in the the third dimension, and to detach the saturation, the value should be 1, and for the value channel, the value should be 2. Very similar to the way we split RGB before in the previous posting.

Now, using the matplotlib library, let’s check the three images which have been channeled through three distinctive hue, saturation, and value channel.

f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize = (20,10))ax1.set_title(‘Hue’)ax1.imshow(h, cmap = ‘gray’)ax2.set_title(‘Saturation’)ax2.imshow(s, cmap = ‘gray’)ax3.set_title(‘Value’)ax3.imshow(v, cmap = ‘gray’)

> Above is the code to show the three images.

HSV channeled image (Hue-Saturation-Value)

We can easily see the difference between RGB channeled one and the HSV channeled.

The reason why we deployed HSV channels rather than RGB, is that each different channel is useful in different ways.

Out of the tiger image, the pink is in the tongue. Let’s see which channel out of RGB and HSV is more helpful to detect the pink tongue of the tiger.

# 1. define our color selection criteria in RGB valuelower_pink = np.array([180,0,100])upper_pink = np.array([255,255,230])# 2. mask the imagemask_rgb = cv2.inRange(tongue, lower_pink, upper_pink)masked_image = np.copy(tongue)masked_image[mask_rgb==0] = [0,0,0]

> First of all, we will set a color selection criteria in RGB value. We set a lower and upper value so that the color range or span would be bounded. After that, using the bounded color range, we mask the other area with black, which is denoted as [0,0,0].

The outcome is below.

RGB — pink detection

Now, let’s see how much the HSV filter works better in this task.

# 1. define our color selection criteria in HSV valuelower_hue = np.array([160,0,0])upper_hue = np.array([180, 255,255])# mask the image using HSVmask_hsv = cv2.inRange(hsv, lower_hue, upper_hue)masked_image = np.copy(tongue)masked_image[mask_hsv==0] = [0,0,0]

> The color criteria value has been changed since the Hue channel of HSV, the maximum is 160, unlike the other channels whose maximum value is 255. Just like the prior process, we mask outside the range area with the black color. And the final result is below.

HSV — pink detection

We can clearly and easily see that HSV filter has far more successful in detecting the pink tongue of the tiger.

As always, we welcome your comments and feedback!

Thank you :) !

--

--

A Ydobon
A Ydobon

No responses yet