To segment the image using global thresholding, adaptive thresholding and Otsu's thresholding using python and OpenCV.
- Anaconda - Python 3.7
- OpenCV
-
Step1: Load the necessary packages.
-
Step2: Read the Image and convert to grayscale.
-
Step3: Use Global thresholding to segment the image.
-
Step4: Use Adaptive thresholding to segment the image.
-
Step5: Use Otsu's method to segment the image and display the results.
NAME : AADHITHYA.M
REG : 212222100001
# Load the necessary packages
import numpy as np
import matplotlib.pyplot as plt
import cv2
# Read the Image and convert to grayscale
image = cv2.imread('flight.jpg',1)
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
image_gray = cv2.imread('flight.jpg',0)
# Use Global thresholding to segment the image
ret,thresh_img1=cv2.threshold(image_gray,86,255,cv2.THRESH_BINARY)
ret,thresh_img2=cv2.threshold(image_gray,86,255,cv2.THRESH_BINARY_INV)
ret,thresh_img3=cv2.threshold(image_gray,86,255,cv2.THRESH_TOZERO)
ret,thresh_img4=cv2.threshold(image_gray,86,255,cv2.THRESH_TOZERO_INV)
ret,thresh_img5=cv2.threshold(image_gray,100,255,cv2.THRESH_TRUNC)
# Use Adaptive thresholding to segment the image
thresh_img7=cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
thresh_img8=cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
# Use Otsu's method to segment the image
ret,thresh_img6=cv2.threshold(image_gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Display the results
titles=["Gray Image","Threshold Image (Binary)","Threshold Image (Binary Inverse)","Threshold Image (To Zero)"
,"Threshold Image (To Zero-Inverse)","Threshold Image (Truncate)","Otsu","Adaptive Threshold (Mean)","Adaptive Threshold (Gaussian)"]
images=[image_gray,thresh_img1,thresh_img2,thresh_img3,thresh_img4,thresh_img5,thresh_img6,thresh_img7,thresh_img8]
for i in range(0,9):
plt.figure(figsize=(10,10))
plt.subplot(1,2,1)
plt.title("Original Image")
plt.imshow(image)
plt.axis("off")
plt.subplot(1,2,2)
plt.title(titles[i])
plt.imshow(cv2.cvtColor(images[i],cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.show()
![](https://private-user-images.githubusercontent.com/119393540/326330917-39a8caf5-b2c7-44a8-b142-984e3b4c26d4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUzMTkyODgsIm5iZiI6MTcyNTMxODk4OCwicGF0aCI6Ii8xMTkzOTM1NDAvMzI2MzMwOTE3LTM5YThjYWY1LWIyYzctNDRhOC1iMTQyLTk4NGUzYjRjMjZkNC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQyMzE2MjhaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT02ZGY0M2I0NzFjNmVmZmRmOTg0ZTFjMDM5ODJmYzFlODBiNjI0YjE0YWQ5NTg1NTQ2NjNjMGJlOWM0YWQ2MWE2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.z9nL_4LQqSIR8fEMKe6JgeunbtPgzdRwn7DySWOC51g)
![](https://private-user-images.githubusercontent.com/119393540/326331166-2e725760-03d4-465b-a28e-34616a283309.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUzMTkyODgsIm5iZiI6MTcyNTMxODk4OCwicGF0aCI6Ii8xMTkzOTM1NDAvMzI2MzMxMTY2LTJlNzI1NzYwLTAzZDQtNDY1Yi1hMjhlLTM0NjE2YTI4MzMwOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQyMzE2MjhaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xOThkMGE4ZjYwNWQwMTlmMmYxNDQ3Yzg3M2I0NmFhOWZhNjM2OGY0MDkzZTFhMzMzOTBkZjZmOGVjOWJhM2YzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.zVXB2Ertz38ZwxcQq-NyM20N1tk8SoopSdyK0byd9Tw)
Thus the images are segmented using global thresholding, adaptive thresholding and optimum global thresholding using python and OpenCV.