-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutlis.py
110 lines (94 loc) · 4.33 KB
/
utlis.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import cv2
import numpy as np
def thresholding(img):
imgHsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#lowerBlack = np.array([0,0,0])
#upperBlack = np.array([50,50,85])
#maskBlack = cv2.inRange(imgHsv, lowerBlack, upperBlack)
lowerWhite = np.array([80,0,0])
upperWhite = np.array([255, 160, 255])
maskWhite = cv2.inRange(imgHsv, lowerWhite, upperWhite)
return maskWhite
# use matrix transformation to give birds eye view
# help improve curve detection
def warpImg(img, points, w, h, inv=False):
pts1 = np.float32(points)
pts2 = np.float32([[0,0], [w,0], [0,h], [w,h]])
if inv:
matrix = cv2.getPerspectiveTransform(pts2,pts1)
else:
matrix = cv2.getPerspectiveTransform(pts1,pts2)
imgWarp = cv2.warpPerspective(img, matrix, (w,h))
return imgWarp
def nothing(a):
pass
# wT = width target
# hT = height target
def initializeTrackbars(intialTracbarVals,wT=480, hT=240):
cv2.namedWindow("Trackbars")
cv2.resizeWindow("Trackbars", 360, 240)
cv2.createTrackbar("Width Top", "Trackbars", intialTracbarVals[0],wT//2, nothing)
cv2.createTrackbar("Height Top", "Trackbars", intialTracbarVals[1], hT, nothing)
cv2.createTrackbar("Width Bottom", "Trackbars", intialTracbarVals[2],wT//2, nothing)
cv2.createTrackbar("Height Bottom", "Trackbars", intialTracbarVals[3], hT, nothing)
def valTrackbars(wT=480, hT=240):
widthTop = cv2.getTrackbarPos("Width Top", "Trackbars")
heightTop = cv2.getTrackbarPos("Height Top", "Trackbars")
widthBottom = cv2.getTrackbarPos("Width Bottom", "Trackbars")
heightBottom = cv2.getTrackbarPos("Height Bottom", "Trackbars")
points = np.float32([(widthTop, heightTop), (wT-widthTop, heightTop),
(widthBottom , heightBottom ), (wT-widthBottom, heightBottom)])
return points
def drawPoints(img, points):
for x in range(4):
cv2.circle(img,(int(points[x][0]),int(points[x][1])),15,(0,0,255),cv2.FILLED)
return img
def getHistogram(img,minPer=0.1,display=False, region=1):
## to get a specific region of the camera view (bottom half, 1 if its
if region == 1:
histValues = np.sum(img,axis=0)
else:
histValues = np.sum(img[int(img.shape[0]//region):,:], axis=0)
#print(histValues)
maxValue = np.max(histValues)
minValue = minPer*maxValue
indexArray = np.where(histValues >= minValue)
basePoint = int(np.average(indexArray))
#print(basePoint)
if display:
imgHist = np.zeros((img.shape[0],img.shape[1],3),np.uint8)
for x,intensity in enumerate(histValues):
cv2.line(imgHist,(x,img.shape[0]), (x,int(img.shape[0]-intensity)),(255,0,255),1)
cv2.circle(imgHist,(basePoint,img.shape[0]),20,(0,255,255),cv2.FILLED)
return basePoint,imgHist
return basePoint
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver