-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharuco_utils.py
More file actions
62 lines (45 loc) · 2.04 KB
/
aruco_utils.py
File metadata and controls
62 lines (45 loc) · 2.04 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2
import cv2.aruco as aruco
'''
calculate_pixel_mm_ration --> takes path to the image, marker size in millimetres and aruco dictionary type as arguments
--> returns the pixel-to-mm ratio(float), by detecting the aruco marker and then using it's known perimeter(in mm) and
perimeter in pixels.
--> returns 0.0 if there is no aruco marker detected
'''
def calculate_pixel_mm_ratio(image_path, marker_size_mm, aruco_dict_type):
# Load ArUco dictionary
parameters = aruco.DetectorParameters()
aruco_dict = aruco.getPredefinedDictionary(aruco_dict_type)
# Create ArUco detector
aruco_detector = aruco.ArucoDetector(aruco_dict, parameters)
image = cv2.imread(image_path)
pixel_mm_ratio = 0.0
# Detect ArUco marker
corners, ids, rejected = aruco_detector.detectMarkers(image)
if ids is not None:
# Perimeter of the ArUco marker
aruco_perimeter = cv2.arcLength(corners[0], True)
# Calculate pixel to mm ratio
pixel_mm_ratio = aruco_perimeter / (marker_size_mm * 4)
return pixel_mm_ratio
'''
calculate_pixel_mm_ratio_video --> takes frame, marker size in millimetres and aruco dictionary type as arguments
--> returns the pixel-to-mm ratio(float), by detecting the aruco marker and then using it's known perimeter(in mm) and
perimeter in pixels.
--> returns 0.0 if there is no aruco marker detected
'''
def calculate_pixel_mm_ratio_video(frame, marker_size_mm, aruco_dict_type):
# Load ArUco dictionary
parameters = aruco.DetectorParameters()
aruco_dict = aruco.getPredefinedDictionary(aruco_dict_type)
# Create ArUco detector
aruco_detector = aruco.ArucoDetector(aruco_dict, parameters)
pixel_mm_ratio = 0.0
# Detect ArUco marker
corners, ids, rejected = aruco_detector.detectMarkers(frame)
if ids is not None:
# Perimeter of the ArUco marker
aruco_perimeter = cv2.arcLength(corners[0], True)
# Calculate pixel to mm ratio
pixel_mm_ratio = aruco_perimeter / (marker_size_mm * 4)
return pixel_mm_ratio