Implementation of Image Thresholding in Python: Full Code in Python

Implementation of Image Thresholding in Python: Full Code in Python
Implementation of Image Thresholding in Python: Full Code in Python

What is Image Thresholding?
  1. Image Thresholding is a method of image segmentation.
  2. It is used to create binary images from grayscale images.
  3. It is used to divide the image into smaller segments.
  4. To define the boundary it uses only one color or grayscale value.
  5. It reduces the complexity of the data.
  6. It makes the process of image recognition and classification simpler.
Code:


import cv2

import numpy as np

image1 = cv2.imread('image1.png')

img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)

ret, thresh2 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY_INV)

ret, thresh3 = cv2.threshold(img, 120, 255, cv2.THRESH_TRUNC)

ret, thresh4 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO)

ret, thresh5 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO_INV)

cv2.imshow('Binary Threshold', thresh1)

cv2.imshow('Binary Threshold Inverted', thresh2)

cv2.imshow('Truncated Threshold', thresh3)

cv2.imshow('Set to 0', thresh4)

cv2.imshow('Set to 0 Inverted', thresh5)

if cv2.waitKey(0) & 0xff == 27:

cv2.destroyAllWindows()

Applications:
  1. Face detection
  2. Analyze and recognize fingerprints.
  3. Motion Detection
  4. Content recognizing.
Advantages:
  1. It reduces the complexity of the data.
  2. It makes the process of image recognition and classification simpler.
Disadvantages:
  1. Due to image thresholding information is lost.

Post a Comment

0 Comments