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: init_patch_win_name = 'Initial Patch' cv2.namedWindow(init_patch_win_name) cv2.imshow(init_patch_win_name, init_patch_resized) init_img_win_name = 'Initial Image' cv2.namedWindow(init_img_win_name) cv2.imshow(init_img_win_name, init_img) templ_fname = '{:s}/frame{:05d}.jpg'.format(src_dir, 1) templ_img = cv2.imread(templ_fname) if len(templ_img.shape) == 2: templ_img = cv2.cvtColor(templ_img, cv2.COLOR_GRAY2RGB) templ_img_gs = cv2.cvtColor(templ_img, cv2.cv.CV_BGR2GRAY).astype(np.float64) templ_hom_mat = np.mat(util.compute_homography(std_corners, templ_corners)) templ_pts = util.dehomogenize(templ_hom_mat * std_pts_hm)
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, legend_font_line_type) if show_grid: drawGrid(new_img, curr_pts, grid_res_x, grid_res_y, col_rgb[tracker['col']], line_thickness) drawRegion(new_img, curr_corners, col_rgb[tracker['col']], line_thickness, annotate_corners, annotation_col, annotation_font_size) curr_img_list.append(new_img) else: if show_legend: cv2.rectangle(curr_img, (legend_x, legend_y + baseline), (legend_x + text_size[0], legend_y - text_size[1] - baseline), legend_bkg_col, -1) cv2.putText(curr_img, tracker['legend'], (legend_x, legend_y), legend_font_face, legend_font_size, col_rgb[tracker['col']], legend_font_thickness, legend_font_line_type) if show_grid:
cv2.setMouseCallback(gt_corners_window_name, mouseHandler, param=[annotated_img, temp_img, pts]) for i in xrange(no_of_frames): curr_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 ret, curr_img = cap.read() if not ret: print 'End of sequence reached unexpectedly' break drawRegion(curr_img, curr_corners, gt_col, 1) cv2.imshow(gt_corners_window_name, curr_img) key = cv2.waitKey(1 - pause_seq) if key == 27: break elif key == 32: pause_seq = 1 - pause_seq
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 else: if show_frame_id: fps_text = 'frame: {:4d}'.format(frame_id + 1) cv2.putText(curr_img, fps_text, (5, 15), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, gt_col) if show_grid: drawGrid(curr_img, curr_pts_ss, ss_grid_res_x, ss_grid_res_y, gt_col, gt_thickness) else: drawRegion(curr_img, curr_corners, gt_col, gt_thickness, label_corners) if show_grid_label_connector: p1 = (int(curr_corners[0, 0]), int(curr_corners[1, 0])) p2 = (int(curr_corners[0, 2]), int(curr_corners[1, 2])) cv2.line(curr_img, p1, conn_end_pt_1, conn_col, conn_thickness, legend_font_line_type) cv2.line(curr_img, p2, conn_end_pt_2, conn_col, conn_thickness, legend_font_line_type) if conn_all_corners: p1 = (int(curr_corners[0, 1]), int(curr_corners[1, 1])) p2 = (int(curr_corners[0, 3]), int(curr_corners[1, 3])) cv2.line(curr_img, p1, conn_end_pt_1, conn_col, conn_thickness, legend_font_line_type) cv2.line(curr_img, p2, conn_end_pt_2, conn_col, conn_thickness, legend_font_line_type)
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 curr_img = cv2.imread('{:s}/frame{:05d}.jpg'.format( src_dir, i + start_id + 1)) if curr_img is None: raise StandardError('End of sequence reached unexpectedly') 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(i)) drawRegion(curr_img, curr_corners, gt_col, gt_thickness) fps_text = 'frame: {:4d}'.format(i + 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) key = cv2.waitKey(1 - pause_seq) if key == 27: break elif key == 32: pause_seq = 1 - pause_seq cv2.destroyWindow(gt_corners_window_name)
col=line_col, title=win_title) # cv2.destroyWindow(win_title) if resize_vis_images: region = [(x / vis_resize_factor, y / vis_resize_factor) for x, y in region] for j in range(len(region)): region_fid.write('{:f}\t'.format(region[j][0])) region_fid.write('\n') for j in range(len(region)): region_fid.write('{:f}\t'.format(region[j][1])) region_fid.write('\n') corners = np.array(region, dtype=np.int32) cv2.fillConvexPoly(frame_mask, corners, fill_col) drawRegion(disp_frame, (corners * vis_resize_factor).transpose(), thickness=2, color=line_col) if read_from_file and show_img: win_title = 'Regions 1-{:d}. Press any key to continue'.format(i + 1) cv2.imshow(win_title, disp_frame) cv2.waitKey(0) cv2.destroyWindow(win_title) regions.append(region) region_fid.close() # frame_mask = cv2.cvtColor(frame_mask, cv2.COLOR_GRAY2RGB) if show_img:
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) if hom_from_prev: prev_corners_gt = curr_corners_gt.copy() prev_corners_selected = curr_corners_selected.copy() key = cv2.waitKey(1 - pause_seq) if key == 27: break elif key == 32: