Segmenting an image: An Image Processing Introduction

Image segmentation is the process of partitioning an image into several segments or extracting a specific part or information of an image. For this article, I will discuss how we would be able to segment the image using two kinds of color segmentation: RGB and HSV, and also via image differentiation.

Color segmentation

It is possible to segment images based on their color via RGB and HSV. Depending on which segmentation technique you will use, you will be able to extract a specific segment of the image as long as it is colored. Let’s use this colored balls for this one:

RGB balls

Via RGB color space

Let’s try extracting the blue ball via the RGB color space using the following code:

fig, ax = plt.subplots(1, 3, figsize=(12,4))
ax[0].imshow(balls[:,:,0], cmap=’Reds’)
ax[1].imshow(balls[:,:,1], cmap=’Greens’)
ax[2].imshow(balls[:,:,2], cmap=’Blues’)
Image showing each of the ball with most of the colors
from skimage.filters import threshold_otsu
balls_gray = rgb2gray(balls[:,:,0] < 100) & (balls[:,:,1] < 100) & (balls[:,:,2] > 100)
thresh = threshold_otsu(balls_gray)
balls_binary_otsu2 = balls_gray < thresh
Segmented blue ball in grayscale

After identifying the RGB color space values of the image, I used Otsu’s thresholding technique in order to fully segment the blue ball from the rest of the image based from the results above. The downside of using RGB color space is that if there are other colors that we need to segment, we need to find the correct RGB color channel values of that specific color in order to fully segment it.

HSV color space

However, it is possible to extract those colors using HSV color space. We would be able to extract other colors much easier if we will use this technique.

from skimage.color import rgb2hsv
balls_hsv = rgb2hsv(balls)
fig, ax = plt.subplots(1, 3, figsize=(12,4))
ax[0].imshow(balls_hsv[:,:,0], cmap=’gray’)
ax[1].imshow(balls_hsv[:,:,1], cmap=’gray’)
ax[2].imshow(balls_hsv[:,:,2], cmap=’gray’)
HSV color channel values
Hue color channel
lower_mask = balls_hsv[:,:,0] > 0.60
upper_mask = balls_hsv[:,:,0] < 0.70
saturation = balls_hsv[:,:,1] > 0.5
mask = upper_mask * lower_mask * saturation
red = balls[:,:,0]*mask
green = balls[:,:,1]*mask
blue = balls[:,:,2]*mask
balls_masked = np.dstack((red,green,blue))
Segmented blue ball

The implementation of segmenting the blue ball via HSV color space is a bit straightforward. Rather than using Otsu’s threshold, what we did here is get the lower mask and upper mask of the hue, and saturation value to show only the blue ball and mask all other parts of the image.

Image differencing

Image differencing is one way of knowing what have changed in the image. What we do here is find the differences between the two images. This is a “good way” when solving “find the difference” puzzles. Let’s use these images for this one:

Sample find the difference image

Some of you may have probably found the differences between the two image, but I want to “cheat” my way onto it:

from skimage.color import rgb2grayimage1 = imread('spotdiff1.jpg')
image2 = imread('spotdiff2.jpg')
image1_gray = rgb2gray(image1)
image2_gray = rgb2gray(image2)
diff = image1_gray - image2_gray
Results of the difference between the two images

Using the implementation above, we were able to highlight the difference between the two images which are shown blue and red.

There you have it! That’s how we would be able to segment the images using color spaces and image differencing.

A developer by day, a programmer by night

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store