-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetermine_pixel_meter_scale.py
More file actions
33 lines (25 loc) · 1.05 KB
/
determine_pixel_meter_scale.py
File metadata and controls
33 lines (25 loc) · 1.05 KB
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
import cv2
from scipy.spatial import distance
input_video = "test1.mp4"
ref_pt = []
out_file = "{}_pixel_scale_map.txt".format()
def selection(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
ref_pt.append((x, y))
image = cv2.imread("test.png")
cv2.namedWindow("image")
cv2.setMouseCallback("image", selection)
with open(out_file, "w") as out:
while True:
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
if len(ref_pt) == 2:
scale_factor = distance.euclidean(ref_pt[0], ref_pt[1]) / 6
cv2.line(image, ref_pt[0], ref_pt[1], (0, 255, 0), thickness=2)
cv2.putText(image, "1m = {} px".format(scale_factor), (ref_pt[0][0] + 10, ref_pt[0][1]+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
print("Your scale factor is: {}".format(scale_factor))
out.write("{},{},{},{},{}\n".format(ref_pt[0][0], ref_pt[0][1], ref_pt[1][0], ref_pt[1][0],scale_factor))
ref_pt = []
elif key == ord("q"):
break
cv2.destroyAllWindows()