-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge detection.py
42 lines (32 loc) · 1.2 KB
/
edge detection.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
import cv2
import numpy as np
img = cv2.imread('sample.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray, (3, 3), 0)
#sobel
img_sobelx = cv2.Sobel(img_gaussian, cv2.CV_8U, 1, 0, ksize=5)
img_sobely = cv2.Sobel(img_gaussian, cv2.CV_8U, 0, 1, ksize=5)
img_sobel = img_sobelx + img_sobely
#prewitt
kernelx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
kernely = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
img_prewittx = cv2.filter2D(img_gaussian, -1, kernelx)
img_prewitty = cv2.filter2D(img_gaussian, -1, kernely)
#robert
robertx = np.array([[1, 0], [0, -1]])
roberty = np.array([[0, 1], [-1, 0]])
img_robertx = cv2.filter2D(img_gaussian, -1, robertx)
img_roberty = cv2.filter2D(img_gaussian, -1, roberty)
grad = img_robertx + img_roberty
cv2.imshow("Original Image", img)
cv2.imshow("Sobel X", img_sobelx)
cv2.imshow("Sobel Y", img_sobely)
cv2.imshow("Sobel", img_sobel)
cv2.imshow("Prewitt X", img_prewittx)
cv2.imshow("Prewitt Y", img_prewitty)
cv2.imshow("Prewitt", img_prewittx + img_prewitty)
cv2.imshow("robert X", img_robertx)
cv2.imshow("robert Y", img_roberty)
cv2.imshow("robert", grad)
cv2.waitKey(0)
cv2.destroyAllWindows()