-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisionConstants.py
123 lines (94 loc) · 5.21 KB
/
VisionConstants.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#THIS FILE CONSISTS OF VISION CONSTANTS, EXPECTED TO BE USED EVERY YEAR
import math
import numpy as np
# Field of View (FOV) of the microsoft camera (68.5 is camera spec)
# Lifecam 3000 from datasheet
# Datasheet: https://dl2jx7zfbtwvr.cloudfront.net/specsheets/WEBC1010.pdf
diagonalView = math.radians(68.5)
# The ELP-USBFHD01M-L21 (Wide angle is 170 degrees)
# Datasheet: http://www.webcamerausb.com/elp-high-speed-120fps-pcb-usb20-webcam-board-2-mega-pixels-1080p-ov2710-cmos-camera-module-with-21mm-lens-elpusbfhd01ml21-p-78.html
# diagonalView = math.radians(170)
# MAY CHANGE IN FUTURE YEARS! It is better to use dynamic calculations as specified in the function
# below. That will allow multiple cameras with different resolutions and FOV parameters to be used
# from the same pipeline code.
image_width = 1280 # 16
image_height = 720 # 9
#To calculate the aspect ratio, first find the greatest common divisor between the
# #image height and image width of the camera
resolution_gcd = math.gcd(image_width, image_height)
#The horizontal aspect is simply the imagewidth divided by the gcd
#for examle, a 640x480 should give a GCD of 160, and aspect ratio is 4:3
horizontalAspect = (image_width/resolution_gcd)
#The vertical Aspect ratio is the image height divided by the gcd
verticalAspect = (image_height/resolution_gcd)
# Reasons for using diagonal aspect is to calculate horizontal field of view.
diagonalAspect = math.hypot(horizontalAspect, verticalAspect)
# Calculations: http://vrguy.blogspot.com/2013/04/converting-diagonal-field-of-view-and.html
horizontalView = math.atan(math.tan(diagonalView / 2) * (horizontalAspect / diagonalAspect)) * 2
verticalView = math.atan(math.tan(diagonalView / 2) * (verticalAspect / diagonalAspect)) * 2
H_FOCAL_LENGTH = image_width / (2 * math.tan((horizontalView / 2)))
V_FOCAL_LENGTH = image_height / (2 * math.tan((verticalView / 2)))
#calculate the Horizontal and Vertical Camera Focal Lengths for this camera and resolution
#input cameraFOV is the manufacturer of the cameras advertised field of view. For example, the
# microsoft lifecam has a Fielf of View (FOV) of 68.5 degrees
# image_width is the image width in pixels (Example 640)
# image_height is the image width in pixels (Example 480)
# Image width and image height are otherwise known as the resolution
def calculateFocalLengthsFromInput(cameraFOV, image_width, image_height):
diagonalView = math.radians(cameraFOV)
#To calculate the aspect ratio, first find the greatest common divisor between the
#image height and image width of the camera
resolution_gcd = math.gcd(image_width, image_height)
#The horizontal aspect is simply the imagewidth divided by the gcd
#for examle, a 640x480 should give a GCD of 160, and aspect ratio is 4:3
horizontalAspect = (image_width/resolution_gcd)
#The vertical Aspect ratio is the image height divided by the gcd
verticalAspect = (image_height/resolution_gcd)
#print("aspect: ", horizontalAspect, verticalAspect)
# Reasons for using diagonal aspect is to calculate horizontal field of view.
diagonalAspect = math.hypot(horizontalAspect, verticalAspect)
# Calculations: http://vrguy.blogspot.com/2013/04/converting-diagonal-field-of-view-and.html
horizontalView = math.atan(math.tan(diagonalView / 2) * (horizontalAspect / diagonalAspect)) * 2
verticalView = math.atan(math.tan(diagonalView / 2) * (verticalAspect / diagonalAspect)) * 2
H_FOCAL_LENGTH = image_width / (2 * math.tan((horizontalView / 2)))
V_FOCAL_LENGTH = image_height / (2 * math.tan((verticalView / 2)))
return H_FOCAL_LENGTH, V_FOCAL_LENGTH
#CARGO_HEIGHT is actual height (for cargo height in feet)
CARGO_BALL_HEIGHT = 0.791667
#image height is the y resolution calculated from image size
#223 is the pixel height of a a ball found at a measured distance (which is 4 feet away)
#65 is the pixel height of a scale image 6 feet away
KNOWN_CARGO_PIXEL_HEIGHT = 223
KNOWN_CARGO_DISTANCE = 4
# Focal Length calculations: https://docs.google.com/presentation/d/1ediRsI-oR3-kwawFJZ34_ZTlQS2SDBLjZasjzZ-eXbQ/pub?start=false&loop=false&slide=id.g12c083cffa_0_165
# H_FOCAL_LENGTH = image_width / (2 * math.tan((horizontalView / 2)))
# V_FOCAL_LENGTH = image_height / (2 * math.tan((verticalView / 2)))
# blurs have to be odd
green_blur = 1
orange_blur = 27
yellow_blur = 1
red_blur = 1
blue_blur = 1
# define colors
purple = (165, 0, 120)
blue = (255, 0, 0)
green = (0, 255, 0)
red = (0, 0, 255)
cyan = (252, 252, 3)
white = (255, 255, 255)
yellow = (0, 255, 255)
orange = (60, 255, 255)
# define range of green of retroreflective tape in HSV
lower_green = np.array([55, 85, 146])
upper_green = np.array([94, 255, 255])
# define range of green of retroreflective tape in HSV
#lower_green = np.array([23, 50, 35])
#upper_green = np.array([85, 255, 255])
lower_yellow = np.array([10, 150, 65]) # was 14, 150, 150
upper_yellow = np.array([30, 255, 255])
# masks for red and blue cargo (HSV)
lower_red = np.array([138,106,123])
upper_red = np.array([180,255,255])
lower_blue = np.array([64,127,116])
upper_blue = np.array([115,213,255])
blingColour = 0