-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.py
More file actions
38 lines (28 loc) · 716 Bytes
/
kernel.py
File metadata and controls
38 lines (28 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read image
img = cv2.imread("image2.jpg", 0)
# Threshold to binary
_, binary = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
# Define kernel (3x3)
kernel = np.ones((3, 3), np.uint8)
# Erosion
eroded = cv2.erode(binary, kernel, iterations=1)
# Dilation
dilated = cv2.dilate(binary, kernel, iterations=1)
# Display results
plt.figure(figsize=(10, 6))
plt.subplot(1, 3, 1)
plt.title("Original Binary")
plt.imshow(binary, cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title("Erosion")
plt.imshow(eroded, cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title("Dilation")
plt.imshow(dilated, cmap='gray')
plt.axis('off')
plt.show()