# size_y = int((abs(templ_corners[1, 3] - templ_corners[1, 0]) + # abs(templ_corners[1, 2] - templ_corners[1, 1])) / 2) print 'Object size: {:d}x{:d}'.format(size_x, size_y) if res_from_size: res_x = int(size_x / resize_factor) res_y = int(size_y / resize_factor) n_pts = res_x * res_y patch_size_x = res_x * resize_factor patch_size_y = res_y * resize_factor std_pts, std_corners = getNormalizedUnitSquarePts(res_x, res_y, 0.5) std_pts_hm = util.homogenize(std_pts) init_hom_mat = np.mat(util.compute_homography(std_corners, init_corners)) init_pts = util.dehomogenize(init_hom_mat * std_pts_hm) init_img_gs = cv2.cvtColor(init_img, cv2.cv.CV_BGR2GRAY).astype(np.float64) img_height, img_width = init_img_gs.shape init_pixel_vals = np.mat([util.bilin_interp(init_img_gs, init_pts[0, pt_id], init_pts[1, pt_id]) for pt_id in xrange(n_pts)]) if init_frame_id == 0: show_init = 0 if show_patches: init_patch = np.reshape(init_pixel_vals, (res_y, res_x)).astype(np.uint8) init_patch_resized = cv2.resize(init_patch, (patch_size_x, patch_size_y)) drawRegion(init_img, init_corners, gt_col, 1, annotate_corners=False) if show_init:
failure_font_thickness, legend_font_line_type) curr_img_list.append(new_img) else: cv2.putText( curr_img, 'Tracker Failed', (curr_img.shape[0] / 2, curr_img.shape[1] / 2), legend_font_face, failure_font_size, col_rgb['red'], failure_font_thickness, legend_font_line_type) continue if show_grid: try: curr_hom_mat = np.mat( util.compute_homography(std_corners, curr_corners)) except: pass curr_pts = util.dehomogenize(curr_hom_mat * std_pts_hm) if show_stacked: new_img = np.copy(curr_img) if show_legend: cv2.rectangle(new_img, (legend_x, legend_y + baseline), (legend_x + text_size[0], legend_y - text_size[1] - baseline), legend_bkg_col, -1) cv2.putText(new_img, tracker['legend'], (legend_x, legend_y), legend_font_face, legend_font_size, col_rgb[tracker['col']], legend_font_thickness,
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') init_pixel_vals = np.array([util.bilin_interp(init_img_gs, init_pts[0, pt_id], init_pts[1, pt_id]) for pt_id in
affine_col = (0, 255, 0) rt_col = (255, 0, 0) trans_col = (255, 255, 255) comp_col = (255, 255, 0) hom_errors = [] affine_errors = [] rt_errors = [] trans_errors = [] for i in xrange(1, no_of_frames): current_corners = np.asarray([ground_truth[i, 0:2].tolist(), ground_truth[i, 2:4].tolist(), ground_truth[i, 4:6].tolist(), ground_truth[i, 6:8].tolist()]).T H = util.compute_homography(base_corners, current_corners) H_inv = np.linalg.inv(np.mat(H)) # H_inv=H_inv/H_inv[2, 2] v1=H_inv[2, 0] v2=H_inv[2, 1] u=H_inv[2, 2] dx = H_inv[0, 2] dy = H_inv[1, 2] dxx = H_inv[0, 0] - H_inv[2, 0] * H_inv[0, 2] - 1 dxy = H_inv[0, 1] - H_inv[2, 1] * H_inv[0, 2] dyx = H_inv[1, 0] - H_inv[2, 0] * H_inv[1, 2] dyy = H_inv[1, 1] - H_inv[2, 1] * H_inv[1, 2] - 1
patch_start_x = img_width - patch_size_x patch_start_y = 0 patch_end_x = img_width patch_end_y = patch_size_y if convert_to_gs: if len(curr_img.shape) == 3: curr_img = cv2.cvtColor(curr_img, cv2.COLOR_RGB2GRAY) n_channels = 3 if len(curr_img.shape) == 2: curr_img = cv2.cvtColor(curr_img, cv2.COLOR_GRAY2RGB) if np.isnan(curr_corners).any(): print 'curr_corners:\n', curr_corners raise StandardError('Gound truth for frame {:d} contains NaN'.format(frame_id)) curr_hom_mat = np.mat(util.compute_homography(std_corners, curr_corners)) curr_pts = util.dehomogenize(curr_hom_mat * std_pts_hm) curr_pts_ss = util.dehomogenize(curr_hom_mat * std_pts_ss_hm) if show_patch: curr_pixel_vals = getPixValsRGB(curr_pts, curr_img.astype(np.float64)) # print 'curr_pixel_vals: ', curr_pixel_vals n_channels = curr_pixel_vals.shape[1] for channel_id in xrange(n_channels): curr_patch = np.reshape(curr_pixel_vals[:, channel_id], (grid_res_y, grid_res_x)).astype(np.uint8) # print 'curr_patch: ', curr_patch curr_patch_resized = cv2.resize(curr_patch, (patch_size_x, patch_size_y)) try: curr_img[patch_start_y:patch_end_y, patch_start_x:patch_end_x, channel_id] = curr_patch_resized except IndexError: curr_img[patch_start_y:patch_end_y, patch_start_x:patch_end_x] = curr_patch_resized
init_corners_selected = np.array(init_corners_selected, dtype=np.float64).T 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)
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(), ground_truth[frame_id2 - 1, 2:4].tolist(), ground_truth[frame_id2 - 1, 4:6].tolist(), ground_truth[frame_id2 - 1, 6:8].tolist()]).T (corners2_norm, norm_mat) = getNormalizedPoints(corners2) hom_mat = np.mat(util.compute_homography(std_corners, corners2_norm)) pts2_norm = util.dehomogenize(hom_mat * std_pts_hm) pts2 = util.dehomogenize(norm_mat * util.homogenize(pts2_norm))