Advertisement
yclee126

fitting check

Jul 28th, 2021
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. def nothing(x):
  5. pass
  6.  
  7. win_w, win_h = 800, 100
  8. img = np.zeros((win_h,win_w,3), np.uint8)
  9. cv2.imshow('image', img)
  10. cv2.createTrackbar('1cm', 'image', 1, 50, nothing)
  11. cv2.createTrackbar('0.01cm', 'image', 0, 100, nothing)
  12. cv2.createTrackbar('offset(0.1cm)', 'image', 0, 500, nothing)
  13. cv2.waitKey(1)
  14.  
  15. # centimeters
  16. vals = (0, 7.5, 9.5, 8.5, 9.5, 9, 65, 9, 9.5, 8.5, 9.5, 7.5, 7.5, 9.5, 8.5, 9.5, 9, 65, 9, 9.5, 8.5, 9.5, 7.5)
  17. dist = 0.0
  18. total_dist = 0
  19. for val in vals:
  20. total_dist += val
  21.  
  22. # pixels
  23. xs = 50
  24. xe = 750
  25. ys = 20
  26. ye = 50
  27. yt = 80
  28.  
  29. # conversion (cm -> px)
  30. conv = lambda a: int(a / total_dist * (xe - xs) + xs)
  31.  
  32. while 1:
  33. img = cv2.rectangle(img, (0, 0), (win_w, win_h), (255, 255, 255), -1)
  34.  
  35. dist = 0
  36. for val in vals:
  37. dist += val
  38. xpos = conv(dist)
  39. img = cv2.line(img, (xpos, ys), (xpos, ye), (0, 0, 0), 2)
  40.  
  41. coarse = cv2.getTrackbarPos('1cm','image')
  42. coarse = max(coarse, 1) # prevent infinite loop
  43. fine = cv2.getTrackbarPos('0.01cm', 'image') / 100
  44. offset = cv2.getTrackbarPos('offset(0.1cm)', 'image') / 10
  45. dist = offset
  46. ticks = 0
  47. while dist < total_dist:
  48. xpos = conv(dist)
  49. img = cv2.line(img, (xpos, ye), (xpos, yt), (255, 0, 0), 2)
  50. dist += coarse + fine
  51. ticks += 1
  52.  
  53. print(f'{ticks} ticks, {coarse+fine}cm gap, {offset}cm offset ', end='\r')
  54. cv2.imshow('image', img)
  55.  
  56. if cv2.waitKey(1) & 0xFF == ord('e'):
  57. break
  58.  
  59. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement