def rotate_phone(phone_pts, rotation_vector, camera_vector): ext = camera_utils.create_extrinsic_matrix(rotation_vector, camera_vector) new_pts = np.matmul(ext, phone_pts.T).T new_pts = camera_utils.back_2_3d(new_pts) obj_pts = camera_utils.print_obj_verts(new_pts) #new_pts_list = new_pts.tolist() #print obj_pts #print new_pts_list top_left = new_pts[0] bottom_right = new_pts[2] center_x = (top_left[0] + bottom_right[0]) / 2. center_y = (top_left[1] + bottom_right[1]) / 2. return obj_pts, new_pts, (center_x, center_y)
phone_pts = np.array([ [0.,0.,0., 1.], [0,-0.1509,0., 1.], [-0.0757 ,-0.1509 , 0., 1.], [-0.0757, 0., 0., 1.] ]) phone_pts = np.array([ [ 0.0550 , -0.0017 , -0.0640,1], [-0.0015 , -0.0518 , 0.0666,1], [-0.0550 , 0.0017 , 0.0640,1], [ 0.0015 , 0.0518 , -0.0666,1] ]) ext_matrix = camera_utils.create_extrinsic_matrix( rotation_vector, center_vector) int_matrix = camera_utils.create_intrinsic_matrix( fx, fy, width/2., height/2.) camera_matrix = np.matmul(int_matrix, ext_matrix) points_2d = np.matmul(camera_matrix, phone_pts.T).T w_coords = points_2d[:,2] points_2d = points_2d / w_coords[:,None] phone_pts = np.asarray(phone_pts[:,:2], dtype = np.float32) points_2d = np.asarray( points_2d[:,:2], dtype = np.float32) phone_pts = np.asarray([
def project_points(rotated_pts, center_vector, height, width, fx, fy, adversary_metadata, render_image_data, homography_im, display): reference_im = 'xr_.png' capture_im = render_image_data ones = np.ones(4)[np.newaxis].T rotated_pts = np.hstack((rotated_pts, ones)) ext_matrix = camera_utils.create_extrinsic_matrix(np.array([0., 180., 0.]), center_vector) int_matrix = camera_utils.create_intrinsic_matrix(fx, fy, width / 2., height / 2.) camera_matrix = np.matmul(int_matrix, ext_matrix) points_2d = np.matmul(camera_matrix, rotated_pts.T).T w_coords = points_2d[:, 2] points_2d = points_2d / w_coords[:, None] rotated_pts = np.asarray(rotated_pts[:, :2], dtype=np.float32) points_2d = np.asarray(points_2d[:, :2], dtype=np.float32) phone_pts = np.asarray([[0, 0], [0, 632], [312, 632], [312, 0]], dtype=np.float32) h, status = cv2.findHomography(points_2d, phone_pts) #im_src = cv2.imread(capture_im) #im_src = adjust_cv2_color(im_src) im_src = render_image_data im_dest = cv2.imread(reference_im) im_dest = adjust_cv2_color(im_dest) im_out = cv2.warpPerspective(im_src, h, (im_dest.shape[1], im_dest.shape[0]), flags=cv2.INTER_LINEAR) im_out = np.fliplr(im_out) #save image below #need to write metadata here!! #metadata = json.dumps(metadata) #tifffile.imsave('microscope.tif', data, description=metadata) #homography_im_tiff = homography_im.split('.')[0] + '.tif' #tifffile.imsave(homography_im_tiff, im_out, description=adversary_metadata) image_name = homography_im.split('/')[-1] csv_filename = "/".join( homography_im.split('/')[:len(homography_im.split('/')) - 1]) + '/metadata.csv' homography_IM = Image.fromarray(im_out) #homography_IM.show() print(homography_im) homography_IM.save(homography_im) write_meta_to_csv(csv_filename, image_name, adversary_metadata) if display: display_images([reference_im, homography_im, capture_im], points_2d)