-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_edge.py
48 lines (31 loc) · 1.51 KB
/
detect_edge.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
import numpy as np
import cv2
def abs_sobel_thresh(img, orient='x', sobel_kernel=3, thresh=(0, 255)):
if orient == 'x':
sobel = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
elif orient == 'y':
sobel = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
else:
print('orient must be x or y!')
abs_sobel = np.absolute(sobel)
scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
binary = np.zeros_like(scaled_sobel)
binary[(scaled_sobel > thresh[0]) & (scaled_sobel < thresh[1])] = 1
return binary
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)):
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
mag_sobel = np.sqrt(np.power(sobelx, 2) + np.power(sobely, 2))
scaled_sobel = np.uint8(255*mag_sobel/np.max(mag_sobel))
binary = np.zeros_like(scaled_sobel)
binary[(scaled_sobel >= mag_thresh[0]) & (scaled_sobel <= mag_thresh[1])] = 1
return binary
def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2)):
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
abs_sobelx = np.absolute(sobelx)
abs_sobely = np.absolute(sobely)
directions = np.arctan2(abs_sobely, abs_sobelx)
binary = np.zeros_like(directions)
binary[(directions >= thresh[0]) & (directions <= thresh[1])] = 1
return binary