def main(): frames = np.load('../data/sylvseq.npy') bases = np.load('../data/sylvbases.npy') fig, ax = plt.subplots(1) rect = np.array([101, 61, 155, 107]) template0 = frames[rect[1]:rect[3] + 1, rect[0]:rect[2] + 1, 0] rect_save = [] for i in range(frames.shape[-1] - 1): template = frames[rect[1]:rect[3] + 1, rect[0]:rect[2] + 1, i] p = LucasKanadeBasis.LucasKanadeBasis(template, frames[:, :, i + 1], rect, bases) #p = LucasKanade.LucasKanade(template,frames[:,:,i+1],rect,bases) rect = np.round(rect + np.vstack([p, p]).reshape(-1, )).astype(int) w = rect[2] - rect[0] h = rect[3] - rect[1] BB = patches.Rectangle((rect[0], rect[1]), w, h, linewidth=1, edgecolor='r', facecolor='none') ax.add_patch(BB) plt.imshow(frames[:, :, i + 1], cmap='gray') plt.pause(0.0001) rect_save.append(rect) if i in [1, 200, 300, 350, 400]: plt.savefig('sylvseq_' + str(i) + '.png') BB.remove() rect_save = np.vstack(rect_save) np.save('sylvseqrects.npy', rect_save)
ax = fig.add_subplot(111) ax.set_title("LucasKanade Tracking with Single Template") for i in range(data.shape[2] - 1): # iteration through all the frames. image = data[:, :, i] image2 = data[:, :, i + 1] p0 = np.array([0, 0]) #initial value for LK algorithm p0 = LucasKanade(image, image2, rect, p0) # output of the LK algorithm rect[0] = rect[0] + (p0[1]) #y rect[1] = rect[1] + (p0[0]) rect[2] = rect[2] + (p0[1]) rect[3] = rect[3] + (p0[0]) rectsArray[i, :] = rect width = rect[2] - rect[0] height = rect[3] - rect[1] p1 = LucasKanadeBasis(image, image2, rect1, bases) rect1[0] = rect1[0] + (p1[1]) #y rect1[1] = rect1[1] + (p1[0]) rect1[2] = rect1[2] + (p1[1]) rect1[3] = rect1[3] + (p1[0]) rectsArray1[i, :] = rect1 rectangle = patches.Rectangle((rect[0], rect[1]), width, height, linewidth=2, edgecolor='y', facecolor='none') rectangle1 = patches.Rectangle((rect1[0], rect1[1]), width, height, linewidth=2,
if __name__ == '__main__': result_dir = '../result/' if not os.path.exists(result_dir): os.makedirs(result_dir) cars_data = np.load('../data/sylvseq.npy') frame = cars_data[:, :, 0] rect_list = [] rect = np.array([101, 61, 155, 107]) rect2 = np.array([101, 61, 155, 107]) rect_list.append(rect) bases = np.load('../data/sylvbases.npy') for i in range(1, cars_data.shape[2]): next_frame = cars_data[:, :, i] # track with two trackers p = LucasKanadeBasis.LucasKanadeBasis(frame, next_frame, rect, bases) p2 = LucasKanade.LucasKanade(frame, next_frame, rect2) rect = [rect[0] + p[0], rect[1] + p[1], rect[2] + p[0], rect[3] + p[1]] rect_list.append(rect) rect2 = [ rect2[0] + p2[0], rect2[1] + p2[1], rect2[2] + p2[0], rect2[3] + p2[1] ] # show image tmp_img = np.zeros((next_frame.shape[0], next_frame.shape[1], 3)) tmp_img[:, :, 0] = next_frame tmp_img[:, :, 1] = next_frame tmp_img[:, :, 2] = next_frame cv2.rectangle(tmp_img, (int(round(rect2[0])), int(round(rect2[1]))),
import LucasKanadeBasis # write your script here, we recommend the above libraries for making your animation if __name__ == '__main__': sylvseq = np.load('../data/sylvseq.npy') imH, imW, frames = np.shape(sylvseq) print(frames) bases = np.load('../data/sylvbases.npy') rect = [101., 61., 155., 107.] rects = rect[:] for frame in range(frames - 1): print(frame) It = sylvseq[:, :, frame] It1 = sylvseq[:, :, frame + 1] p = LucasKanadeBasis.LucasKanadeBasis(It, It1, rect, bases) rect[0] += p[0] rect[2] += p[0] rect[1] += p[1] rect[3] += p[1] # print(rect) rects = np.vstack((rects, rect)) # # print(rects) # # plt.imshow(It1, cmap='gray') # # patch = patches.Rectangle((rect[0], rect[1]), (rect[2] - rect[0]), (rect[3] - rect[1]), edgecolor='y', # # facecolor='none') # # ax = plt.gca() # # ax.add_patch(patch) # # plt.show() # # if ((frame) % 100 ==99 or frame == 0): # # plt.imshow(It1,cmap = 'gray')
# write your script here, we recommend the above libraries for making your animation video_path = '../data/sylvseq.npy' base_path = '../data/sylvbases.npy' init_rect = [61.0, 101.0, 107.0, 155.0] frame_step = 1 data = np.load(video_path) frame_num = data.shape[2] bases = np.load(base_path) rect_list = [init_rect] for i in np.arange(1, frame_num, frame_step): # pdb.set_trace() img_pre = data[:, :, i - 1] img_cur = data[:, :, i] p1 = LucasKanadeBasis(img_pre, img_cur, init_rect, bases) init_rect = init_rect + np.array([p1[0], p1[1], p1[0], p1[1]]) rect_list.append(init_rect) print(p1) init_rect = [61.0, 101.0, 107.0, 155.0] rect_list_old = [init_rect] for i in np.arange(1, frame_num, frame_step): # pdb.set_trace() img_pre = data[:, :, i - 1] img_cur = data[:, :, i] p1 = LucasKanade(img_pre, img_cur, init_rect) init_rect = init_rect + np.array([p1[0], p1[1], p1[0], p1[1]]) rect_list_old.append(init_rect) print(p1)
# for i in range(2): # Print the the frame number in process print(i) # print(rect) # Load the template and the input images It = sylv_data[:, :, i] It1 = sylv_data[:, :, i + 1] p0 = np.zeros(2) # Choose to use appearance basis or not # ------------------------------------------------------------------------------------------------ p = LucasKanadeBasis.LucasKanadeBasis(It=It, It1=It1, rect=rect, bases=bases) # p = LucasKanade.LucasKanade(It = It, It1= It1, rect=rect, p0 = p0) # update p0, but only update the translation p0 = np.zeros(2) p0[0] = p[4] p0[1] = p[5] # tmplt_pts, warp_p = LucasKanadeBasis.init_affine(p0=p0, rect=rect) # Update square # -------------------------------------------------------------------------------- # Compose tranformation W = np.zeros((2, 3))