-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathENPM673 Problem 2.py
126 lines (102 loc) · 3.12 KB
/
ENPM673 Problem 2.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import matplotlib.pyplot as plt
import random
# Pulling data from Data sets
datafile_1 = np.genfromtxt('data_1.csv', delimiter=',')
# print(datafile_1)
datafile_2 = np.genfromtxt('data_2.csv', delimiter=',')
# print(datafile_2)
datafile_1 = datafile_1[1:]
datafile_2 = datafile_2[1:]
# Least square method for Data Set 1
# Fitting a parabola for y=Ax+B
def fit_parabola(points):
n_points = len(points)
# calculating matrix A and B for least squares
A = []
B = []
for point in points:
x = point[0]
y = point[1]
A.append([x**2,x,1])
B.append([y])
# Array A, B
A = np.array(A)
B = np.array(B)
# print(A)
# print(B)
# Matrix multiplication for getting solution (sol) = (ATA)^-1.AT.B
sol = np.matmul(np.transpose(A),A)
# print(sol)
sol = np.linalg.inv(sol)
# print(sol)
sol = np.matmul(sol,np.transpose(A))
# print(sol)
sol = np.matmul(sol,B)
return sol
plt.scatter(datafile_1[:,0],datafile_1[:,1])
# print(datafile_1[:,0])
datafile_1_list = datafile_1.tolist()
least_square_sol = fit_parabola(datafile_1_list)
# print(least_square_sol)
a = least_square_sol[0]
b = least_square_sol[1]
c = least_square_sol[2]
x = np.linspace(0,500,1000)
# print(x)
y = a*(x**2)+b*x+c
# print(y)
#plotting the parabola
plt.plot(x,y)
plt.title('Least Square Method')
plt.show()
#RANSAC Method for Data Set 2
datafile_2_list = datafile_2.tolist()
dist_thresh = 70 # Distance threshold for line fundamental matrix, we considered this by looking at the data set
thresh_prob = 0.8 # desired probability that we get a good sample
plt.scatter(datafile_2[:,0],datafile_2[:,1])
sol_found = False
for n_interations in range(150):
#choosing random 3 points
random_points = []
random_indices = random.sample(range(len(datafile_2_list)), 3)
# printing (random_indices)
for random_index in random_indices:
random_points.append(datafile_2_list[random_index])
# print(random_points)
random_points_sol = fit_parabola(random_points)
a = random_points_sol[0]
b = random_points_sol[1]
c = random_points_sol[2]
# fit a parabola using these fit_parabola(points)
inliers = []
# finding distance of all the points from parbola and append to inliers
for point in datafile_2_list:
x_curr_point = point[0]
y_curr_point = point[1]
# find distsance of point from parabola
dist = abs(y_curr_point- (a*(x_curr_point**2)+b*x_curr_point+c))
# print(dist)
# if distance is less than the threshold that will be an inlier
if dist<dist_thresh:
inliers.append([x_curr_point,y_curr_point])
# If the probability to find the best sample is more than our considered threshold probability then we have the best solution
if((float(len(inliers))/len(datafile_2_list))>thresh_prob):
sol_found = True
break
# plotting parabola
if(sol_found==True):
inliers_arr = np.array(inliers)
plt.scatter(inliers_arr[:,0],inliers_arr[:,1],color="r")
least_square_sol = fit_parabola(inliers)
# print(least_square_sol)
a = least_square_sol[0]
b = least_square_sol[1]
c = least_square_sol[2]
x = np.linspace(0,500,1000)
y = a*(x**2)+b*x+c
plt.title('RANSAC Method')
plt.plot(x,y)
plt.show()
else:
print('I counld not find a solution')