-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTargetDetector.py
More file actions
61 lines (52 loc) · 2.21 KB
/
Copy pathTargetDetector.py
File metadata and controls
61 lines (52 loc) · 2.21 KB
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
import numpy as np
import cv2
class TargetDetector:
lightblue = (255, 221, 0) # variable that stands for the lightblue color
corners = None # variable that holds the array of corner points
leftRect = None
rightRect = None
isCorrectOrientation = False
def __init__(self):
pass
# method that converts frame to HSV and then returns new frame of thresholded values
def threshold(self, min, max, frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
thresh = cv2.inRange(hsv, min, max)
return thresh
# method that finds and sets the contours to the variable contours for later usage
def contours(self, threshold):
self.contours = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[1]
# method that iterates through each contour in the contours array and filters them by number and size
def filterContours(self):
for c in self.contours:
epsilon = 0.01 * cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c ,epsilon, True)
if(len(approx) == 4 and cv2.contourArea(approx) > 1000):
self.corners = approx
if self.isLeftRect(approx):
self.leftRect = approx
if self.isRightRect(approx):
self.rightRect = approx
def isLeftRect(self, corners):
highest = [0] * 2
lowest = [1000] * 2
for corner in corners:
if(corner[0][0] > highest[0]):
highest = corner[0]
if(corner[0][0] < lowest[0]):
lowest = corner[0]
return highest[1] < lowest[1]
def isRightRect(self, corners):
highest = [0] * 2
lowest = [1000] * 2
for corner in corners:
if(corner[0][0] > highest[0]):
highest = corner[0]
if(corner[0][0] < lowest[0]):
lowest = corner[0]
return highest[1] > lowest[1]
def foundCorners(self):
return self.corners is not None
# getter method that returns the contours array, index of significant contour, and the corners array
def getCorners(self):
return self.corners