-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2_1.py
76 lines (63 loc) · 2.14 KB
/
Q2_1.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
import numpy as np
import cv2 as cv
import sys
from matplotlib import pyplot as plt
np.set_printoptions(threshold=sys.maxsize)
cap = cv.VideoCapture('Resources/ball_video1.mp4')
def binary_img_conversion(frame):
gray_img = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
_,binary_img = cv.threshold(gray_img, 127, 255, cv.THRESH_BINARY)
return binary_img
def findpoints(frame):
points = np.where(frame==0)
x_cords = points[0]
y_cords = points[1]
ix_min = np.argmin(x_cords)
ix_max = np.argmax(x_cords)
cord_top = np.array([x_cords[ix_min], y_cords[ix_min]])
cord_bottom = np.array([x_cords[ix_max], y_cords[ix_max]])
return (cord_top, cord_bottom)
def convert_axis(cords):
temp_cords = cords
x_new = cords[:,1]
y_new = 1676 - temp_cords[:,0]
new_cords = np.vstack((x_new,y_new)).T
return new_cords
def standard_least_sqr(cordinates):
x_val = cordinates[:,0]
y_val = cordinates[:,1]
ones = np.ones(x_val.shape)
zeros = np.vstack((np.square(x_val),x_val,ones)).T
trans_1 = np.dot(zeros.transpose(),zeros)
trans_2 = np.dot(np.linalg.inv(trans_1),zeros.transpose())
A_matrix = np.dot(trans_2, y_val.reshape(-1,1))
return A_matrix
def plot_parabola(coefficents, cordinates):
x_val = cordinates[:,0]
y_val = cordinates[:,1]
x_min = np.min(x_val)
x_max = np.max(x_val)
x_plot = np.linspace(x_min-100,x_max+100,300)
ones_plot = np.ones(x_plot.shape)
z_plot = np.vstack((np.square(x_plot),x_plot, ones_plot)).T
y_plot = np.dot(z_plot,coefficents)
plt.title('LS Estimation for Video 1')
plt.plot(x_val,y_val,'ro',x_plot,y_plot,'-b')
plt.show()
def main():
plotarr = []
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
bin_frame = binary_img_conversion(frame)
top,bottom = findpoints(bin_frame)
plotarr.append(top)
plotarr.append(bottom)
plotarr = np.array(plotarr)
plotarr = convert_axis(plotarr)
coeffcient_parabola = standard_least_sqr(plotarr)
plot_parabola(coeffcient_parabola,plotarr)
cap.release()
if __name__ == '__main__':
main()