Welcome back to our computer vision blog series! The fundamentals of image processing, feature extraction, and using OpenCV to work with images and videos have all been covered previously. We’ll go deeper into more complex OpenCV image processing methods today. These methods are crucial for improving pictures, drawing out important details, and getting pictures ready for additional examination.

This blog post will discuss:

In this blog, we will cover:

  1. Color Mappings
  2. Blending and Pasting Images
  3. Image Thresholding
  4. Blurring and Smoothing
  5. Morphological Operators
  6. Gradients
  7. Histograms and Histogram Equalization

Color Mappings

The process of altering an image’s colours using a predetermined mapping function is known as colour mapping. OpenCV offers multiple colour mapping techniques. This is an illustration using the colormap.

import cv2
import numpy as np

# Read the image
image = cv2.imread('path_to_your_image.jpg')

# Apply a colormap
colormap = cv2.applyColorMap(image, cv2.COLORMAP_JET)

# Display the result
cv2.imshow('Colormap', colormap)
cv2.waitKey(0)
cv2.destroyAllWindows()

Blending and Pasting Images

Combining two images is known as “blending and pasting.” Using this to create composite images may be beneficial. This is how two images can be blended:

# Read the images
image1 = cv2.imread('path_to_your_first_image.jpg')
image2 = cv2.imread('path_to_your_second_image.jpg')

# Resize the images to the same size
image1 = cv2.resize(image1, (500, 500))
image2 = cv2.resize(image2, (500, 500))

# Blend the images
blended = cv2.addWeighted(image1, 0.7, image2, 0.3, 0)

# Display the result
cv2.imshow('Blended Image', blended)
cv2.waitKey(0)
cv2.destroyAllWindows()

Blending and Pasting Images (Using Mask)

Blending can be controlled more precisely when masks are used. The areas of the image that should be blended are specified by masks:

# Create a mask
mask = np.zeros(image1.shape[:2], dtype="uint8")
cv2.circle(mask, (250, 250), 200, 255, -1)

# Apply the mask to the images
masked_image1 = cv2.bitwise_and(image1, image1, mask=mask)
masked_image2 = cv2.bitwise_and(image2, image2, mask=cv2.bitwise_not(mask))

# Combine the masked images
blended_with_mask = cv2.add(masked_image1, masked_image2)

# Display the result
cv2.imshow('Blended Image with Mask', blended_with_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

Image Thresholding

Thresholding is an image segmentation technique. It transforms binary images from grayscale images:

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply a binary threshold
_, thresholded = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

# Display the result
cv2.imshow('Thresholded Image', thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()

Blurring and Smoothing

Images are subjected to smoothing and blurring to minimise noise and enhance detail. Two popular methods are median blur and Gaussian blur:

# Apply Gaussian blur
gaussian_blur = cv2.GaussianBlur(image, (15, 15), 0)

# Display the result
cv2.imshow('Gaussian Blur', gaussian_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

Morphological Operators

Binary images are processed and image components are extracted using morphological operators. Dilation and erosion are examples of common operators:

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply a binary threshold
_, binary_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

# Apply dilation
dilated = cv2.dilate(binary_image, None, iterations=2)

# Apply erosion
eroded = cv2.erode(binary_image, None, iterations=2)

# Display the results
cv2.imshow('Dilated Image', dilated)
cv2.imshow('Eroded Image', eroded)
cv2.waitKey(0)
cv2.destroyAllWindows()

Gradients

Images’ edges can be found using gradients. The Sobel operator is a widely used technique to compute gradients:

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Sobel operator
sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5)
sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5)

# Display the results
cv2.imshow('Sobel X', sobelx)
cv2.imshow('Sobel Y', sobely)
cv2.waitKey(0)
cv2.destroyAllWindows()

Histograms

The distribution of pixel intensities in an image is shown by histograms. This is how a histogram is computed and shown:

from matplotlib import pyplot as plt

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Calculate the histogram
histogram = cv2.calcHist([gray_image], [0], None, [256], [0, 256])

# Plot the histogram
plt.plot(histogram)
plt.title('Grayscale Histogram')
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.show()

Histogram Equalization

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply histogram equalization
equalized_image = cv2.equalizeHist(gray_image)

# Display the result
cv2.imshow('Equalized Image', equalized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this blog, we looked at various OpenCV advanced image processing techniques, such as gradients, histograms, colour mappings, image thresholding, blurring and smoothing, morphological operations, and blending and pasting. These methods are essential for improving and evaluating pictures in a range of computer vision applications.

Watch this space for our upcoming blog post, where we’ll continue to explore more complex computer vision topics. Have fun with coding!

Categories: Uncategorized