Advertisement
Guest User

cam_dist_fit

a guest
Dec 11th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import matplotlib.pyplot as plt;
  5. import numpy as np;
  6. import scipy.optimize as opt;
  7.  
  8. # This is the cost function which parameters we are looking for
  9. def blobSizeToDistance(blob_size, a, b):
  10. ##########################
  11. dist = a / blob_size - b # <<<-- Define your function here
  12. ##########################
  13. return dist
  14.  
  15. # Define the experimentally measured data
  16. x_data = [27.5, 31, 33.7, 38.1, 41, 45.4, 50.8, 55.47, 62.6, 72.6, 84.5, 98, 116, 137, 182] # Size [px]
  17. y_data = [1700, 1600, 1500, 1400, 1300, 1200, 1100, 1000, 900, 800, 700, 600, 500, 400, 300] # Distance [mm]
  18.  
  19. # Lower and upper boundaries for each fitted parameter
  20. bnd_lo = [-np.inf, -np.inf]
  21. bnd_up = [np.inf, np.inf]
  22.  
  23. # Plot the measured data
  24. plt.plot(x_data, y_data, ".", label="measured data");
  25.  
  26. # The curve fitting happens here
  27. optimized_parameters, pcov = opt.curve_fit(blobSizeToDistance, x_data, y_data, bounds=(bnd_lo, bnd_up));
  28.  
  29. a = optimized_parameters[0]
  30. b = optimized_parameters[1]
  31.  
  32. print("Optimized parameters:")
  33. print(" a = " + str(a))
  34. print(" b = " + str(b))
  35.  
  36. # Calculate points with the optimized parameters
  37. x_data_fit = np.linspace(min(x_data), max(x_data), 100)
  38. y_data_fit = blobSizeToDistance(x_data_fit, *optimized_parameters)
  39.  
  40. # Plot the fit
  41. plt.plot(x_data_fit, y_data_fit, label="fited data");
  42.  
  43. # Show the graph
  44. plt.legend();
  45. plt.xlabel("Blob size (px)")
  46. plt.ylabel("Distance (mm)")
  47. plt.show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement