Commit f6fb7432 authored by Matt Clarkson's avatar Matt Clarkson

Issue #7: Update sksurgeryrendermodelslikecamera to use new generator in scikit-surgeryvtk.

parent a7dd1c2e
Pipeline #3497 passed with stages
in 6 minutes and 35 seconds
......@@ -6,6 +6,8 @@
.coverage
*.pyc
.pytest_cache/
outputs/
tests/output
# PyInstaller
build/
......
......@@ -4,8 +4,8 @@
# doc/requirements.rst
six>=1.10
numpy>=1.11
opencv-contrib-python>=3.4.4
--extra-index-url http://download.qt.io/snapshots/ci/pyside/5.9/latest/ --trusted-host download.qt.io
PySide2<=5.12.0
opencv-contrib-python>=4.1.0.26
PySide2<=5.13.1
scikit-surgerycore>=0.1.7
scikit-surgeryimage>=0.2.0
scikit-surgeryvtk>=0.5.1
scikit-surgeryvtk>=0.12.0
# coding=utf-8
"""Command line processing"""
"""
Command line entry point for sksurgeryrendermodleslikecamera application.
"""
import argparse
from sksurgeryutils import __version__
......@@ -8,60 +10,79 @@ from sksurgeryutils.ui.sksurgeryrendermodelslikecamera_demo import run_demo
def main(args=None):
"""Entry point for rendermodelslikecamera application"""
"""
Entry point for sksurgeryrendermodleslikecamera application.
"""
parser = argparse.ArgumentParser(
description='scikit-surgeryrendermodelslikecamera')
description='sksurgeryrendermodleslikecamera')
parser.add_argument("-f", "--image_file",
parser.add_argument("-m", "--models",
required=True,
default=None,
type=str,
help="Background image")
help='Models .json file.')
parser.add_argument("-m", "--models",
parser.add_argument("-b", "--background",
required=True,
type=str,
help="Background image file name.")
parser.add_argument("-i", "--intrinsic_matrix",
required=True,
type=str,
help="Intrinsic 3x3 matrix file.")
parser.add_argument("-w", "--model_to_world",
required=False,
default=None,
default="0,0,0,0,0,0",
type=str,
help='Models Directory')
help="rx,ry,rz,tx,ty,tz in degrees, mm")
parser.add_argument("-e", "--extrinsic_matrix",
parser.add_argument("-c", "--camera_to_world",
required=False,
default=None,
default="0,0,0,0,0,0",
type=str,
help="Camera to world 4x4 matrix file")
help="rx,ry,rz,tx,ty,tz in degrees, mm")
parser.add_argument("-i", "--intrinsic_matrix",
parser.add_argument("-l", "--left_to_right",
required=False,
default=None,
default="0,0,0,0,0,0",
type=str,
help="Intrinsic 3x3 matrix file")
help="rx,ry,rz,tx,ty,tz in degrees, mm")
parser.add_argument("-p", "--points_file",
parser.add_argument("-r", "--clippingrange",
required=False,
default=None,
default="1,1000",
type=str,
help="File of points to render")
help="near,far")
parser.add_argument("-s", "--sigma",
required=False,
default=0.0,
type=float,
help="Sigma for Gaussian Blur.")
parser.add_argument("-o", "--output_file",
required=False,
default=None,
type=str,
help="Output file name to save image to")
help="Output file to save image to.")
version_string = __version__
friendly_version_string = version_string if version_string else 'unknown'
parser.add_argument(
"-v", "--version",
action='version',
version='sksurgeryrendermodelslikecamera version '
version='sksurgeryrendermodleslikecamera version '
+ friendly_version_string)
args = parser.parse_args(args)
run_demo(args.image_file,
args.models,
args.extrinsic_matrix,
run_demo(args.models,
args.background,
args.intrinsic_matrix,
args.points_file,
args.model_to_world,
args.camera_to_world,
args.left_to_right,
args.sigma,
args.clippingrange,
args.output_file)
# coding=utf-8
""" Demo app, to render a model from a particular perspective"""
"""
App to render a set of models using a calibrated camera.
"""
import sys
import cv2
# pylint: disable=import-error
import numpy as np
from PySide2 import QtWidgets
from sksurgeryvtk.widgets import vtk_overlay_window
from sksurgeryvtk.models import vtk_surface_model_directory_loader
from sksurgeryvtk.models import vtk_point_model
def run_demo(image_file, model_dir, extrinsics_file,
intrinsics_file, points_file, output_file):
""" Demo app, to render an image using a calibrated camera. """
import sksurgeryvtk.widgets.vtk_rendering_generator as rg
def split_string(param_string):
"""
Splits a comma separated list of rx,ry,rz,tx,ty,tz to a list of floats.
:param param_string: string
:return: list of float
"""
result = [0, 0, 0, 0, 0, 0]
if param_string is not None:
split = param_string.split(',')
if len(split) != 6:
raise ValueError('List is wrong length:' + str(split))
for i in range(6):
result[i] = float(split[i])
return result
def run_demo(models_file,
background_image,
intrinsic_file,
model_to_world,
camera_to_world,
left_to_right,
sigma,
clippingrange,
output_file):
""" Demo app, to render a set of models using a calibrated camera. """
app = QtWidgets.QApplication([])
vtk_widget = vtk_overlay_window.VTKOverlayWindow()
# Set the video image on the widget.
img = cv2.imread(image_file)
vtk_widget.set_video_image(img)
layout = QtWidgets.QVBoxLayout()
layout.setSpacing(0)
layout.setMargin(0)
layout.addWidget(vtk_widget)
window = QtWidgets.QWidget()
window.resize(img.shape[1], img.shape[0])
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:
intrinsics = np.loadtxt(intrinsics_file, dtype=np.float)
extrinsics = np.loadtxt(extrinsics_file)
vtk_widget.set_camera_matrix(intrinsics)
vtk_widget.set_camera_pose(extrinsics)
m2w = split_string(model_to_world)
c2w = split_string(camera_to_world)
l2r = split_string(left_to_right)
clip = clippingrange.split(',')
if len(clip) != 2:
raise ValueError("Clipping range not valid:" + str(clip))
gen = rg.VTKRenderingGenerator(models_file,
background_image,
intrinsic_file,
m2w,
c2w,
l2r,
sigma
)
gen.set_clipping_range(float(clip[0]), float(clip[1]))
gen.show()
img = gen.get_image()
if output_file:
vtk_widget.save_scene_to_file(output_file)
return 0
bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite(output_file, bgr)
else:
sys.exit(app.exec_())
return sys.exit(app.exec_())
return 0
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