Image Segmentation Techniques in Python with OpenCV

Image segmentation is a crucial step in computer vision and image processing that involves dividing an image into multiple segments or regions, each containing similar features. This process helps in simplifying the image data and extracting relevant information. In this article, we will explore various image segmentation techniques using the OpenCV library in Python.

Table of Contents

  1. Introduction to OpenCV
  2. Thresholding
  3. Watershed Algorithm
  4. GrabCut Algorithm
  5. Conclusion

Introduction to OpenCV

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It is written in C++ and has a Python interface, making it a popular choice for image processing tasks. To get started with OpenCV, you can install it using pip:

pip install opencv-python

Once installed, you can import the library using:

import cv2


Thresholding is a simple image segmentation method that converts an image into a binary format based on a predefined threshold value. It is particularly useful for images with high contrast between the foreground and background.

Simple Thresholding

In simple thresholding, a global threshold value is used to convert an image into a binary format. Here's an example using OpenCV:

import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread('image.jpg', 0)
_, thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
_, thresh2 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV)

titles = ['Original Image', 'Binary', 'Binary Inverted']
images = [image, thresh1, thresh2]

for i in range(3):
    plt.subplot(1, 3, i+1)
    plt.imshow(images[i], 'gray')
    plt.xticks([]), plt.yticks([])

Adaptive Thresholding

Adaptive thresholding is a more sophisticated technique that calculates a threshold value for smaller regions within the image. This method is suitable for images with varying illumination conditions.

adaptive_thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

Otsu's Thresholding

Otsu's thresholding is an automatic method that calculates an optimal threshold value based on the image histogram.

_, otsu_thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

Watershed Algorithm

The watershed algorithm is a more advanced image segmentation method that treats the image as a topographical surface, where the intensity of each pixel represents elevation. The algorithm finds segment boundaries by simulating the flooding of the surface from the lowest points.

import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
sure_bg = cv2.dilate(opening, kernel, iterations=3)

dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
_, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0)

sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg, sure_fg)

_, markers = cv2.connectedComponents(sure_fg)
markers = markers + 1
markers[unknown == 255] = 0

markers = cv2.watershed(image, markers)
image[markers == -1] = [0, 0, 255]

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

GrabCut Algorithm

The GrabCut algorithm is an interactive image segmentation technique that uses a combination of graph cuts and border matting. This method requires the user to define a bounding box around the object of interest.

import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread('image.jpg')
mask = np.zeros(image.shape[:2], np.uint8)

bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)

rect = (50, 50, 450, 290)  # Define the bounding box
cv2.grabCut(image, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)

mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
image = image * mask2[:, :, np.newaxis]

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))


In this article, we discussed various image segmentation techniques using Python's OpenCV library, including thresholding, watershed, and GrabCut algorithms. These methods are commonly used in computer vision and image processing applications to simplify image data and extract relevant information.

An AI coworker, not just a copilot

View VelocityAI