import numpy as np
import math
import json
import csv
[docs]
class Config:
"""
Class for handling configuration parameters for the MoilSDK application.
"""
def __init__(self, filename):
super(Config, self).__init__()
self.PI = 3.1415926
self.alphaToRho_Table = []
self.rhoToAlpha_Table = []
"""setting default parameter"""
if filename is None:
pass
else:
with open(filename) as f:
data = json.load(f)
self.camera = data["cameraName"]
self.sensor_width = data['cameraSensorWidth']
self.sensor_height = data['cameraSensorHeight']
self.Icx = data['iCx']
self.Icy = data['iCy']
self.ratio = data['ratio']
self.imageWidth = data['imageWidth']
self.imageHeight = data['imageHeight']
self.calibrationRatio = data['calibrationRatio']
self.parameter0 = data['parameter0']
self.parameter1 = data['parameter1']
self.parameter2 = data['parameter2']
self.parameter3 = data['parameter3']
self.parameter4 = data['parameter4']
self.parameter5 = data['parameter5']
self.initAlphaRho_Table()
[docs]
def get_cameraName(self):
"""
Returns the name of the camera.
Returns:
str: The name of the camera.
"""
return self.camera
[docs]
def get_sensorWidth(self):
"""
Returns the width of the camera sensor.
Returns:
int: The width of the camera sensor.
"""
return self.sensor_width
[docs]
def get_sensor_height(self):
"""
Returns the height of the camera sensor.
Returns:
int: The height of the camera sensor.
"""
return self.sensor_height
[docs]
def get_Icx(self):
"""
Returns the center x-coordinate of the camera sensor.
Returns:
int: The center x-coordinate of the camera sensor.
"""
return self.Icx
[docs]
def get_Icy(self):
"""
Returns the center y-coordinate of the camera sensor.
Returns:
int: The center y-coordinate of the camera sensor.
"""
return self.Icy
[docs]
def get_ratio(self):
"""
Returns the ratio of the camera.
Returns:
float: The ratio of the camera.
"""
return self.ratio
[docs]
def get_imageWidth(self):
"""
Returns the width of the image.
Returns:
int: The width of the image.
"""
return self.imageWidth
[docs]
def get_imageHeight(self):
"""
Returns the height of the image.
Returns:
int: The height of the image.
"""
return self.imageHeight
[docs]
def get_calibrationRatio(self):
"""
Returns the calibration ratio.
Returns:
float: The calibration ratio.
"""
return self.calibrationRatio
[docs]
def get_parameter0(self):
"""
Returns parameter0 used for alpha-to-rho conversion.
Returns:
float: Parameter0 used for alpha-to-rho conversion.
"""
return self.parameter0
[docs]
def get_parameter1(self):
"""
Returns parameter1 used for alpha-to-rho conversion.
Returns:
float: Parameter1 used for alpha-to-rho conversion.
"""
return self.parameter1
[docs]
def get_parameter2(self):
"""
Returns parameter2 used for alpha-to-rho conversion.
Returns:
float: Parameter2 used for alpha-to-rho conversion.
"""
return self.parameter2
[docs]
def get_parameter3(self):
"""
Returns parameter3 used for alpha-to-rho conversion.
Returns:
float: Parameter3 used for alpha-to-rho conversion.
"""
return self.parameter3
[docs]
def get_parameter4(self):
"""
Returns parameter4 used for alpha-to-rho conversion.
Returns:
float: Parameter4 used for alpha-to-rho conversion.
"""
return self.parameter4
[docs]
def get_parameter5(self):
"""
Returns parameter5 used for alpha-to-rho conversion.
Returns:
float: Parameter5 used for alpha-to-rho conversion.
"""
return self.parameter5
[docs]
def initAlphaRho_Table(self):
"""
Initializes the alpha-to-rho and rho-to-alpha conversion tables.
"""
for i in range(1800):
alpha = i / 10 * 3.1415926 / 180
self.alphaToRho_Table.append((self.parameter0 * alpha * alpha * alpha * alpha * alpha * alpha
+ self.parameter1 * alpha * alpha * alpha * alpha * alpha
+ self.parameter2 * alpha * alpha * alpha * alpha
+ self.parameter3 * alpha * alpha * alpha
+ self.parameter4 * alpha * alpha
+ self.parameter5 * alpha) * self.calibrationRatio)
i += 1
i = 0
index = 0
while i < 1800:
while index < self.alphaToRho_Table[i]:
self.rhoToAlpha_Table.append(i)
index += 1
i += 1
while index < 3600:
self.rhoToAlpha_Table.append(i)
index += 1
[docs]
def getAlphaFromRho(self, rho):
"""
Converts rho value to alpha angle.
Args:
rho (int): The rho value.
Returns:
float: The alpha angle.
"""
if rho >= 0:
return self.rhoToAlpha_Table[rho] / 10
else:
return -self.rhoToAlpha_Table[-rho] / 10
[docs]
def getRhoFromAlpha(self, alpha):
"""
Converts alpha angle to rho value.
Args:
alpha (float): The alpha angle.
Returns:
int: The rho value.
"""
return self.alphaToRho_Table[round(alpha * 10)]
[docs]
def get_alpha_beta(self, mode, delta_x, delta_y):
"""
Calculates alpha and beta angles from delta_x and delta_y.
Args:
mode (int): The mode for calculation.
delta_x (float): The change in x-coordinate.
delta_y (float): The change in y-coordinate.
Returns:
tuple: A tuple containing the calculated alpha and beta angles.
"""
if mode == 0:
r = round(math.sqrt(math.pow(delta_x, 2) + math.pow(delta_y, 2)))
alpha = self.getAlphaFromRho(r)
if delta_x == 0:
angle = 0
else:
angle = (math.atan2(delta_y, delta_x) * 180) / self.PI
beta = 90 - angle
else:
alpha = self.getAlphaFromRho(delta_y)
beta = self.getAlphaFromRho(delta_x)
return alpha, beta