diff --git a/calibration.py b/calibration.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..54ce5b61b9c40e77c397ae4fa4f25a31b270ccce 100644 --- a/calibration.py +++ b/calibration.py @@ -0,0 +1,58 @@ +import numpy as np +import matplotlib.pyplot as plt + +from sklearn.preprocessing import PolynomialFeatures +from sklearn.linear_model import LinearRegression +from sklearn.pipeline import Pipeline + + +def polynomial_regression(x, y, angles_calib, deg): + """ + Return the coefficients of a polynomial regression of degree deg on x and y. + x: x-positions of the spot (np-array of dim n_calib_steps) + x: y-positions of the spot (np-array of dim n_calib_steps) + angles_calib: H and V angles of calibration (np-array of dim n_calib_steps*2) + deg: degree of the regression's polynome (int) + + RETURNS + coefs: coefficients for the two polynomes (np-array of dim n_coefs*2) + """ + # Preprocessing of the data + measured_values = np.array([x, y]).transpose() + angles_horizontal = angles_calib[0, :] + angles_vertical = angles_calib[1, :] + + # Creation of the models + model_horizontal = Pipeline([('poly', PolynomialFeatures(degree = deg, interaction_only=False)), + ('linear', LinearRegression(fit_intercept=False))]) + model_vertical = Pipeline([('poly', PolynomialFeatures(degree = deg, interaction_only=False)), + ('linear', LinearRegression(fit_intercept=False))]) + # Fitting of the models and return of the results + model_horizontal = model_horizontal.fit(measured_values, angles_horizontal) + coefs_horizontal = model_horizontal.named_steps['linear'].coef_ + print(coefs_horizontal) + model_vertical = model_vertical.fit(measured_values, angles_vertical) + coefs_vertical = model_vertical.named_steps['linear'].coef_ + print(coefs_vertical) + + return np.array([coefs_horizontal, coefs_vertical]).transpose() + + +if __name__ == '__main__': + import matplotlib.pyplot as plt + x = np.array([-10,0,10]) + y = np.array([4,5,6]) + angles_horizontal = np.array([-8,8, 93]) + angles_vertical = np.array([10, 11, 12]) + angles = np.array([angles_horizontal, angles_vertical]) + + coefs = polynomial_regression(x, y, angles, 3) + import pdb; pdb.set_trace() + coefs_horizontal = coefs[:,0] + x_plot = np.linspace(-10, 10, 100) + y_plot = x_plot + angle_horizontal_reg = coefs_horizontal[0] + coefs_horizontal[1] * x_plot + coefs_horizontal[2] * y_plot + coefs_horizontal[3] * x_plot**2 + coefs_horizontal[4] * x_plot*y_plot + coefs_horizontal[5] * y_plot**2 + coefs_horizontal[6] * x_plot**3 + coefs_horizontal[7] * x_plot**2 * y_plot + coefs_horizontal[8] * x_plot * y_plot**2 + coefs_horizontal[9] * y_plot**3 + plt.figure() + plt.scatter(x, angles_horizontal) + plt.plot(x_plot, angle_horizontal_reg) + plt.show() diff --git a/main.py b/main.py index f67b83a60dd0667a22f257e05c315295d25aa599..eb836a02672180cabe441071b1bf421d7377e2cb 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ import numpy as np import uvc from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt +import calibration ### Variables initiales dev_list = uvc.device_list() @@ -270,10 +271,20 @@ class App(QWidget): self.centroid_right_text_3.setText(str(self.centroids[1][1])) # print(self.centroids) if self.calib_done: - self.angles[0][0] = (centroids[0][0] - self.b_leftH) / self.a_leftH - self.angles[0][1] = (centroids[0][1] - self.b_leftV) / self.a_leftV - self.angles[1][0] = (centroids[1][0] - self.b_rightH) / self.a_rightH - self.angles[1][1] = (centroids[1][1] - self.b_rightV) / self.a_rightV + # self.angles[0][0] = (centroids[0][0] - self.b_leftH) / self.a_leftH + # self.angles[0][1] = (centroids[0][1] - self.b_leftV) / self.a_leftV + # self.angles[1][0] = (centroids[1][0] - self.b_rightH) / self.a_rightH + # self.angles[1][1] = (centroids[1][1] - self.b_rightV) / self.a_rightV + x_l = centroids[0][0] + y_l = centroids[0][1] + x_r = centroids[1][0] + y_r = centroids[1][1] + c_l = self.regression_coefs_left + c_r = self.regression_coefs_right + self.angles[0][0] = c_l[0][0] + c_l[1][0] * x_l + c_l[2][0] * y_l + c_l[3][0] * x_l**2 + c_l[4][0] * x_l * y_l + c_l[5][0] * y_l**2 + self.angles[0][1] = c_l[0][1] + c_l[1][1] * x_l + c_l[2][1] * y_l + c_l[3][1] * x_l**2 + c_l[4][1] * x_l * y_l + c_l[5][1] * y_l**2 + self.angles[1][0] = c_r[0][0] + c_r[1][0] * x_r + c_r[2][0] * y_r + c_r[3][0] * x_r**2 + c_r[4][0] * x_r * y_r + c_r[5][0] * y_r**2 + self.angles[1][1] = c_r[0][1] + c_r[1][1] * x_r + c_r[2][1] * y_r + c_r[3][1] * x_r**2 + c_r[4][1] * x_r * y_r + c_r[5][1] * y_r**2 # print(self.angles) self.angle_left_text_2.setText(str(self.angles[0][0])) self.angle_left_text_3.setText(str(self.angles[0][1])) @@ -375,31 +386,34 @@ class App(QWidget): print(anglesH_calib.reshape(1,-1).shape) print(self.calibH[0,:].shape) - self.reg_leftH.fit(anglesH_calib.reshape(-1,1), self.calibH[0,:]) - print(anglesH_calib.reshape(-1,1)) - print(self.calibH[0,:]) - self.reg_rightH.fit(anglesH_calib.reshape(-1,1), self.calibH[1,:]) - self.reg_leftV.fit(anglesV_calib.reshape(-1,1), self.calibV[0,:]) - self.reg_rightV.fit(anglesV_calib.reshape(-1,1), self.calibV[1,:]) - self.a_leftH = self.reg_leftH.coef_ - self.b_leftH = self.reg_leftH.intercept_ - self.a_rightH = self.reg_rightH.coef_ - self.b_rightH = self.reg_leftH.intercept_ - self.a_leftV = self.reg_leftV.coef_ - self.b_leftV = self.reg_leftH.intercept_ - self.a_rightV = self.reg_rightV.coef_ - self.b_rightV = self.reg_leftH.intercept_ - print(self.a_leftH) - print(self.b_leftV) - print(self.a_rightH) - print(self.b_rightV) - print(self.a_leftH) - print(self.b_leftV) - print(self.a_rightH) - print(self.b_rightV) - plt.plot(anglesH_calib, self.calibH[0,:], 'r*') - plt.plot(anglesH_calib, self.a_leftH[0] * anglesH_calib + self.b_leftH, 'r-') - plt.show() + self.angles_calib = np.array([anglesH_calib, anglesV_calib]).transpose() + self.regression_coefs_left = calibration.polynomial_regression(self.calibH[:, 0], self.calibV[:, 0], self.angles_calib, deg=3) + self.regression_coefs_right = calibration.polynomial_regression(self.calibH[:, 1], self.calibV[:, 1], self.angles_calib, deg=3) + # self.reg_leftH.fit(anglesH_calib.reshape(-1,1), self.calibH[0,:]) + # print(anglesH_calib.reshape(-1,1)) + # print(self.calibH[0,:]) + # self.reg_rightH.fit(anglesH_calib.reshape(-1,1), self.calibH[1,:]) + # self.reg_leftV.fit(anglesV_calib.reshape(-1,1), self.calibV[0,:]) + # self.reg_rightV.fit(anglesV_calib.reshape(-1,1), self.calibV[1,:]) + # self.a_leftH = self.reg_leftH.coef_ + # self.b_leftH = self.reg_leftH.intercept_ + # self.a_rightH = self.reg_rightH.coef_ + # self.b_rightH = self.reg_leftH.intercept_ + # self.a_leftV = self.reg_leftV.coef_ + # self.b_leftV = self.reg_leftH.intercept_ + # self.a_rightV = self.reg_rightV.coef_ + # self.b_rightV = self.reg_leftH.intercept_ + # print(self.a_leftH) + # print(self.b_leftV) + # print(self.a_rightH) + # print(self.b_rightV) + # print(self.a_leftH) + # print(self.b_leftV) + # print(self.a_rightH) + # print(self.b_rightV) + # plt.plot(anglesH_calib, self.calibH[0,:], 'r*') + # plt.plot(anglesH_calib, self.a_leftH[0] * anglesH_calib + self.b_leftH, 'r-') + # plt.show() print('Calibration complete !') self.calib_step_label.setText('Calibration complete !')