-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_gradient.py
More file actions
36 lines (26 loc) · 1005 Bytes
/
image_gradient.py
File metadata and controls
36 lines (26 loc) · 1005 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
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('img/light_maze1.jpeg', cv2.IMREAD_GRAYSCALE)
lap = cv2.Laplacian(img, cv2.CV_64F, ksize=3)
lap = np.uint8(np.absolute(lap))
sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0)
sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1)
sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))
sobelCombined = cv2.bitwise_or(sobelX, sobelY)
# sharpening
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
# sharpen sobelCombined
sharpened = cv2.filter2D(sobelCombined, -1, kernel)
titles = ['image', 'Laplacian', 'sobelX', 'sobelY', 'sobelCombined', 'sharpened']
images = [img, lap, sobelX, sobelY, sobelCombined, sharpened]
# Increase font size for titles
font_size = 16
for i in range(6):
plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i], fontsize=font_size) # Set the fontsize for titles
plt.xticks([]), plt.yticks([])
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()