Commit 4cd38b20 authored by Andy Regensky's avatar Andy Regensky
Browse files

Update calibration file format to json and use focal length from calibration file

parent b7131da9
{
"focal_length": 1.8,
"coefficients": [
0.03145,
-0.24345,
0.75722,
-1.2132,
1.136,
-0.78004,
0.43818,
-0.10717,
0.0043719,
1.5678,
1.6278e-06
]
}
0.03145, -0.24345, 0.75722, -1.2132, 1.136, -0.78004, 0.43818, -0.10717, 0.0043719, 1.5678, 1.6278e-06
\ No newline at end of file
from PySide2.QtCore import QObject, Signal, Slot
import json
from PySide2.QtCore import QObject, Signal
import numpy as np
from fishui import projections
from fishui import coordinate_conversion
......@@ -11,10 +12,18 @@ class MainViewModel(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.projections = {
'equisolid': projections.EquisolidProjection,
'equidistant': projections.EquidistantProjection,
'orthographic': projections.OrthographicProjection,
'stereographic': projections.StereographicProjection,
'calibrated': projections.CalibratedProjection
}
self.sensor_size_mm = (5.2, 5.2)
self.sensor_size_px = (1088, 1088)
self.focal_length_mm = 1.8
self.projection_cls = projections.EquisolidProjection
self.projection_key = 'equisolid'
self.calibration_coeffs = None
self.origin = (512, 512)
......@@ -59,18 +68,16 @@ class MainViewModel(QObject):
self.focal_length_mm = focal_length_mm
self.updateProjections()
def setProjectionCls(self, projection_cls):
self.projection_cls = projection_cls
def setProjection(self, projection_key, calib_file=None):
if projection_key == 'calibrated' and calib_file is not None:
self.focal_length_mm, self.calibration_coeffs = self.readCalibrationData(calib_file)
elif projection_key == 'calibrated' and calib_file is None:
raise Exception("Calibrated projection requires calibration file argument.")
elif projection_key != 'calibrated' and calib_file is not None:
raise Exception("Calibration file argument is valid for calibrated projection only.")
self.projection_key = projection_key
self.updateProjections()
def setCalibrationData(self, calib_file):
with open(calib_file, 'r') as file:
calib_str = file.read()
calib_coeffs = np.array([float(coeff) for coeff in calib_str.split(",")])
if len(calib_coeffs) == 0:
raise ValueError
self.calibration_coeffs = calib_coeffs
def setOrigin(self, origin):
self.origin = origin
self.updateProjections()
......@@ -97,14 +104,23 @@ class MainViewModel(QObject):
self.perspective_scale = scale
self.updateProjections()
def readCalibrationData(self, calib_file):
with open(calib_file, 'r') as file:
calib_json = json.load(file)
focal_length = calib_json['focal_length']
calib_coeffs = np.array([float(coeff) for coeff in calib_json['coefficients']])
if len(calib_coeffs) == 0:
raise ValueError
return focal_length, calib_coeffs
def updateProjections(self):
focal_length_px = projections.Projection.to_focal_length_px(self.focal_length_mm, self.sensor_size_mm, self.sensor_size_px)
if self.projection_cls == projections.CalibratedProjection:
if self.projection_key == 'calibrated':
px_per_mm = self.sensor_size_px[0]/self.sensor_size_mm[0]
calib_coeffs = self.calibration_coeffs * px_per_mm
self.projection = projections.CalibratedProjection(calib_coeffs, focal_length_px)
else:
self.projection = self.projection_cls(focal_length_px)
self.projection = self.projections[self.projection_key](focal_length_px)
self.perspective = projections.PerspectiveProjection(focal_length_px)
self.fov_180_radius = self.projection.radius(np.deg2rad(90))
self.center = (np.asarray(self.sensor_size_px) - 1) / 2
......
......@@ -58,13 +58,8 @@ class PropertiesWidget(QWidget):
self.projectionLabel = QLabel(self)
self.projectionLabel.setText("Projection:")
self.projections = {'equisolid': projections.EquisolidProjection,
'equidistant': projections.EquidistantProjection,
'orthographic': projections.OrthographicProjection,
'stereographic': projections.StereographicProjection,
'calibrated': projections.CalibratedProjection}
self.projectionComboBox = QComboBox(self)
self.projectionComboBox.addItems(list(self.projections.keys()))
self.projectionComboBox.addItems(list(self.viewModel.projections.keys()))
self.projectionComboBox.currentTextChanged.connect(self.projectionComboBoxTextChanged)
self.originLabel = QLabel(self)
......@@ -170,9 +165,8 @@ class PropertiesWidget(QWidget):
self.resolutionWidthEdit.setText(f"{self.viewModel.sensor_size_px[0]}")
self.resolutionHeightEdit.setText(f"{self.viewModel.sensor_size_px[1]}")
self.focalLengthEdit.setText(f"{self.viewModel.focal_length_mm}")
self.projectionComboBox.setCurrentText(
list(self.projections.keys())[list(self.projections.values()).index(self.viewModel.projection_cls)]
)
self.focalLengthEdit.setEnabled(self.viewModel.projection_key != 'calibrated')
self.projectionComboBox.setCurrentText(self.viewModel.projection_key)
self.originXEdit.setText(f"{self.viewModel.origin[0]}")
self.originYEdit.setText(f"{self.viewModel.origin[1]}")
self.blocksizeWidthEdit.setText(f"{self.viewModel.blocksize[0]}")
......@@ -225,17 +219,17 @@ class PropertiesWidget(QWidget):
@Slot()
def projectionComboBoxTextChanged(self):
projection = self.projectionComboBox.currentText()
calib_file = None
if projection == "calibrated":
calib_file = QFileDialog.getOpenFileName(self,
"Open calibration data (.txt)",
".",
"Text files (""*.txt)")
try:
self.viewModel.setCalibrationData(calib_file[0])
except:
self.updateValues()
return
self.viewModel.setProjectionCls(self.projections[self.projectionComboBox.currentText()])
"Text files (""*.json)")[0]
try:
self.viewModel.setProjection(projection, calib_file)
except (ValueError, FileNotFoundError): # Errors if calib_file is invalid
self.updateValues()
@Slot()
def originXEditingFinished(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment