-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDetectScreen.py
More file actions
46 lines (27 loc) · 938 Bytes
/
Copy pathDetectScreen.py
File metadata and controls
46 lines (27 loc) · 938 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
37
38
39
40
41
42
43
44
45
46
import cv2
import numpy as np
def calculateRect(contour):
contour = contour[np.argsort(contour[:, 0])]
contour = contour[np.argsort(contour[:, 1])]
leftTop = contour[0]
rightDown = contour[len(contour)]
rect = [leftTop, rightDown]
return rect
def selectScreen(contours):
area = -1.0
result = contours[0]
for contour in contours:
curr = cv2.contourArea(contour)
if curr > area:
curr = area
result = calculateRect(curr)
return result
def DetectScreen(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 21)
ret, binarythresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY +cv2.THRESH_OTSU)
img2, contours, hierarchy = cv2.findContours(binarythresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if hierarchy is None:
return []
rect = selectScreen(contours)
return rect