Thresholding and Filtering: A Powerful Tool for Image Processing

Thresholding and Filtering: A Powerful Tool for Image Processing

Thresholding and Filtering to Segment Images and Identify Regions of Interest in OpenCV

ยท

3 min read

Image thresholding and filtering are two of the most basic image processing techniques. Thresholding is used to convert an image into a binary image, where pixels are either black or white. Filtering is used to smooth or sharpen an image, or to remove noise.

Thresholding and filtering can be used to segment images and identify regions of interest. For example, you can use thresholding to segment an image of a person by thresholding on the skin colour. You can then use filtering to smooth the image and remove noise. This will help you to identify the person's face and other features.

In this blog, we will discuss how to use thresholding and filtering to segment images and identify regions of interest in OpenCV.

Thresholding

Thresholding is a simple image processing technique that can be used to convert an image into a binary image. In a binary image, pixels are either black or white. The threshold value is used to determine whether a pixel is black or white.

There are two types of thresholding:

  • Binary thresholding: This is the simplest type of thresholding. In binary thresholding, all pixels with values above the threshold are set to white, and all pixels with values below the threshold are set to black.

  • Multi-level thresholding: This type of thresholding allows you to set multiple thresholds. Pixels with values above the first threshold are set to white, pixels with values between the first and second thresholds are set to gray , and pixels with values below the second threshold are set to black.

Filtering

Filtering is another simple image-processing technique that can be used to smooth or sharpen an image, or to remove noise. There are many different types of filters, but the most common types are:

  • Gaussian blur: This filter is used to smooth an image by averaging the values of neighbouring pixels.

  • Median blur: This filter is used to remove noise from an image by replacing each pixel with the median value of its neighbouring pixels.

  • Bilateral filter: This filter is a combination of a Gaussian blur and a median blur. It is used to smooth an image while preserving edges.

Implementation

To implement thresholding and filtering in OpenCV, you can use the following functions:

  • cv2.threshold(): This function is used to perform binary thresholding.

  • cv2.adaptiveThreshold(): This function is used to perform adaptive thresholding.

  • cv2.GaussianBlur(): This function is used to perform a Gaussian blur.

  • cv2.medianBlur(): This function is used to perform a median blur.

  • cv2.bilateralFilter(): This function is used to perform a bilateral filter.

Example

The following code shows how to use thresholding and filtering to segment an image of a person (you can use your img here):

Python

import cv2

# Load the image
image = cv2.imread("your_image.jpg")

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

# Apply a Gaussian blur to the image
blurred_image = cv2.GaussianBlur(grayscale_image, (5, 5), 0)

# Apply a threshold to the image
thresholded_image = cv2.threshold(blurred_image, 127, 255, cv2.THRESH_BINARY)[1]

# Display the images
cv2.imshow("Original Image", image)
cv2.imshow("Grayscale Image", grayscale_image)
cv2.imshow("Blurred Image", blurred_image)
cv2.imshow("Thresholded Image", thresholded_image)

# Wait for a key to be pressed
cv2.waitKey(0)

# Close all windows
cv2.destroyAllWindows()

This code will display the original image, the grayscale image, the blurred image, and the thresholded image. The thresholded image will show the person's face and other features.

Conclusion

Thresholding and filtering are two of the most basic image processing techniques. They can be used to segment images and identify regions of interest. In this blog, we discussed how to use thresholding and filtering to segment images and identify regions of interest in OpenCV.

Catch me up on my socials: https://bento.me/harshitpy and I will meet you in the next one ๐Ÿ‘‹

Thank you so much for reading ๐Ÿ’–

You can support me here BuyMeACoffee

Did you find this article valuable?

Support Harshit Jain by becoming a sponsor. Any amount is appreciated!

ย