def test_drr_registration(): import numpy as np from PIL import Image from camera import Camera from raybox import RayBox from drr_set import DrrSet from drr_registration import DrrRegistration from utils import read_rho, str_to_mat xray1 = Image.open('Test_Data/Sawbones_L2L3/0.bmp').convert('L') xray2 = Image.open('Test_Data/Sawbones_L2L3/90.bmp').convert('L') xray1 = np.array(xray1).astype(np.float32) xray2 = np.array(xray2).astype(np.float32) xray1 = (xray1-xray1.min())/(xray1.max()-xray1.min()) xray2 = (xray2-xray2.min())/(xray2.max()-xray2.min()) m1 = str_to_mat('[-0.785341, -0.068020, -0.615313, -5.901115; 0.559239, 0.348323, -0.752279, -4.000824; 0.265498, -0.934903, -0.235514, -663.099792]') m2 = str_to_mat('[-0.214846, 0.964454, 0.153853, 12.792526; 0.557581, 0.250463, -0.791436, -6.176056; -0.801838, -0.084251, -0.591572, -627.625305]') k1 = str_to_mat('[3510.918213, 0.000000, 368.718994; 0.000000, 3511.775635, 398.527802; 0.000000, 0.000000, 1.000000]') k2 = str_to_mat('[3533.860352, 0.000000, 391.703888; 0.000000, 3534.903809, 395.485229; 0.000000, 0.000000, 1.000000]') cam1, cam2 = Camera(m1, k1), Camera(m2, k2) raybox = RayBox('cpu') rho, sp = read_rho('Test_Data/Sawbones_L2L3/sawbones.nii.gz') raybox.set_rho(rho, sp) drr_set = DrrSet(cam1, cam2, raybox) drr_registration = DrrRegistration(xray1, xray2, drr_set) res = drr_registration.register(np.array([-98.92, -106.0, -185.0, -35.0, 25.0, 175])) print(res)
def init_cams_from_path(self, fpaths): cams = [] for fpath in fpaths: with open(fpath) as f: s = f.read() m = str_to_mat(re.search('[Mm]\s*=\s*\[(.*)\]', s).group(1)) k = str_to_mat(re.search('[Kk]\s*=\s*\[(.*)\]', s).group(1)) h = int(re.search('[Hh]\s*=\s*([0-9]+)', s).group(1)) w = int(re.search('[Ww]\s*=\s*([0-9]+)', s).group(1)) cams.append(Camera(m=m, k=k, h=h, w=w)) # TODO: Add to drr_set self.drr_set.set_cams(*cams) print('Set cams')
def test_camera_set_tfm_fixed(): import time from camera import Camera from camera_set import CameraSet from utils import str_to_mat import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d.art3d import Poly3DCollection m1 = str_to_mat('[-0.785341, -0.068020, -0.615313, -5.901115; 0.559239, 0.348323, -0.752279, -4.000824; 0.265498, -0.934903, -0.235514, -663.099792]') m2 = str_to_mat('[-0.214846, 0.964454, 0.153853, 12.792526; 0.557581, 0.250463, -0.791436, -6.176056; -0.801838, -0.084251, -0.591572, -627.625305]') k1 = str_to_mat('[3510.918213, 0.000000, 368.718994; 0.000000, 3511.775635, 398.527802; 0.000000, 0.000000, 1.000000]') k2 = str_to_mat('[3533.860352, 0.000000, 391.703888; 0.000000, 3534.903809, 395.485229; 0.000000, 0.000000, 1.000000]') cam1 = Camera(m=m1, k=k1) cam2 = Camera(m=m2, k=k2) camset = CameraSet(cam1, cam2) center = camset.center # Z rotations, i.e PSI from 0 to 360 degrees plt.ion() fig = plt.figure() ax = fig.gca(projection='3d') for rz in range(-180, 180, 5): params = [0, 0, 0, 0, 0, rz] camset.set_tfm_params(*params) cam1._make_cam_plot_fixed(fig, ax, '1') cam2._make_cam_plot_fixed(fig, ax, '2') ax.text(center[0], center[1], center[2], 'Z rotation') r = camset.tfm[:3, :3] u, v ,w = 100*r[0, :], 100*r[1, :], 100*r[2, :] ax.plot3D([center[0], center[0] + u[0]], [center[1], center[1] + u[1]], [center[2], center[2] + u[2]], color='red') ax.plot3D([center[0], center[0] + v[0]], [center[1], center[1] + v[1]], [center[2], center[2] + v[2]], color='green') ax.plot3D([center[0], center[0] + w[0]], [center[1], center[1] + w[1]], [center[2], center[2] + w[2]], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for ry in range(-180, 180, 5): params = [0, 0, 0, 0, ry, 0] camset.set_tfm_params(*params) cam1._make_cam_plot_fixed(fig, ax, '1') cam2._make_cam_plot_fixed(fig, ax, '2') ax.text(center[0], center[1], center[2], 'Y rotation') r = camset.tfm[:3, :3] u, v ,w = 100*r[0, :], 100*r[1, :], 100*r[2, :] ax.plot3D([center[0], center[0] + u[0]], [center[1], center[1] + u[1]], [center[2], center[2] + u[2]], color='red') ax.plot3D([center[0], center[0] + v[0]], [center[1], center[1] + v[1]], [center[2], center[2] + v[2]], color='green') ax.plot3D([center[0], center[0] + w[0]], [center[1], center[1] + w[1]], [center[2], center[2] + w[2]], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for rx in range(-180, 180, 5): params = [0, 0, 0, rx, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot_fixed(fig, ax, '1') cam2._make_cam_plot_fixed(fig, ax, '2') ax.text(center[0], center[1], center[2], 'X rotation') r = camset.tfm[:3, :3] u, v ,w = 100*r[0, :], 100*r[1, :], 100*r[2, :] ax.plot3D([center[0], center[0] + u[0]], [center[1], center[1] + u[1]], [center[2], center[2] + u[2]], color='red') ax.plot3D([center[0], center[0] + v[0]], [center[1], center[1] + v[1]], [center[2], center[2] + v[2]], color='green') ax.plot3D([center[0], center[0] + w[0]], [center[1], center[1] + w[1]], [center[2], center[2] + w[2]], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for tx in range(-100, 100, 5): params = [tx, 0, 0, 0, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot_fixed(fig, ax, '1') cam2._make_cam_plot_fixed(fig, ax, '2') r = camset.tfm[:3, :3] u, v, w = 100*r[0, :], 100*r[1, :], 100*r[2, :] tx, ty, tz = camset.tfm[:3, 3] ax.text(center[0] + tx, center[1] + ty, center[2] + tz, 'X translation') ax.plot3D([center[0] + tx, center[0] + tx + u[0]], [center[1] + ty, center[1] + ty + u[1]], [center[2] + tz, center[2] + tz + u[2]], color='red') ax.plot3D([center[0] + tx, center[0] + tx + v[0]], [center[1] + ty, center[1] + ty + v[1]], [center[2] + tz, center[2] + tz + v[2]], color='green') ax.plot3D([center[0] + tx, center[0] + tx + w[0]], [center[1] + ty, center[1] + ty + w[1]], [center[2] + tz, center[2] + tz + w[2]], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for ty in range(-100, 100, 5): params = [0, ty, 0, 0, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot_fixed(fig, ax, '1') cam2._make_cam_plot_fixed(fig, ax, '2') r = camset.tfm[:3, :3] u, v, w = 100*r[0, :], 100*r[1, :], 100*r[2, :] tx, ty, tz = camset.tfm[:3, 3] ax.text(center[0] + tx, center[1] + ty, center[2] + tz, 'Y translation') ax.plot3D([center[0] + tx, center[0] + tx + u[0]], [center[1] + ty, center[1] + ty + u[1]], [center[2] + tz, center[2] + tz + u[2]], color='red') ax.plot3D([center[0] + tx, center[0] + tx + v[0]], [center[1] + ty, center[1] + ty + v[1]], [center[2] + tz, center[2] + tz + v[2]], color='green') ax.plot3D([center[0] + tx, center[0] + tx + w[0]], [center[1] + ty, center[1] + ty + w[1]], [center[2] + tz, center[2] + tz + w[2]], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for tz in range(-100, 100, 5): params = [0, 0, tz, 0, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot_fixed(fig, ax, '1') cam2._make_cam_plot_fixed(fig, ax, '2') r = camset.tfm[:3, :3] u, v, w = 100*r[0, :], 100*r[1, :], 100*r[2, :] tx, ty, tz = camset.tfm[:3, 3] ax.text(center[0] + tx, center[1] + ty, center[2] + tz, 'Z translation') ax.plot3D([center[0] + tx, center[0] + tx + u[0]], [center[1] + ty, center[1] + ty + u[1]], [center[2] + tz, center[2] + tz + u[2]], color='red') ax.plot3D([center[0] + tx, center[0] + tx + v[0]], [center[1] + ty, center[1] + ty + v[1]], [center[2] + tz, center[2] + tz + v[2]], color='green') ax.plot3D([center[0] + tx, center[0] + tx + w[0]], [center[1] + ty, center[1] + ty + w[1]], [center[2] + tz, center[2] + tz + w[2]], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear()
def test_camera_set_tfm(): import time import numpy as np from camera import Camera from camera_set import CameraSet from utils import str_to_mat import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d.art3d import Poly3DCollection m1 = str_to_mat('[-0.785341, -0.068020, -0.615313, -5.901115; 0.559239, 0.348323, -0.752279, -4.000824; 0.265498, -0.934903, -0.235514, -663.099792]') m2 = str_to_mat('[-0.214846, 0.964454, 0.153853, 12.792526; 0.557581, 0.250463, -0.791436, -6.176056; -0.801838, -0.084251, -0.591572, -627.625305]') k1 = str_to_mat('[3510.918213, 0.000000, 368.718994; 0.000000, 3511.775635, 398.527802; 0.000000, 0.000000, 1.000000]') k2 = str_to_mat('[3533.860352, 0.000000, 391.703888; 0.000000, 3534.903809, 395.485229; 0.000000, 0.000000, 1.000000]') cam1 = Camera(m=m1, k=k1) cam2 = Camera(m=m2, k=k2) camset = CameraSet(cam1, cam2) center = camset.center # Z rotations, i.e PSI from 0 to 360 degrees plt.ion() fig = plt.figure() ax = fig.gca(projection='3d') for rz in range(-180, 180, 5): params = [0, 0, 0, 0, 0, rz] camset.set_tfm_params(*params) cam1._make_cam_plot(fig, ax, '1') cam2._make_cam_plot(fig, ax, '2') ax.text(center[0], center[1], center[2], 'Z rotation') ax.plot3D([center[0], center[0] + 100], [center[1], center[1] + 0], [center[2], center[2] + 0], color='red') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 100], [center[2], center[2] + 0], color='green') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 0], [center[2], center[2] + 100], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) p1 = cam1.pos p2 = cam2.pos c = camset.center verts = [c-(p1+p2), c-(p1-p2), c+(p1+p2), c+(p1-p2)] verts = [list(map(lambda x: x.tolist(), verts))] parallelogram = Poly3DCollection(verts) ax.add_collection3d(parallelogram, zs='z') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for ry in range(-180, 180, 5): params = [0, 0, 0, 0, ry, 0] camset.set_tfm_params(*params) cam1._make_cam_plot(fig, ax, '1') cam2._make_cam_plot(fig, ax, '2') ax.text(center[0], center[1], center[2], 'Y rotation') ax.plot3D([center[0], center[0] + 100], [center[1], center[1] + 0], [center[2], center[2] + 0], color='red') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 100], [center[2], center[2] + 0], color='green') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 0], [center[2], center[2] + 100], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) p1 = cam1.pos p2 = cam2.pos c = camset.center verts = [c-(p1+p2), c-(p1-p2), c+(p1+p2), c+(p1-p2)] verts = [list(map(lambda x: x.tolist(), verts))] parallelogram = Poly3DCollection(verts) ax.add_collection3d(parallelogram, zs='z') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for rx in range(-180, 180, 5): params = [0, 0, 0, rx, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot(fig, ax, '1') cam2._make_cam_plot(fig, ax, '2') ax.text(center[0], center[1], center[2], 'X rotation') ax.plot3D([center[0], center[0] + 100], [center[1], center[1] + 0], [center[2], center[2] + 0], color='red') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 100], [center[2], center[2] + 0], color='green') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 0], [center[2], center[2] + 100], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) p1 = cam1.pos p2 = cam2.pos c = camset.center verts = [c-(p1+p2), c-(p1-p2), c+(p1+p2), c+(p1-p2)] verts = [list(map(lambda x: x.tolist(), verts))] parallelogram = Poly3DCollection(verts) ax.add_collection3d(parallelogram, zs='z') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.001) ax.clear() for tx in range(-100, 100, 5): params = [tx, 0, 0, 0, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot(fig, ax, '1') cam2._make_cam_plot(fig, ax, '2') ax.text(center[0], center[1], center[2], 'X translation') ax.plot3D([center[0], center[0] + 100], [center[1], center[1] + 0], [center[2], center[2] + 0], color='red') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 100], [center[2], center[2] + 0], color='green') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 0], [center[2], center[2] + 100], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) c = camset.center t_c = np.linalg.inv(camset.tfm).dot(c.tolist() + [1])[:3] ax.plot3D([t_c[0]], [t_c[1]], [t_c[2]], marker='*', color='red') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.01) ax.clear() for ty in range(-100, 100, 5): params = [0, ty, 0, 0, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot(fig, ax, '1') cam2._make_cam_plot(fig, ax, '2') ax.text(center[0], center[1], center[2], 'Y translation') ax.plot3D([center[0], center[0] + 100], [center[1], center[1] + 0], [center[2], center[2] + 0], color='red') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 100], [center[2], center[2] + 0], color='green') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 0], [center[2], center[2] + 100], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) c = camset.center t_c = np.linalg.inv(camset.tfm).dot(c.tolist() + [1])[:3] ax.plot3D([t_c[0]], [t_c[1]], [t_c[2]], marker='*', color='red') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.01) ax.clear() for tz in range(-100, 100, 5): params = [0, 0, tz, 0, 0, 0] camset.set_tfm_params(*params) cam1._make_cam_plot(fig, ax, '1') cam2._make_cam_plot(fig, ax, '2') ax.text(center[0], center[1], center[2], 'Z translation') ax.plot3D([center[0], center[0] + 100], [center[1], center[1] + 0], [center[2], center[2] + 0], color='red') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 100], [center[2], center[2] + 0], color='green') ax.plot3D([center[0], center[0] + 0], [center[1], center[1] + 0], [center[2], center[2] + 100], color='blue') ax.set_xlim3d(center[0] - 600, center[0] + 600) ax.set_ylim3d(center[1] - 600, center[1] + 600) ax.set_zlim3d(center[2] - 600, center[2] + 600) c = camset.center t_c = np.linalg.inv(camset.tfm).dot(c.tolist() + [1])[:3] ax.plot3D([t_c[0]], [t_c[1]], [t_c[2]], marker='*', color='red') fig.canvas.draw() fig.canvas.flush_events() plt.pause(0.01) ax.clear()
def patch_to_pts(patch): if patch is not '': patch = str_to_mat(patch).T return np.vstack((patch, np.ones((1, patch.shape[1])))) else: return None
help='Second params file') parser.add_argument('--drr_dir', type=str, required=True, help='Directory with DRRs') parser.add_argument('--prefix', type=str, default='', help='Prefix for images') parser.add_argument('--patch', type=str, default='', help='3D Patch to project on 2D') parser.add_argument('--carm_dir', type=str, required=True, help='Directory with Carm images') parser.add_argument('--results_dir', type=str, required=True, help='Results dir') args = parser.parse_args() camera_set = CameraSet() with open(args.camera1, 'r') as f: s = f.read() m1 = str_to_mat(re.search('[Mm]\s*=\s*\[(.*)\]', s).group(1)) k1 = str_to_mat(re.search('[Kk]\s*=\s*\[(.*)\]', s).group(1)) h1 = int(re.search('[Hh]\s*=\s*([0-9]+)', s).group(1)) w1 = int(re.search('[Ww]\s*=\s*([0-9]+)', s).group(1)) cam1 = Camera(m=m1, k=k1, h=h1, w=w1) with open(args.camera2, 'r') as f: s = f.read() m2 = str_to_mat(re.search('[Mm]\s*=\s*\[(.*)\]', s).group(1)) k2 = str_to_mat(re.search('[Kk]\s*=\s*\[(.*)\]', s).group(1)) h2 = int(re.search('[Hh]\s*=\s*([0-9]+)', s).group(1)) w2 = int(re.search('[Ww]\s*=\s*([0-9]+)', s).group(1)) cam2 = Camera(m=m2, k=k2, h=h2, w=w2) with open(args.camera3, 'r') as f: s = f.read() m3 = str_to_mat(re.search('[Mm]\s*=\s*\[(.*)\]', s).group(1)) k3 = str_to_mat(re.search('[Kk]\s*=\s*\[(.*)\]', s).group(1))