Commit 1d5e8adb authored by Matt Clarkson's avatar Matt Clarkson

Issue #5: merge sksurgeryrendermodelslikecamera.

parent cf20734e
......@@ -56,12 +56,14 @@ setup(
'opencv-contrib-python>=3.4.4',
'PySide2>=5.12.0',
'scikit-surgeryimage>=0.1.1',
'scikit-surgeryvtk>=0.3.0',
],
entry_points={
'console_scripts': [
'sksurgeryvideolag=sksurgeryutils.ui.sksurgeryvideolag_command_line:main',
'sksurgerycharucotest=sksurgeryutils.ui.sksurgerycharucotest_command_line:main',
'sksurgeryrendermodelslikecamera=sksurgeryutils.ui.sksurgeryrendermodelslikecamera_command_line:main',
],
},
)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from sksurgeryutils.ui.sksurgeryrendermodelslikecamera_command_line import main
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
# coding=utf-8
"""Command line processing"""
import argparse
from sksurgeryutils import __version__
from sksurgeryutils.ui.sksurgeryrendermodelslikecamera_demo import run_demo
def main(args=None):
"""Entry point for rendermodelslikecamera application"""
default_camera_interval = 33
default_screen_interval = 15
parser = argparse.ArgumentParser(description='scikit-surgeryrendermodelslikecamera')
parser.add_argument("-f", "--image_file",
required=True,
default=None,
type=str,
help="Background image")
parser.add_argument("-m", "--models",
required=False,
default=None,
type=str,
help='Models Directory')
parser.add_argument("-e", "--extrinsic_matrix",
required=False,
default=None,
type=str,
help="extrinsic matrix file")
parser.add_argument("-i", "--intrinsic_matrix",
required=False,
default=None,
type=str,
help="intrinsic matrix file")
parser.add_argument("-p", "--points_file",
required=False,
default=None,
type=str,
help="File of points to render")
version_string = __version__
friendly_version_string = version_string if version_string else 'unknown'
parser.add_argument(
"-v", "--version",
action='version',
version='scikit-surgeryvtk version ' + friendly_version_string)
args = parser.parse_args(args)
run_demo( args.image_file,
args.models,
args.extrinsic_matrix,
args.intrinsic_matrix,
args.points_file)
# coding=utf-8
""" Demo app, to render a model from a particular perspective"""
import cv2
import sys
import vtk
import numpy as np
from PySide2 import QtWidgets
from PySide2.QtCore import Qt
from sksurgeryvtk.widgets import vtk_overlay_window
from sksurgeryvtk.models import vtk_surface_model_directory_loader, vtk_point_model
import sksurgeryvtk.camera.vtk_camera_model as cam
def run_demo(image_file, model_dir, extrinsics_file, intrinsics_file, points_file):
app = QtWidgets.QApplication([])
img = cv2.imread(image_file)
vtk_widget = vtk_overlay_window.VTKOverlayWindow()
vtk_widget.set_video_image(img)
# Use a QLayout to maintain corret aspect ratio of the vtk widget
# Important: the '0, Qt.AlignCenter' arguments in addWidget are
# needed for correct scaling.
layout = QtWidgets.QVBoxLayout()
layout.addWidget(vtk_widget, 0, Qt.AlignCenter)
window = QtWidgets.QWidget()
window.setLayout(layout)
window.show()
if points_file:
points = np.loadtxt(points_file)
vtk_points = vtk_point_model.VTKPointModel(points.astype(np.float), points.astype(np.byte))
vtk_widget.add_vtk_actor(vtk_points.actor)
if model_dir:
model_loader = vtk_surface_model_directory_loader.VTKSurfaceModelDirectoryLoader(model_dir)
vtk_widget.add_vtk_models(model_loader.models)
if extrinsics_file and intrinsics_file:
# Load intrinsics for projection matrix.
intrinsics = np.loadtxt(intrinsics_file, dtype=np.float)
# Load extrinsics for camera pose (position, orientation).
extrinsics = np.loadtxt(extrinsics_file)
model_to_camera = cam.create_vtk_matrix_from_numpy(extrinsics)
# OpenCV maps from model to camera.
# Assume model == world, so the input matrix is world_to_camera.
# We need camera_to_world to position the camera in world coordinates.
# So, invert it.
model_to_camera.Invert()
height, width = img.shape[:2]
projection_matrix = cam.compute_projection_matrix(width, height,
intrinsics[0][0], intrinsics[1][1],
intrinsics[0][2], intrinsics[1][2],
0.01, 1000,
1
)
camera = vtk_widget.get_foreground_camera()
cam.set_camera_pose(camera, model_to_camera)
cam.set_projection_matrix(camera, projection_matrix)
return sys.exit(app.exec_())
\ No newline at end of file
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