Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- def nothing(x):
- pass
- win_w, win_h = 800, 100
- img = np.zeros((win_h,win_w,3), np.uint8)
- cv2.imshow('image', img)
- cv2.createTrackbar('1cm', 'image', 1, 50, nothing)
- cv2.createTrackbar('0.01cm', 'image', 0, 100, nothing)
- cv2.createTrackbar('offset(0.1cm)', 'image', 0, 500, nothing)
- cv2.waitKey(1)
- # centimeters
- 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)
- dist = 0.0
- total_dist = 0
- for val in vals:
- total_dist += val
- # pixels
- xs = 50
- xe = 750
- ys = 20
- ye = 50
- yt = 80
- # conversion (cm -> px)
- conv = lambda a: int(a / total_dist * (xe - xs) + xs)
- while 1:
- img = cv2.rectangle(img, (0, 0), (win_w, win_h), (255, 255, 255), -1)
- dist = 0
- for val in vals:
- dist += val
- xpos = conv(dist)
- img = cv2.line(img, (xpos, ys), (xpos, ye), (0, 0, 0), 2)
- coarse = cv2.getTrackbarPos('1cm','image')
- coarse = max(coarse, 1) # prevent infinite loop
- fine = cv2.getTrackbarPos('0.01cm', 'image') / 100
- offset = cv2.getTrackbarPos('offset(0.1cm)', 'image') / 10
- dist = offset
- ticks = 0
- while dist < total_dist:
- xpos = conv(dist)
- img = cv2.line(img, (xpos, ye), (xpos, yt), (255, 0, 0), 2)
- dist += coarse + fine
- ticks += 1
- print(f'{ticks} ticks, {coarse+fine}cm gap, {offset}cm offset ', end='\r')
- cv2.imshow('image', img)
- if cv2.waitKey(1) & 0xFF == ord('e'):
- break
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement