5 Best Ways to Apply Top Hat and Black Hat Transform using OpenCV Python

πŸ’‘ Problem Formulation: In digital image processing, the top hat and black hat transforms are used for extracting small elements and details from images. Given an input image, possibly with uneven lighting or foreground clutter, the problem is to enhance or isolate elements such as white or black regions. Using Top Hat transform, we aim to highlight light elements on dark backgrounds. Conversely, Black Hat transform reveals dark elements on light backgrounds. The desired output is a processed image that makes subsequent analysis or feature recognition easier.

Method 1: Basic Top Hat Transform

This method involves applying the top hat transform which is effectively used to highlight bright regions that are smaller than the kernel size. In OpenCV, the cv2.morphologyEx() function is used, specifying the cv2.MORPH_TOPHAT argument. This can help in emphasizing features such as bright scratches or marks on a dark surface.

Here’s an example:

import cv2
import numpy as np

# Read the image
image = cv2.imread('input.jpg', 0)

# Define kernel size
kernel_size = 15
kernel = np.ones((kernel_size, kernel_size), np.uint8)

# Apply the top hat transform
top_hat = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel)

# Display the transformed image
cv2.imshow('Top Hat', top_hat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: An image displaying the bright features more prominently against the dark background.

In this code snippet, an image is first read in grayscale mode. A kernel is defined with the given size and shape, and then the top hat operation is performed using the morphologyEx function. The resulting image highlights bright areas smaller than the kernel, enhancing the visibility of light features on a dark surface.

Method 2: Basic Black Hat Transform

The black hat transform, which is the counterpart of the top hat transform, brings out dark features on light backgrounds. In OpenCV, this is also done using the cv2.morphologyEx() function but with cv2.MORPH_BLACKHAT. This technique is especially useful for cases like spotting dark stains or imperfections on bright materials.

Here’s an example:

import cv2
import numpy as np

# Read the image
image = cv2.imread('input.jpg', 0)

# Define kernel size
kernel_size = 15
kernel = np.ones((kernel_size, kernel_size), np.uint8)

# Apply the black hat transform
black_hat = cv2.morphologyEx(image, cv2.MORPH_BLACKHAT, kernel)

# Display the transformed image
cv2.imshow('Black Hat', black_hat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: An image that exhibits dark features on a light background distinctly.

Similarly to the top hat transform, the black hat operation uses a kernel to highlight dark regions that are smaller than the kernel. The image undergoes a transformation that makes darker elements within bright surroundings stand out, which can be useful in various image analysis tasks.

Method 3: Adaptive Kernel Size Top Hat Transform

Using an adaptive kernel size allows the top hat transform to be more flexible in highlighting features of varying sizes. This method involves programmatically adjusting the size of the kernel based on the content in the image, allowing for better feature extraction when the sizes of the bright regions are unknown or varying across the image.

Here’s an example:

import cv2
import numpy as np

# Read the image
image = cv2.imread('input.jpg', 0)

# Adaptive kernel size calculation (example logic)
mean_val = np.mean(image)
kernel_size = int(mean_val / 10) if mean_val / 10 > 1 else 1
kernel = np.ones((kernel_size, kernel_size), np.uint8)

# Apply the adaptive top hat transform
adaptive_top_hat = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel)

# Display the transformed image
cv2.imshow('Adaptive Top Hat', adaptive_top_hat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: An image where the bright features are enhanced while adapting to their sizes.

Here the kernel size is adaptively determined based on the average pixel value of the image, illustrating a simple method of adjusting the kernel size to the content. The adaptive top hat transformation is particularly useful when the scale of the features of interest varies within the image domain.

Method 4: Top Hat and Black Hat Combination

Combining top hat and black hat transforms can help in extracting both bright and dark features within the same image. This is useful in scenarios where you need to analyze both kinds of features simultaneously, like extracting both scratches and marks from quality control images.

Here’s an example:

import cv2
import numpy as np

# Read the image
image = cv2.imread('input.jpg', 0)

# Define kernel size
kernel_size = 15
kernel = np.ones((kernel_size, kernel_size), np.uint8)

# Apply the top hat transform
top_hat = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel)

# Apply the black hat transform
black_hat = cv2.morphologyEx(image, cv2.MORPH_BLACKHAT, kernel)

# Combine the top hat and black hat images
combined_transform = cv2.add(top_hat, black_hat)

# Display the combined transformed image
cv2.imshow('Combined Top Hat and Black Hat', combined_transform)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: An image where both bright and dark features are highlighted simultaneously.

The code performs both top hat and black hat transforms on the same image and then combines the results. The final image is enhanced in a way that both bright and dark small-scale features are distinctly visible, which makes it very useful for comprehensive feature extraction.

Bonus One-Liner Method 5: Inline Top Hat Transform

For a quick and concise inline top hat transformation, a one-liner code can be used. This method is beneficial for seasoned programmers looking to implement this transform with minimal code.

Here’s an example:

cv2.imshow('Inline Top Hat', cv2.morphologyEx(cv2.imread('input.jpg', 0), cv2.MORPH_TOPHAT, np.ones((15, 15), np.uint8))); cv2.waitKey(0)

Output: A single line of code that reads an image, applies the top hat transform, and displays the result.

This code snippet performs the entire operation of reading, transforming, and displaying the result in an image viewer in just one line. It’s a compact version for quick implementation without additional variable definitions or steps.

Summary/Discussion

  • Method 1: Basic Top Hat Transform. Strengths: Simple and effective for highlighting bright features against dark backgrounds. Weaknesses: Not adaptable to varying feature sizes.
  • Method 2: Basic Black Hat Transform. Strengths: Effective for enhancing dark features on light backgrounds. Weaknesses: Same limitations as Method 1 with regards to feature size adaptability.
  • Method 3: Adaptive Kernel Size Top Hat Transform. Strengths: Allows for flexibility in handling various feature sizes within an image. Weaknesses: May require additional logic to calculate the optimal kernel size effectively.
  • Method 4: Top Hat and Black Hat Combination. Strengths: Simultaneously extracts both bright and dark features. Weaknesses: The final image can become cluttered if not used carefully.
  • Bonus Method 5: Inline Top Hat Transform. Strengths: Extremely concise for quick usage. Weaknesses: Reduced readability and flexibility for adjustments.