Sitemap

BEHIND THE SCENES OF IMAGE PROCESSING (5 OF 9)

Image Processing using Python — Image Segmentation

5 min readJun 17, 2023

--

Press enter or click to view image in full size

In this article, we embark on an exciting journey through the realm of image segmentation as we delve into the implementation of this powerful technique on a captivating image I captured during a thrilling parasailing adventure. Our objective is to demonstrate how image segmentation can be employed to extract meaningful regions from an image, unraveling the story behind the snapshot and unlocking hidden insights.

The Essence of Image Segmentation

Image segmentation serves as a fundamental tool in computer vision and image analysis, enabling us to partition an image into distinct regions based on specific characteristics or attributes. By identifying and isolating meaningful regions, we gain valuable information about the objects or subjects within the image, facilitating further analysis, object recognition, and understanding of the visual content.

To begin, let us import all necessary libraries.

import matplotlib.pyplot as plt
import numpy as np

from skimage.io import imread, imshow
from skimage.color import rgb2hsv
from skimage.exposure import histogram, cumulative_distribution
from skimage.filters import threshold_otsu

Now, let us see the image!

sail= imread('sail.png')
imshow(sail)
Press enter or click to view image in full size

Implementing Image Segmentation: Unveiling the Story within the Snapshot

To showcase the power of image segmentation, we will apply this technique to an exhilarating photograph captured during a parasailing experience. By following a series of steps, including preprocessing, feature extraction, and region grouping, we can effectively separate the different elements present in the image. From the sky and the parachute to the person in mid-air, image segmentation allows us to uncover the individual components that make up the fascinating scene.

Let us grayscale for easier thresholding.

sail_gray = rgb2gray(sail)
imshow(sail_gray)
Press enter or click to view image in full size
th = 0.4
sail_gray_bw = sail_gray<th
imshow(sail_gray_bw)
Press enter or click to view image in full size

Let’s implement Otsu’s method to determine if our manual thresholding is sufficiently accurate.

Press enter or click to view image in full size

Indeed, the manual thresholding I implemented closely approximates the desired outcome.

The Significance of Image Segmentation in HSV Color Space

Get Francis Camarao’s stories in your inbox

Join Medium for free to get updates from this writer.

Understanding the significance of image segmentation in HSV color space expands our possibilities for visual analysis. By isolating regions based on color characteristics, we can extract meaningful information, detect objects, and facilitate further analysis or applications. The application of image segmentation in HSV color space extends beyond recreational photography, finding utility in various fields like object recognition, image-based sorting, and even medical imaging.

sail_hsv = rgb2hsv(sail)

fig, ax = plt.subplots(1, 3, figsize=(12,4))
ax[0].imshow(sail_hsv[:,:,0], cmap='gray')
ax[0].set_title('Hue')
ax[1].imshow(sail_hsv[:,:,1], cmap='gray')
ax[1].set_title('Saturation')
ax[2].imshow(sail_hsv[:,:,2], cmap='gray')
ax[2].set_title('Value');
Press enter or click to view image in full size
plt.imshow(sail_hsv[:,:,0],cmap='hsv')
plt.colorbar()

Let us now try to threshold the image using the Hue par of the HSV color channel to segment the image.

Red

lower_mask = sail_hsv[:,:,0] > 0.90
upper_mask = sail_hsv[:,:,0] < 1
mask = upper_mask*lower_mask
plt.imshow(mask)
red = sail[:,:,0]*mask
green = sail[:,:,1]*mask
blue = sail[:,:,2]*mask
sail_masked = np.dstack((red,green,blue))
imshow(sail_masked)
Press enter or click to view image in full size

Yellow

lower_mask = sail_hsv[:,:,0] > 0.08
upper_mask = sail_hsv[:,:,0] < 0.19
mask = upper_mask*lower_mask
plt.imshow(mask)

red = sail[:,:,0]*mask
green = sail[:,:,1]*mask
blue = sail[:,:,2]*mask
sail_masked = np.dstack((red,green,blue))
imshow(sail_masked)
Press enter or click to view image in full size

Blue

lower_mask = sail_hsv[:,:,0] > 0.58
upper_mask = sail_hsv[:,:,0] < 0.7
mask = upper_mask*lower_mask
plt.imshow(mask)

red = sail[:,:,0]*mask
green = sail[:,:,1]*mask
blue = sail[:,:,2]*mask
sail_masked = np.dstack((red,green,blue))
imshow(sail_masked)
Press enter or click to view image in full size
import matplotlib.pyplot as plt

fig, axes = plt.subplots(1, 3, figsize=(12, 4))

# First subplot
lower_mask = sail_hsv[:,:,0] > 0.90
upper_mask = sail_hsv[:,:,0] < 1
mask = upper_mask * lower_mask
axes[0].imshow(mask)
axes[0].set_title("Mask 1")

red = sail[:,:,0] * mask
green = sail[:,:,1] * mask
blue = sail[:,:,2] * mask
sail_masked = np.dstack((red, green, blue))
axes[0].imshow(sail_masked)
axes[0].set_title("Red Masked 1")

# Second subplot
lower_mask = sail_hsv[:,:,0] > 0.08
upper_mask = sail_hsv[:,:,0] < 0.19
mask = upper_mask * lower_mask
axes[1].imshow(mask)
axes[1].set_title("Mask 2")

red = sail[:,:,0] * mask
green = sail[:,:,1] * mask
blue = sail[:,:,2] * mask
sail_masked = np.dstack((red, green, blue))
axes[1].imshow(sail_masked)
axes[1].set_title("Yellow Masked 2")

# Third subplot
lower_mask = sail_hsv[:,:,0] > 0.58
upper_mask = sail_hsv[:,:,0] < 0.7
mask = upper_mask * lower_mask
axes[2].imshow(mask)
axes[2].set_title("Mask 3")

red = sail[:,:,0] * mask
green = sail[:,:,1] * mask
blue = sail[:,:,2] * mask
sail_masked = np.dstack((red, green, blue))
axes[2].imshow(sail_masked)
axes[2].set_title("Blue Masked 3")

# Adjust spacing and display the plot
plt.tight_layout()
plt.show()
Press enter or click to view image in full size

Finally, by utilizing the HSV color space, we have successfully segmented the image.

Closing Thoughts

Through the implementation of image segmentation in HSV color space on a captivating parasailing snapshot, we have witnessed the transformative capabilities of this technique. By leveraging the unique properties of HSV color space, we can accurately extract meaningful regions, unravel the story captured within the image, and gain deeper insights into its content. The power of image segmentation in HSV color space lies in its ability to enhance visual analysis, enabling us to extract valuable information and facilitate a wide range of applications. So, let’s embrace the magic of image segmentation in HSV color space and embark on a journey of exploration and discovery, uncovering the hidden tales within our treasured snapshots.

References

Benjur Emmanuel L. Borja. 2023. Image Processing(MSDS2023). Asian Institute of Management.

Did you like this article?

Feel free to connect with me on LinkedIn. Let’s establish a professional network and stay in touch. Additionally, you can explore my GitHub profile for the complete implementation of this project and discover more fascinating projects I have worked on.

--

--

Francis Camarao
Francis Camarao

Written by Francis Camarao

a Data Scientist who can't write