-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradients.py
71 lines (49 loc) · 1.86 KB
/
Gradients.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
Gradients == vector arrows pointing from lighter to darker region
Sobel_Feldman operators (aka Sobel) : rate of change of color intensity
Helps in edge detection
Sobel operator in the x-direction looks like you're shining a torch from the RHS of img
Similarly, Sobel operator in y-direction is like shining a torch from the bottom of the img
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_gradients/py_gradients.html
'''
import cv2
import numpy as np
def get_sobel(img, x, y):
sobel = cv2.Sobel(src=img, ddepth=cv2.CV_16S, dx=x, dy=y, ksize=7) #careful with ddepth here, I haven't understood it
return sobel
def get_laplace(img):
laplace_img = cv2.Laplacian(src=img, ddepth=cv2.CV_64F)
return laplace_img
def blend(img1, img2):
b_img = cv2.addWeighted(src1=img1, alpha=0.5, src2=img2, beta=0.5, gamma=0)
return b_img
def get_gradient(img):
kernel = np.ones((3,3), np.int16)
t_img = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
return t_img
def threshold(img):
ret, t_img = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
return t_img
def main():
img = cv2.imread("Computer-Vision-with-Python//DATA//sudoku.jpg", 0)
img = cv2.resize(img, (0,0), img, 0.5, 0.5)
print(type(img))
sobelx_img = get_sobel(img,0,1)
sobely_img = get_sobel(img,1,0)
sobel_img = blend(sobelx_img, sobely_img)
laplace_img = get_laplace(img)
gradient_img = get_gradient(sobel_img)
gradient_img_o = get_gradient(img)
threshold_img = threshold(img)
while True:
#cv2.imshow("Original img", img)
cv2.imshow("Sobel img", sobel_img)
cv2.imshow("Laplace img", laplace_img)
cv2.imshow("Gradient on Sobel img", gradient_img)
#cv2.imshow("Gradient on Original img", gradient_img_o)
cv2.imshow("Threshold on Original", threshold_img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
main()