To implement Opening and Closing using Python and OpenCV.
- Anaconda - Python 3.7
- OpenCV
Import the necessary packages
Create the Text using cv2.putText
Create the structuring element
Use Opening operation
Use Closing Operation
Developed by : GOKULARAMANAN K
Register no : 212222230040
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Read the color image
input_image_path = 'kitty.jpg'
color_image = cv2.imread(input_image_path)
# Convert the color image to grayscale
gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
# Perform edge detection using Canny
edges = cv2.Canny(gray_image, 100, 200) # you can adjust the thresholds as needed
# Define the kernel size for erosion and dilation
kernel_size = 5
kernel = np.ones((kernel_size, kernel_size), np.uint8)
# Perform erosion
erosion = cv2.erode(edges, kernel, iterations=1)
# Perform dilation
dilation = cv2.dilate(edges, kernel, iterations=1)
# Perform opening
opening = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
# Perform closing
closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
plt.figure(figsize=(15, 10))
plt.subplot(2, 3, 1)
plt.imshow(cv2.cvtColor(color_image, cv2.COLOR_BGR2RGB))
plt.title('Original Color Image')
plt.axis('on')
plt.subplot(2, 3, 2)
plt.imshow(opening, cmap='gray')
plt.title('Opening')
plt.axis('on')
plt.subplot(2, 3, 3)
plt.imshow(closing, cmap='gray')
plt.title('Closing')
plt.axis('on')
plt.show()
Thus the Opening and Closing operation is used in the image using python and OpenCV.