Skip to content

Commit

Permalink
ENH: Update calibration writer to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
NicerNewerCar committed Sep 25, 2023
1 parent f06d873 commit 83dd492
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions AutoscoperM/AutoscoperMLib/IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,54 +39,29 @@ def loadSegmentation(segmentationNode: slicer.vtkMRMLSegmentationNode, filename:

def generateCameraCalibrationFile(camera: Camera, filename: str):
"""
Generates a VTK camera calibration yaml file from the given camera.
Generates a VTK camera calibration json file from the given camera.
:param camera: Camera
:type camera: Camera
:param filename: Output file name
:type filename: str
"""
import datetime
import json

contents = {}
contents["@schema"] = "https://autoscoperm.slicer.org/vtk-schema-1.0.json"
contents["version"] = 1.0
contents["focal-point"] = camera.vtkCamera.GetFocalPoint()
contents["camera-position"] = camera.vtkCamera.GetPosition()
contents["view-up"] = camera.vtkCamera.GetViewUp()
contents["view-angle"] = camera.vtkCamera.GetViewAngle()
contents["image-width"] = camera.imageSize[0]
contents["image-height"] = camera.imageSize[1]

contents_json = json.dumps(contents, indent=4)

with open(filename, "w+") as f:
f.write("# VTK Camera Calibration File Version 1.0\n")
f.write(
"# This file was automatically generated by AutoscoperM on " + datetime.datetime.now().strftime("%c") + "\n"
)
f.write("\n")
f.write("version: 1.0\n")
f.write("\n")
f.write(
"focal-point: ["
+ str(camera.vtkCamera.GetFocalPoint()[0])
+ ", "
+ str(camera.vtkCamera.GetFocalPoint()[1])
+ ", "
+ str(camera.vtkCamera.GetFocalPoint()[2])
+ "]\n"
)
f.write(
"camera-position: ["
+ str(camera.vtkCamera.GetPosition()[0])
+ ", "
+ str(camera.vtkCamera.GetPosition()[1])
+ ", "
+ str(camera.vtkCamera.GetPosition()[2])
+ "]\n"
)
f.write(
"view-up: ["
+ str(camera.vtkCamera.GetViewUp()[0])
+ ", "
+ str(camera.vtkCamera.GetViewUp()[1])
+ ", "
+ str(camera.vtkCamera.GetViewUp()[2])
+ "]\n"
)
f.write("view-angle: " + str(camera.vtkCamera.GetViewAngle()) + "\n")
f.write("image-width: " + str(camera.imageSize[0]) + "\n")
f.write("image-height: " + str(camera.imageSize[1]) + "\n")
f.write("\n")
f.write(contents_json)


def generateConfigFile(
Expand Down Expand Up @@ -130,7 +105,7 @@ def generateConfigFile(
# Get the camera calibration files, camera root directories, and volumes
volumes = glob.glob(os.path.join(mainDirectory, subDirectories[0], "*.tif"))
cameraRootDirs = glob.glob(os.path.join(mainDirectory, subDirectories[1], "*"))
calibrationFiles = glob.glob(os.path.join(mainDirectory, subDirectories[2], "*.yaml"))
calibrationFiles = glob.glob(os.path.join(mainDirectory, subDirectories[2], "*.json"))

# Check that we have the same number of camera calibration files and camera root directories
if len(calibrationFiles) != len(cameraRootDirs):
Expand Down

0 comments on commit 83dd492

Please sign in to comment.