def getNormalizedPoints(pts): centroid = np.mean(pts, axis=1) trans_mat = getTranslationMatrix(-centroid[0], -centroid[1]) trans_mat_inv = getTranslationMatrix(centroid[0], centroid[1]) trans_pts = util.dehomogenize(trans_mat * util.homogenize(pts)) mean_dist = np.mean(np.sqrt(np.sum(np.square(trans_pts), axis=1))) if mean_dist == 0: print 'Error in getNormalizedPoints:: mean distance between the given points is zero: ', pts sys.exit() norm_scale = math.sqrt(2) / mean_dist scale_mat = getScalingMatrix(norm_scale - 1) scale_mat_inv = getScalingMatrix(1.0 / norm_scale - 1) norm_pts = util.dehomogenize(scale_mat * trans_mat * util.homogenize(pts)) norm_mat_inv = trans_mat_inv * scale_mat_inv # print 'pts:\n', pts # print 'centroid:\n', centroid # print 'trans_mat:\n', trans_mat # print 'trans_mat_inv:\n', trans_mat_inv # print 'trans_pts:\n', trans_pts # print 'mean_dist:', mean_dist # print 'norm_scale:', norm_scale # print 'scale_mat:\n', scale_mat # print 'scale_mat_inv:\n', scale_mat_inv # print 'norm_mat_inv:\n', norm_mat_inv return norm_pts, norm_mat_inv
def shiftGeometricCenterToOrigin(corners): geom_center_x = np.mean(corners[0, :]) geom_center_y = np.mean(corners[1, :]) trans_mat = np.mat([[1, 0, -geom_center_x], [0, 1, -geom_center_y], [0, 0, 1]]) trans_mat_inv = np.mat([[1, 0, geom_center_x], [0, 1, geom_center_y], [0, 0, 1]]) shifted_corners = util.dehomogenize(trans_mat * util.homogenize(corners)) return (shifted_corners, trans_mat_inv)
def getSSDProj(params, in_pts, out_pts, pts_size): # print 'getSSDShear:: params:', params params_len = len(params) # print 'getSSDShear:: params_len:', params_len if params_len < 2: # print'getSSDShear:: Invalid params provided' params = params[0] # sys.exit() proj_mat = getProjectionMatrix(params[0], params[1]) rec_pts = util.homogenize(util.dehomogenize(proj_mat * in_pts)) rec_error = np.sum(np.square(rec_pts - out_pts)) / pts_size return rec_error
def shiftProjectiveCenterToOrigin(corners): x1 = corners[0, 0] y1 = corners[1, 0] x2 = corners[0, 1] y2 = corners[1, 1] x3 = corners[0, 2] y3 = corners[1, 2] x4 = corners[0, 3] y4 = corners[1, 3] m1 = (y1 - y3) / (x1 - x3) b1 = y1 - m1 * x1 m2 = (y2 - y4) / (x2 - x4) b2 = y2 - m2 * x2 proj_center_x = -(b1 - b2) / (m1 - m2) proj_center_y = m1 * proj_center_x + b1 trans_mat = np.mat([[1, 0, -proj_center_x], [0, 1, -proj_center_y], [0, 0, 1]]) trans_mat_inv = np.mat([[1, 0, proj_center_x], [0, 1, proj_center_y], [0, 0, 1]]) shifted_corners = util.dehomogenize(trans_mat * util.homogenize(corners)) return (shifted_corners, trans_mat_inv)
read_from_vid = 0 # img_name_fmt='img%03d.jpg' img_name_fmt = 'frame%05d.jpg' vid_fmt = 'avi' if end_id < start_id: end_id = start_id if actor_id >= 0: seq_ids = range(start_id, end_id + 1) else: seq_ids = range(start_id, start_id + 1) std_pts, std_corners = getNormalizedUnitSquarePts(grid_res_x, grid_res_y, 0.5) std_pts_hm = util.homogenize(std_pts) for seq_id in seq_ids: # mtf_sm_id = param_ids['mtf_sm_id'] # mtf_am_id = param_ids['mtf_am_id'] # mtf_ssm_id = param_ids['mtf_ssm_id'] # init_identity_warp = param_ids['init_identity_warp'] if actor_id >= 0: actor = actors[actor_id] seq_name = sequences[actor][seq_id] # mtf_sm = mtf_sms[mtf_sm_id] # mtf_am = mtf_ams[mtf_am_id] # mtf_ssm = mtf_ssms[mtf_ssm_id] print 'actor: ', actor
ground_truth_fname = db_root_dir + '/' + actor + '/' + seq_name + '.txt' ground_truth = readTrackingData(ground_truth_fname) no_of_frames = ground_truth.shape[0] print 'no_of_frames: ', no_of_frames end_id = no_of_frames init_corners = np.asarray([ground_truth[0, 0:2].tolist(), ground_truth[0, 2:4].tolist(), ground_truth[0, 4:6].tolist(), ground_truth[0, 6:8].tolist()]).T std_pts, std_corners = getNormalizedUnitSquarePts(std_resx, std_resy, 0.5) std_pts_hm = util.homogenize(std_pts) (init_corners_norm, init_norm_mat) = getNormalizedPoints(init_corners) init_hom_mat = np.mat(util.compute_homography(std_corners, init_corners_norm)) init_pts_norm = util.dehomogenize(init_hom_mat * std_pts_hm) init_pts = util.dehomogenize(init_norm_mat * util.homogenize(init_pts_norm)) # ret, init_img = cap.read() init_img = cv2.imread(src_folder + '/frame{:05d}.jpg'.format(1)) if init_img is None: raise StandardError( 'The initial image could not be read from the file:\n' + src_folder + '/frame{:05d}.jpg'.format(1)) init_img_gs = cv2.cvtColor(init_img, cv2.cv.CV_BGR2GRAY).astype(np.float64) if filter_type != 'none': init_img_gs = applyFilter(init_img_gs, filter_type, kernel_size) if write_img_data: init_img_gs.astype(np.uint8).tofile(img_root_dir + '/' + 'frame_0_gs.bin')
[[1, 0, dx], [0, 1, dy], [0, 0, 1]] ) # print 'dx: ', dx # print 'dy: ', dy # print 'dxx: ', dxx # print 'dxy: ', dxy # print 'dyx: ', dyx # print 'dyy: ', dyy # print 'affine_mat:\n', affine_mat # print 'rt_mat:\n', rt_mat # print 'trans_mat:\n', trans_mat base_corners_hm = util.homogenize(base_corners) # print 'base_corners_hm: ', base_corners_hm hom_corners_hm = np.mat(H) * np.mat(base_corners_hm) hom_corners = util.dehomogenize(hom_corners_hm) hom_error = math.sqrt(np.sum(np.square(hom_corners - current_corners)) / 4) hom_errors.append(hom_error) affine_corners_hm = np.mat(affine_mat) * np.mat(base_corners_hm) affine_corners = util.dehomogenize(affine_corners_hm) affine_error = math.sqrt(np.sum(np.square(affine_corners - current_corners)) / 4) affine_errors.append(affine_error) comp_corners_hm = np.mat(P) * np.mat(affine_mat) * np.mat(base_corners_hm) comp_corners = util.dehomogenize(comp_corners_hm)
continue gt_hom_file = open(gt_hom_fname, 'r') gt_hom_lines = gt_hom_file.readlines() gt_hom_file.close() gt_pts_file = open(gt_pts_fname, 'r') gt_pts_lines = gt_pts_file.readlines() gt_pts_file.close() words = gt_pts_lines[0].rstrip().split() if len(words) != 8: raise StandardError('invalid formatting on first line of line of points gt file : {:s}'.format(init_location)) corners, idx = arrangeCorners(words) init_location = np.matrix(idx) init_location_hom = homogenize(init_location) # print 'init_location: ', init_location # print 'init_location_hom: ', init_location_hom print 'Ground truth file has {:d} lines'.format(len(gt_hom_lines)) dst_file = open(dst_fname, 'w') dst_file.write('frame ulx uly urx ury lrx lry llx lly \n') curr_location_list =[] line_id = 0 for line in gt_pts_lines: words = line.rstrip().split() # if len(words) != 9: # raise StandardError('invalid formatting on line {:d} of homography gt file : {:s}'.format(
sel_corners_fid = open(sel_corners_fname, 'w') sel_corners_fid.write('frame ulx uly urx ury lrx lry llx lly \n') writeCorners(sel_corners_fid, init_corners_selected, init_frame_id + 1) cv2.namedWindow(gt_corners_window_name) prev_corners_gt = init_corners_gt.copy() prev_corners_selected = init_corners_selected.copy() for frame_id in xrange(init_frame_id + 1, no_of_frames): curr_corners_gt = np.asarray([ground_truth[frame_id, 0:2].tolist(), ground_truth[frame_id, 2:4].tolist(), ground_truth[frame_id, 4:6].tolist(), ground_truth[frame_id, 6:8].tolist()]).T curr_hom_gt = util.compute_homography(prev_corners_gt, curr_corners_gt) curr_corners_selected = util.dehomogenize(np.mat(curr_hom_gt) * np.mat(util.homogenize(prev_corners_selected))) curr_img = cv2.imread('{:s}/frame{:05d}.jpg'.format(src_dir, frame_id + 1)) if len(curr_img.shape) == 2: curr_img = cv2.cvtColor(curr_img, cv2.COLOR_GRAY2RGB) drawRegion(curr_img, curr_corners_selected, gt_col, gt_thickness, annotate_corners=True, annotation_col=annotation_col) fps_text = 'frame: {:4d}'.format(frame_id + 1) cv2.putText(curr_img, fps_text, (5, 15), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255)) cv2.imshow(gt_corners_window_name, curr_img) writeCorners(sel_corners_fid, curr_corners_selected, frame_id + 1)
pose_fname = db_root_dir + '/' + actor + '/' + seq_name + '/poseGroundtruth_new.txt' calibration_fname = db_root_dir + '/' + actor + '/' + seq_name + '/internalCalibrationMatrix_new.txt' pose_data = np.loadtxt(pose_fname, dtype=np.float64, delimiter='\t') calibration_matrix = np.mat( np.loadtxt(calibration_fname, dtype=np.float64, delimiter='\t')) print 'calibration_matrix:', calibration_matrix if pose_data.shape[1] != no_of_frames: raise StandardError( 'Size of pose data does not match no. of frames in the sequence') sel_pts = getTrackingObject2(init_img) init_corners = np.asarray(sel_pts).astype(np.float64).T print 'init_corners:', init_corners init_corners_hm = np.mat(util.homogenize(init_corners)) print 'init_corners_hm:', init_corners_hm from_last_frame = True pause_seq = True gt_frames = no_of_frames init_rot_mat = np.mat(np.reshape(pose_data[:9, 0], (3, 3)).transpose()) init_trans_mat = np.mat(pose_data[9:, 0]) init_trans_mat = init_trans_mat.transpose() print 'init_rot_mat:\n', init_rot_mat print 'init_trans_mat\n:', init_trans_mat init_trans_mat_tiled = np.tile(init_trans_mat, 4) print 'init_trans_mat_tiled\n:', init_trans_mat_tiled
print 'hist_type: ', hist_type n_bins_vec = np.array(range(min_bins, max_bins + 1), dtype=np.uint32) n_bins_size = n_bins_vec.size src_folder = db_root_path + '/' + actor + '/' + seq_name ground_truth_fname = db_root_path + '/' + actor + '/' + seq_name + '.txt' ground_truth = readTrackingData(ground_truth_fname) no_of_frames = ground_truth.shape[0] print 'no_of_frames: ', no_of_frames frame_id2_vec=range(2, no_of_frames) std_pts, std_corners = getNormalizedUnitSquarePts(std_resx, std_resy) std_pts_hm = util.homogenize(std_pts) std_corners_hm = util.homogenize(std_corners) corners = np.asarray([ground_truth[frame_id1 - 1, 0:2].tolist(), ground_truth[frame_id1 - 1, 2:4].tolist(), ground_truth[frame_id1 - 1, 4:6].tolist(), ground_truth[frame_id1 - 1, 6:8].tolist()]).T (corners_norm, norm_mat) = getNormalizedPoints(corners) hom_mat = np.mat(util.compute_homography(std_corners, corners_norm)) pts_norm = util.dehomogenize(hom_mat * std_pts_hm) pts = util.dehomogenize(norm_mat * util.homogenize(pts_norm)) img = cv2.imread(src_folder + '/frame{:05d}.jpg'.format(frame_id1)) img_gs = cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY).astype(np.float64) corners2 = np.asarray([ground_truth[frame_id2 - 1, 0:2].tolist(),