-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv_functions.py
41 lines (34 loc) · 1.26 KB
/
cv_functions.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
import cv2
import numpy as np
def findlines(frame,rect_start,rect_end,method='b2w'):
if len(frame.shape) == 3:
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
else:
gray = frame
bw_offset = 5
roi = gray[rect_start[1]:rect_end[1],rect_start[0]:rect_end[0]]
ret,thresh = cv2.threshold(roi,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
lsd = cv2.createLineSegmentDetector(0)
minlen = 5
lines = lsd.detect(thresh)[0]
if lines is not None:
x1 = lines[0][0][0]
y1 = lines[0][0][1]
x2 = lines[0][0][2]
y2 = lines[0][0][3]
cpx = (x2-x1)/2
cpy = (y2-y1)/2
line_len = int(((abs(x2-x1)**2)+(abs(y2-y1)**2)**(1/2)))
line_center_px_above = thresh[int(cpy-bw_offset),int(cpx)]
line_center_px_below = thresh[int(cpy+bw_offset),int(cpx)]
if x1 != x2 and line_len > minlen:
if line_center_px_above < line_center_px_below and method == "b2w":
return(x1,y1,x2,y2)
elif line_center_px_above > line_center_px_below and method == "w2b":
return(x1,y1,x2,y2)
else:
return(None,None,None,None)
else:
return(None,None,None,None)
else:
return(None,None,None,None)