import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread("INDIA.jpg")
if image is None:
print("Error: Image not found.")
exit()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(
gray, 127, 255, cv2.THRESH_BINARY
)
kernel = np.ones((5, 5), np.uint8)
while True:
print("\n========== MORPHOLOGICAL OPERATIONS===========")
print("1. Display Original Image")
print("2. Display Binary Image")
print("3. Erosion")
print("4. Dilation")
print("5. Display Erosion and Dilation")
print("6. Exit")
choice = input("Enter your choice: ")
if choice == "1":
plt.imshow(
cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
)
plt.title("Original Image")
plt.axis("off")
plt.show()
elif choice == "2":
plt.imshow(binary, cmap="gray")
plt.title("Binary Image")
plt.axis("off")
plt.show()
elif choice == "3":
erosion = cv2.erode(
binary,
kernel,
iterations=1
)
print("\nDisplaying Eroded Image...")
plt.imshow(erosion, cmap="gray")
plt.title("Erosion")
plt.axis("off")
plt.show()
elif choice == "4":
dilation = cv2.dilate(
binary,
kernel,
iterations=1
)
print("\nDisplaying Dilated Image...")
plt.imshow(dilation, cmap="gray")
plt.title("Dilation")
plt.axis("off")
plt.show()
elif choice == "5":
erosion=cv2.erode(
binary,
kernel,
iterations=1)
delation=cv2.dilate(
binary,kernel,iterations=1)
dilation=cv2.dilate(
binary,
kernel,
iterations=1)
plt.figure(figsize=(12,4))
plt.subplot(1,3,1)
plt.imshow(binary,cmap="gray")
plt.title("binary image")
plt.axis("off")
plt.subplot(1,3,2)
plt.imshow(binary,cmap="gray")
plt.title("erosion")
plt.axis("off")
plt.subplot(1,3,3)
plt.imshow(binary,cmap="gray")
plt.title("dilation")
plt.axis("off")
plt.show()
elif choice == "6":
print("\nProgram Terminated.")
break
else:
print("\nInvalid choice. Please try again.")⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.