def interpolate(img0, img1, levels, k_size, k_type, sigma, interpolation, border_mode): # Hierarchical Lucas-Kanade method forward and backward direction ufwd, vfwd = ps4.hierarchical_lk(img0, img1, levels, k_size, k_type, sigma, interpolation, border_mode) ubwd, vbwd = ps4.hierarchical_lk(img1, img0, levels, k_size, k_type, sigma, interpolation, border_mode) # Save the flows, including the first which is raw image #flowls = [ps4.warp(img0, -0.2*i*u, -0.2*i*v, interpolation, border_mode) for i in range(0,5)] flowlsfwd = [img0] flowlsbwd = [img1] for i in range(1, 3): flowlsfwd.append( ps4.warp(flowlsfwd[-1], -0.2 * ufwd, -0.2 * vfwd, interpolation, border_mode)) flowlsbwd.append( ps4.warp(flowlsbwd[-1], -0.2 * ubwd, -0.2 * vbwd, interpolation, border_mode)) #print(flowlsfwd[0].shape, flowlsbwd[0].shape) # Stack the image frames frames = np.vstack((np.hstack(flowlsfwd), np.hstack(flowlsbwd[::-1]))) return frames
def part_5b(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ mc01 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc01.png'), 0) / 255. mc02 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc02.png'), 0) / 255. mc03 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc03.png'), 0) / 255. # Optional: smooth the images if LK doesn't work well on raw images levels = 5 # TODO: Define the number of levels k_size = 20 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.hierarchical_lk(mc01, mc02, levels, k_size, k_type, sigma, interpolation, border_mode) # Flow image shift_02 = ps4.warp(mc02, 0.8 * u, 0.8 * v, interpolation, border_mode) shift_04 = ps4.warp(mc02, 0.6 * u, 0.6 * v, interpolation, border_mode) shift_06 = ps4.warp(mc02, 0.4 * u, 0.4 * v, interpolation, border_mode) shift_08 = ps4.warp(mc02, 0.2 * u, 0.2 * v, interpolation, border_mode) u_v = np.vstack((np.hstack( (mc01, shift_02, shift_04)), np.hstack((shift_06, shift_08, mc02)))) cv2.imwrite(os.path.join(output_dir, "ps4-5-b-1.png"), ps4.normalize_and_scale(u_v)) # Optional: smooth the images if LK doesn't work well on raw images levels = 6 # TODO: Define the number of levels k_size = 60 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 9 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values mc02g = cv2.GaussianBlur(mc02, ksize=(15, 15), sigmaX=sigma, sigmaY=sigma) mc03g = cv2.GaussianBlur(mc03, ksize=(15, 15), sigmaX=sigma, sigmaY=sigma) u, v = ps4.hierarchical_lk(mc02g, mc03g, levels, k_size, k_type, sigma, interpolation, border_mode) # Flow image shift_02 = ps4.warp(mc03, 0.8 * u, 0.8 * v, interpolation, border_mode) shift_04 = ps4.warp(mc03, 0.6 * u, 0.6 * v, interpolation, border_mode) shift_06 = ps4.warp(mc03, 0.4 * u, 0.4 * v, interpolation, border_mode) shift_08 = ps4.warp(mc03, 0.2 * u, 0.2 * v, interpolation, border_mode) u_v = np.vstack((np.hstack( (mc02, shift_02, shift_04)), np.hstack((shift_06, shift_08, mc03)))) cv2.imwrite(os.path.join(output_dir, "ps4-5-b-2.png"), ps4.normalize_and_scale(u_v))
def part_3a_1(): yos_img_01 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_01.jpg'), 0) / 255. yos_img_02 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_02.jpg'), 0) / 255. levels = 2 # Define the number of pyramid levels yos_img_01_g_pyr = ps4.gaussian_pyramid(yos_img_01, levels) yos_img_02_g_pyr = ps4.gaussian_pyramid(yos_img_02, levels) level_id = 1 k_size = 32 k_type = "uniform" sigma = 0 u, v = ps4.optic_flow_lk(yos_img_01_g_pyr[level_id], yos_img_02_g_pyr[level_id], k_size, k_type, sigma) u, v = scale_u_and_v(u, v, level_id, yos_img_02_g_pyr) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values yos_img_02_warped = ps4.warp(yos_img_02, u, v, interpolation, border_mode) diff_yos_img = yos_img_01 - yos_img_02_warped cv2.imwrite(os.path.join(output_dir, "ps4-3-a-1.png"), ps4.normalize_and_scale(diff_yos_img))
def part_3a_2(): yos_img_02 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_02.jpg'), 0) / 255. yos_img_03 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_03.jpg'), 0) / 255. levels = 4 yos_img_02_g_pyr = ps4.gaussian_pyramid(yos_img_02, levels) yos_img_03_g_pyr = ps4.gaussian_pyramid(yos_img_03, levels) level_id = 0 k_size = 51 k_type = "gaussian" sigma = 3 u, v = ps4.optic_flow_lk(yos_img_02_g_pyr[level_id], yos_img_03_g_pyr[level_id], k_size, k_type, sigma) u, v = scale_u_and_v(u, v, level_id, yos_img_03_g_pyr) interpolation = cv2.INTER_CUBIC border_mode = cv2.BORDER_REFLECT101 yos_img_03_warped = ps4.warp(yos_img_03, u, v, interpolation, border_mode) diff_yos_img = yos_img_02 - yos_img_03_warped cv2.imwrite(os.path.join(output_dir, "ps4-3-a-2.png"), ps4.normalize_and_scale(diff_yos_img))
def part_4b(): urban_img_01 = cv2.imread(os.path.join(input_dir, 'Urban2', 'urban01.png'), 0) / 255. urban_img_02 = cv2.imread(os.path.join(input_dir, 'Urban2', 'urban02.png'), 0) / 255. levels = 5 k_size = 51 k_type = "gaussian" sigma = 3 interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.hierarchical_lk(urban_img_01, urban_img_02, levels, k_size, k_type, sigma, interpolation, border_mode) u_v = quiver(u, v, scale=3, stride=10) cv2.imwrite(os.path.join(output_dir, "ps4-4-b-1.png"), u_v) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values urban_img_02_warped = ps4.warp(urban_img_02, u, v, interpolation, border_mode) diff_img = urban_img_01 - urban_img_02_warped cv2.imwrite(os.path.join(output_dir, "ps4-4-b-2.png"), ps4.normalize_and_scale(diff_img))
def part_3a_2(): yos_img_02 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_02.jpg'), 0) / 255. yos_img_03 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_03.jpg'), 0) / 255. levels = 1 # Define the number of pyramid levels yos_img_02_g_pyr = ps4.gaussian_pyramid(yos_img_02, levels) yos_img_03_g_pyr = ps4.gaussian_pyramid(yos_img_03, levels) level_id = 3 # TODO: Select the level number (or id) you wish to use k_size = 3 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 1 # TODO: Select a sigma value if you are using a gaussian kernel u, v = ps4.optic_flow_lk(yos_img_02_g_pyr[level_id], yos_img_03_g_pyr[level_id], k_size, k_type, sigma) u, v = scale_u_and_v(u, v, level_id, yos_img_03_g_pyr) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values yos_img_03_warped = ps4.warp(yos_img_03, u, v, interpolation, border_mode) diff_yos_img = yos_img_02 - yos_img_03_warped cv2.imwrite(os.path.join(output_dir, "ps4-3-a-2.png"), ps4.normalize_and_scale(diff_yos_img))
def part_4b(): urban_img_01 = cv2.imread(os.path.join(input_dir, 'Urban2', 'urban01.png'), 0) / 255. urban_img_02 = cv2.imread(os.path.join(input_dir, 'Urban2', 'urban02.png'), 0) / 255. levels = 1 # TODO: Define the number of levels k_size = 0 # TODO: Select a kernel size k_type = "" # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.hierarchical_lk(urban_img_01, urban_img_02, levels, k_size, k_type, sigma, interpolation, border_mode) u_v = quiver(u, v, scale=3, stride=10) cv2.imwrite(os.path.join(output_dir, "ps4-4-b-1.png"), u_v) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values urban_img_02_warped = ps4.warp(urban_img_02, u, v, interpolation, border_mode) diff_img = urban_img_01 - urban_img_02_warped cv2.imwrite(os.path.join(output_dir, "ps4-4-b-2.png"), ps4.normalize_and_scale(diff_img))
def part_5a(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ shift_0 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'Shift0.png'), 0) / 255. shift_r2 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR2.png'), 0) / 255. k_size = 25 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 0.5 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.optic_flow_lk(shift_0, shift_r2, k_size, k_type, sigma) img_0 = ps4.normalize_and_scale(shift_0) img_02 = ps4.warp(shift_0, -0.2 * u, -0.2 * v, interpolation, border_mode) img_04 = ps4.warp(shift_0, -0.4 * u, -0.4 * v, interpolation, border_mode) img_06 = ps4.warp(shift_0, -0.6 * u, -0.6 * v, interpolation, border_mode) img_08 = ps4.warp(shift_0, -0.8 * u, -0.8 * v, interpolation, border_mode) img_02 = ps4.normalize_and_scale(img_02) img_04 = ps4.normalize_and_scale(img_04) img_06 = ps4.normalize_and_scale(img_06) img_08 = ps4.normalize_and_scale(img_08) img_1 = ps4.normalize_and_scale(shift_r2) img_row1 = np.concatenate((img_0, img_02, img_04), axis=1) img_row2 = np.concatenate((img_06, img_08, img_1), axis=1) img_all = np.concatenate((img_row1, img_row2), axis=0) images = [img_0, img_02, img_04, img_06, img_08, img_1] imageio.mimsave(os.path.join(output_dir, "ps4-5-1-a-1.gif"), images) # Flow image u_v = quiver(u, v, scale=3, stride=10) #cv2.imwrite(os.path.join(output_dir, "ps4-5-1-a-00.png"), img_0) #cv2.imwrite(os.path.join(output_dir, "ps4-5-1-a-02.png"), img_02) #cv2.imwrite(os.path.join(output_dir, "ps4-5-1-a-04.png"), img_04) #cv2.imwrite(os.path.join(output_dir, "ps4-5-1-a-06.png"), img_06) #cv2.imwrite(os.path.join(output_dir, "ps4-5-1-a-08.png"), img_08) #cv2.imwrite(os.path.join(output_dir, "ps4-5-1-a-10.png"), img_1) cv2.imwrite(os.path.join(output_dir, "ps4-5-1-a-1.png"), img_all)
def interpolate_frames(I_0, I_1, t, u, v): I_1_copy = np.copy(I_1) I_0_copy = np.copy(I_0) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values I_t = ps4.warp(I_0_copy, -t * u, -t * v, interpolation, border_mode) return I_t
def part_5a(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ shift_0 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'Shift0.png'), 0) / 255. shift_r10 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR10.png'), 0) / 255. levels = 4 # TODO: Define the number of levels k_size = 35 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.hierarchical_lk(shift_0, shift_r10, levels, k_size, k_type, sigma, interpolation, border_mode) t = 0.2 shift_r10_wraped_1 = ps4.warp(shift_0, -t*u, -t*v, interpolation, border_mode) t = 0.4 shift_r10_wraped_2 = ps4.warp(shift_0, -t*u, -t*v, interpolation, border_mode) t = 0.6 shift_r10_wraped_3 = ps4.warp(shift_0, -t*u, -t*v, interpolation, border_mode) t = 0.8 shift_r10_wraped_4 = ps4.warp(shift_0, -t*u, -t*v, interpolation, border_mode) H, W = shift_0.shape target = np.ones((2*H, 3*W), dtype = np.float64) target[0 : H, 0 : W] = ps4.normalize_and_scale(shift_0) target[0 : H, W : 2*W] = ps4.normalize_and_scale(shift_r10_wraped_1) target[0 : H, 2*W : 3*W] = ps4.normalize_and_scale(shift_r10_wraped_2) target[H : 2*H, 0 : W] = ps4.normalize_and_scale(shift_r10_wraped_3) target[H : 2*H, W : 2*W] = ps4.normalize_and_scale(shift_r10_wraped_4) target[H : 2*H, 2*W : 3*W] = ps4.normalize_and_scale(shift_r10) cv2.imwrite(os.path.join(output_dir, "ps4-5-a-1.png"), ps4.normalize_and_scale(target))
def part_3a_1(): yos_img_01 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_01.jpg'), 0) / 255. yos_img_02 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_02.jpg'), 0) / 255. levels = 4 # Define the number of pyramid levels yos_img_01_g_pyr = ps4.gaussian_pyramid(yos_img_01, levels) yos_img_02_g_pyr = ps4.gaussian_pyramid(yos_img_02, levels) # k_size = "kSize" # window = "Params" # cv2.namedWindow(window) # cv2.createTrackbar(k_size, window, 1, 100, nothing) # while 1: # k = cv2.waitKey(1) & 0xFF # if k == 27: # break # # level_id = 0 # TODO: Select the level number (or id) you wish to use # # k_size = 11 # TODO: Select a kernel size # k_size = cv2.getTrackbarPos('kSize', 'Params') # k_type = 'uniform' # TODO: Select a kernel type # sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel # u, v = ps4.optic_flow_lk(yos_img_01_g_pyr[level_id], # yos_img_02_g_pyr[level_id], # k_size, k_type, sigma) # # u, v = scale_u_and_v(u, v, level_id, yos_img_02_g_pyr) # # interpolation = cv2.INTER_CUBIC # You may try different values # border_mode = cv2.BORDER_REFLECT101 # You may try different values # yos_img_02_warped = ps4.warp(yos_img_02, u, v, interpolation, border_mode) # # diff_yos_img_01_02 = yos_img_01 - yos_img_02_warped # # cv2.imshow("Params", diff_yos_img_01_02) level_id = 1 # TODO: Select the level number (or id) you wish to use k_size = 23 # TODO: Select a kernel size k_type = 'uniform' # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel u, v = ps4.optic_flow_lk(yos_img_01_g_pyr[level_id], yos_img_02_g_pyr[level_id], k_size, k_type, sigma) u, v = scale_u_and_v(u, v, level_id, yos_img_02_g_pyr) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values yos_img_02_warped = ps4.warp(yos_img_02, u, v, interpolation, border_mode) diff_yos_img_01_02 = yos_img_01 - yos_img_02_warped cv2.imwrite(os.path.join(output_dir, "ps4-3-a-1.png"), ps4.normalize_and_scale(diff_yos_img_01_02))
def part_5a(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ image_at_0 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'Shift0.png'), 0) / 255 image_at_10 = cv2.imread( os.path.join(input_dir, 'TestSeq', 'ShiftR10.png'), 0) / 255 levels, k_size, k_type, sigma = 5, 9, "uniform", 1 interpolation, border_mode = cv2.INTER_CUBIC, cv2.BORDER_REFLECT101 u, v = ps4.hierarchical_lk(image_at_0, image_at_10, levels, k_size, k_type, sigma, interpolation, border_mode) motion_frame = [] motion_frame.append(image_at_0) motion_frame.append( ps4.warp(image_at_0, -0.2 * u, -0.2 * v, interpolation, border_mode)) motion_frame.append( ps4.warp(image_at_0, -0.4 * u, -0.4 * v, interpolation, border_mode)) motion_frame.append( ps4.warp(image_at_0, -0.6 * u, -0.6 * v, interpolation, border_mode)) motion_frame.append( ps4.warp(image_at_0, -0.8 * u, -0.8 * v, interpolation, border_mode)) motion_frame.append(image_at_10) height, width = image_at_0.shape motion_picture = np.ones((2 * height, 3 * width), dtype=np.float64) for i in range(2): for j in range(3): motion_picture[i * height:(i + 1) * height, j * width:(j + 1) * width] = motion_frame[3 * i + j] cv2.imwrite(os.path.join(output_dir, "ps4-5-a-1.png"), ps4.normalize_and_scale(motion_picture))
def part_5b(): mc_01 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc01.png'), 0) / 255. mc_02 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc02.png'), 0) / 255. mc_03 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc03.png'), 0) / 255. # Create a window cv2.namedWindow('image') u_v = np.zeros(mc_01.shape) # create trackbars for color change cv2.createTrackbar('levels', 'image', 1, 15, nothing) cv2.createTrackbar('k_size', 'image', 3, 70, nothing) cv2.createTrackbar('k_type', 'image', 0, 1, nothing) cv2.createTrackbar('sigma', 'image', 1, 50, nothing) while (1): cv2.imshow('image', u_v) k = cv2.waitKey(1) & 0xFF if k == 27: break # Optional: smooth the images if LK doesn't work well on raw images levels = cv2.getTrackbarPos('levels', 'image') k_size = cv2.getTrackbarPos('k_size', 'image') k_type = 'gaussian' if cv2.getTrackbarPos('k_type', 'image') == 1 else 'uniform' sigma = cv2.getTrackbarPos('sigma', 'image') interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.hierarchical_lk(mc_01, mc_02, levels, k_size, k_type, sigma, interpolation, border_mode) u_v = experiment.quiver(u, v, scale=0.5, stride=10) print levels, k_size, k_type, sigma interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values mc_02_warped = ps4.warp(mc_02, u, v, interpolation, border_mode) diff_img = ps4.normalize_and_scale(mc_01 - mc_02_warped).astype( np.uint8) cv2.imshow('image2', diff_img) cv2.destroyAllWindows() print levels, k_size, k_type, sigma
def part_5a(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ shift_0 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'Shift0.png'), 0) / 255. shift_r10 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR10.png'), 0) / 255. # Optional: smooth the images if LK doesn't work well on raw images levels = 5 # TODO: Define the number of levels k_size = 15 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.hierarchical_lk(shift_0, shift_r10, levels, k_size, k_type, sigma, interpolation, border_mode) # Flow image shift_02 = ps4.warp(shift_r10, 0.8 * u, 0.8 * v, interpolation, border_mode) shift_04 = ps4.warp(shift_r10, 0.6 * u, 0.6 * v, interpolation, border_mode) shift_06 = ps4.warp(shift_r10, 0.4 * u, 0.4 * v, interpolation, border_mode) shift_08 = ps4.warp(shift_r10, 0.2 * u, 0.2 * v, interpolation, border_mode) u_v = np.vstack((np.hstack((shift_0, shift_02, shift_04)), np.hstack((shift_06, shift_08, shift_r10)))) cv2.imwrite(os.path.join(output_dir, "ps4-5-a-1.png"), ps4.normalize_and_scale(u_v))
def part_3(): yos_img_01 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_02.jpg'), 0) / 255. yos_img_02 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_03.jpg'), 0) / 255. levels = 10 # Define the number of pyramid levels yos_img_01_g_pyr = ps4.gaussian_pyramid(yos_img_01, levels) yos_img_02_g_pyr = ps4.gaussian_pyramid(yos_img_02, levels) # Create a window cv2.namedWindow('image') diff_yos_img = np.zeros(yos_img_01.shape) # create trackbars for color change cv2.createTrackbar('level_id', 'image', 1, levels - 1, nothing) cv2.createTrackbar('k_size', 'image', 3, 70, nothing) cv2.createTrackbar('k_type', 'image', 0, 1, nothing) cv2.createTrackbar('sigma', 'image', 1, 50, nothing) while (1): cv2.imshow('image', diff_yos_img) k = cv2.waitKey(1) & 0xFF if k == 27: break # Optional: smooth the images if LK doesn't work well on raw images level_id = cv2.getTrackbarPos('level_id', 'image') k_size = cv2.getTrackbarPos('k_size', 'image') k_type = 'gaussian' if cv2.getTrackbarPos('k_type', 'image') == 1 else 'uniform' sigma = cv2.getTrackbarPos('sigma', 'image') u, v = ps4.optic_flow_lk(yos_img_01_g_pyr[level_id], yos_img_02_g_pyr[level_id], k_size, k_type, sigma) u, v = experiment.scale_u_and_v(u, v, level_id, yos_img_02_g_pyr) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values yos_img_02_warped = ps4.warp(yos_img_02, u, v, interpolation, border_mode) diff_yos_img = ps4.normalize_and_scale(yos_img_01 - yos_img_02_warped).astype( np.uint8) cv2.destroyAllWindows() print level_id, k_size, k_type, sigma
def part_4b(): urban_img_01 = cv2.imread(os.path.join(input_dir, 'Urban2', 'urban01.png'), 0) / 255. urban_img_02 = cv2.imread(os.path.join(input_dir, 'Urban2', 'urban02.png'), 0) / 255. levels = 4 k_size = 57 k_type = 'uniform' sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values # k_size = "kSize" # window = "Params" # cv2.namedWindow(window) # cv2.createTrackbar(k_size, window, 1, 100, nothing) # while 1: # k = cv2.waitKey(1) & 0xFF # if k == 27: # break # # level_id = 1 # TODO: Select the level number (or id) you wish to use # # k_size = 11 # TODO: Select a kernel size # k_size = cv2.getTrackbarPos('kSize', 'Params') # k_type = 'uniform' # TODO: Select a kernel type # # u, v = ps4.hierarchical_lk(urban_img_01, urban_img_02, levels, k_size, # k_type, sigma, interpolation, border_mode) # # u_v = quiver(u, v, scale=3, stride=10) # cv2.imshow("Params", u_v) u, v = ps4.hierarchical_lk(urban_img_01, urban_img_02, levels, k_size, k_type, sigma, interpolation, border_mode) u_v = quiver(u, v, scale=3, stride=10) cv2.imwrite(os.path.join(output_dir, "ps4-4-b-1.png"), u_v) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values urban_img_02_warped = ps4.warp(urban_img_02, u, v, interpolation, border_mode) diff_img = urban_img_01 - urban_img_02_warped cv2.imwrite(os.path.join(output_dir, "ps4-4-b-2.png"), ps4.normalize_and_scale(diff_img))
def part_5a(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ #I1(x0) = I0(x0 - tu0) I_0 = cv2.imread('input_images/TestSeq/Shift0.png', 0) / 1. I_1 = cv2.imread('input_images/TestSeq/ShiftR10.png', 0) / 1. k_size = 15 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values im_array = [] t_values = np.arange(0, 1.2, .2) U, V = ps4.optic_flow_lk(I_0, I_1, k_size=k_size, k_type=k_type, sigma=sigma) for val in t_values: print val scaled_U = U * val scaled_V = V * val warped = ps4.warp(I_0, U=-scaled_U, V=-scaled_V, interpolation=interpolation, border_mode=border_mode) cv2.imwrite(str(val) + '.png', warped) im_array.append(warped) r1 = np.concatenate((im_array[0], im_array[1], im_array[2]), axis=1) r2 = np.concatenate((im_array[3], im_array[4], im_array[5]), axis=1) complete = np.concatenate((r1, r2), axis=0) cv2.imwrite('output/ps4-5-1-a-1.png', complete.astype(np.int16)) print 'FINISHED PART_5A'
def interpolate_frames(img_a, img_b, levels=4, k_size=13, n_add=4): interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values u, v = ps4.hierarchical_lk(img_b, img_a, levels, k_size, 'uniform', 0, interpolation, border_mode) u = u / (n_add + 1) v = v / (n_add + 1) frames = [img_a] for i in range(n_add): f = ps4.warp(frames[i], u, v, interpolation, border_mode) frames.append(f) frames.append(img_b) return frames
def test_warp(self): for i in range(2): f1 = self.input_imgs_1[i] # Not used f2 = self.input_imgs_2[i] f3 = self.input_flows[i] img1 = np.load(INPUT_DIR + f1) # Not used img2 = np.load(INPUT_DIR + f2) u_v = np.load(INPUT_DIR + f3) u = u_v[:, :, 0] v = u_v[:, :, 1] warped = ps4.warp(img2.copy(), u.copy(), v.copy(), cv2.INTER_CUBIC, cv2.BORDER_REFLECT101) r = self.r_val[i] c = self.c_val[i] box_value = self.bv[i] center_box_average = np.mean(warped[r:3 * r, c:3 * c]) correct_center_box = abs(center_box_average - box_value) <= 0.51 error_msg = "Center box average pixel value is greater than the " \ "value used in the input image." self.assertTrue(correct_center_box, error_msg) warped_without_center = np.copy(warped) warped_without_center[r:3 * r, c:3 * c] = 0. average_warped_img = np.mean(warped_without_center) center_box_average = box_value * 0.15 correct_warped_img = center_box_average >= average_warped_img error_msg = "Average of values outside the center box area are " \ "greater than the allowed amount." self.assertTrue(correct_warped_img, error_msg)
def part_3a_1(): yos_img_01 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_01.jpg'), 0) / 255. yos_img_02 = cv2.imread( os.path.join(input_dir, 'DataSeq1', 'yos_img_02.jpg'), 0) / 255. k = 11 sigma = 7 yos_img_01g = cv2.GaussianBlur(yos_img_01, ksize=(k, k), sigmaX=sigma, sigmaY=sigma) yos_img_02g = cv2.GaussianBlur(yos_img_02, ksize=(k, k), sigmaX=sigma, sigmaY=sigma) levels = 5 # Define the number of pyramid levels yos_img_01_g_pyr = ps4.gaussian_pyramid(yos_img_01g, levels) yos_img_02_g_pyr = ps4.gaussian_pyramid(yos_img_02g, levels) level_id = 0 # TODO: Select the level number (or id) you wish to use k_size = 20 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 10 # TODO: Select a sigma value if you are using a gaussian kernel u, v = ps4.optic_flow_lk(yos_img_01_g_pyr[level_id], yos_img_02_g_pyr[level_id], k_size, k_type, sigma) u, v = scale_u_and_v(u, v, level_id, yos_img_02_g_pyr) interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values yos_img_02_warped = ps4.warp(yos_img_02, u, v, interpolation, border_mode) diff_yos_img_01_02 = yos_img_01 - yos_img_02_warped cv2.imwrite(os.path.join(output_dir, "ps4-3-a-1.png"), ps4.normalize_and_scale(diff_yos_img_01_02))
def part_5b(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ ## Load the data first mc01 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc01.png'), 0) / 255 mc02 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc02.png'), 0) / 255 mc03 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc03.png'), 0) / 255 ## Part_1 levels, k_size, k_type, sigma = 5, 35, "gaussian", 5 interpolation, border_mode = cv2.INTER_CUBIC, cv2.BORDER_REFLECT101 u_1, v_1 = ps4.hierarchical_lk(mc01, mc02, levels, k_size, k_type, sigma, interpolation, border_mode) motion_frame_1 = [] motion_frame_1.append(mc01) motion_frame_1.append( ps4.warp(mc01, -0.2 * u_1, -0.2 * v_1, interpolation, border_mode)) motion_frame_1.append( ps4.warp(mc01, -0.4 * u_1, -0.4 * v_1, interpolation, border_mode)) motion_frame_1.append( ps4.warp(mc01, -0.6 * u_1, -0.6 * v_1, interpolation, border_mode)) motion_frame_1.append( ps4.warp(mc01, -0.8 * u_1, -0.8 * v_1, interpolation, border_mode)) motion_frame_1.append(mc02) height, width = mc01.shape motion_picture_1 = np.zeros((2 * height, 3 * width), dtype=np.float64) for i in range(2): for j in range(3): motion_picture_1[i * height:(i + 1) * height, j * width:(j + 1) * width] = motion_frame_1[3 * i + j] cv2.imwrite(os.path.join(output_dir, "ps4-5-b-1.png"), ps4.normalize_and_scale(motion_picture_1)) ## Part_2 levels, k_size, k_type, sigma = 5, 29, "gaussian", 3 interpolation, border_mode = cv2.INTER_CUBIC, cv2.BORDER_REFLECT101 u_2, v_2 = ps4.hierarchical_lk(mc02, mc03, levels, k_size, k_type, sigma, interpolation, border_mode) motion_frame_2 = [] motion_frame_2.append(mc02) motion_frame_2.append( ps4.warp(mc02, -0.2 * u_2, -0.2 * v_2, interpolation, border_mode)) motion_frame_2.append( ps4.warp(mc02, -0.4 * u_2, -0.4 * v_2, interpolation, border_mode)) motion_frame_2.append( ps4.warp(mc02, -0.6 * u_2, -0.6 * v_2, interpolation, border_mode)) motion_frame_2.append( ps4.warp(mc02, -0.8 * u_2, -0.8 * v_2, interpolation, border_mode)) motion_frame_2.append(mc03) height, width = mc02.shape motion_picture_2 = np.zeros((2 * height, 3 * width), dtype=np.float64) for i in range(2): for j in range(3): motion_picture_2[i * height:(i + 1) * height, j * width:(j + 1) * width] = motion_frame_2[3 * i + j] cv2.imwrite(os.path.join(output_dir, "ps4-5-b-2.png"), ps4.normalize_and_scale(motion_picture_2))
def part_5a(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ k_type = 'uniform' border_mode = cv2.BORDER_REFLECT101 # You may try different values interpolation = cv2.INTER_CUBIC images = [] # need intervals for 0.2, 0.4, 0.6, 0.8. arange is not end inclusive. intervals = np.arange(0.2, 1, 0.2) image_1 = cv2.imread('input_images/TestSeq/Shift0.png', 0) / 1. image_2 = cv2.imread('input_images/TestSeq/ShiftR10.png', 0) / 1. images.append(image_1) # add t0 cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(0))), image_1) k_size = 21 # loop over time intervals. counter = 1 img = image_1.copy() for i in intervals: U, V = ps4.hierarchical_lk(img, image_2, levels=4, k_size=k_size, k_type=k_type, sigma=0, interpolation=interpolation, border_mode=border_mode) # orientate and scale U = -U * i V = -V * i warped = ps4.warp(img, U, V, interpolation=interpolation, border_mode=border_mode) cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(counter))), warped) images.append(warped) img = warped counter += 1 images.append(image_2) # add t1 cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(counter))), image_2) # build output image # r1 0, 0.2, 0.4 # r2 0.6, 0.8, 1 row_1 = np.concatenate((images[0], images[1], images[2]), axis=1) row_2 = np.concatenate((images[3], images[4], images[5]), axis=1) output = np.concatenate((row_1, row_2), axis=0) cv2.imwrite(os.path.join(output_dir, 'ps4-5-1-a-1.png'), output)
def part_5b(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ k_type = 'uniform' border_mode = cv2.BORDER_REFLECT101 # You may try different values interpolation = cv2.INTER_CUBIC images = [] # need intervals for 0, 0.2, 0.4, 0.6, 0.8 and 1. arange is not end inclusive. intervals = np.arange(0.2, 1, 0.2) image_1 = cv2.imread('input_images/MiniCooper/mc01.png', 0) / 1. image_2 = cv2.imread('input_images/MiniCooper/mc02.png', 0) / 1. images.append(image_1) cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(10))), image_1) k_size = 45 # # loop over time intervals. counter = 11 img = image_1 for i in intervals: # from the current interval image to desired output. U, V = ps4.hierarchical_lk(img, image_2, levels=4, k_size=k_size, k_type=k_type, sigma=0, interpolation=interpolation, border_mode=border_mode) # orientate. U = -U # * i V = -V # * i warped = ps4.warp(img, U, V, interpolation=interpolation, border_mode=border_mode) img = warped # reset for next iteration cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(counter))), warped) images.append(warped) counter += 1 images.append(image_2) cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(counter))), image_2) # build output image # r1 0, 0.2, 0.4 # r2 0.6, 0.8, 1 row_1 = np.concatenate((images[0], images[1], images[2]), axis=1) row_2 = np.concatenate((images[3], images[4], images[5]), axis=1) output = np.concatenate((row_1, row_2), axis=0) cv2.imwrite(os.path.join(output_dir, 'ps4-5-1-b-1.png'), output) # part 2 images = [] image_1 = cv2.imread('input_images/MiniCooper/mc02.png', 0) / 1. image_2 = cv2.imread('input_images/MiniCooper/mc03.png', 0) / 1. images.append(image_1) k_size = 45 # # loop over time intervals. counter = 21 img = image_1 for i in intervals: # from the current interval image to desired output. U, V = ps4.hierarchical_lk(img, image_2, levels=4, k_size=k_size, k_type=k_type, sigma=0, interpolation=interpolation, border_mode=border_mode) # orientate. U = -U # * i V = -V # * i warped = ps4.warp(img, U, V, interpolation=interpolation, border_mode=border_mode) img = warped # reset for next iteration cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(counter))), warped) images.append(warped) counter += 1 images.append(image_2) cv2.imwrite(os.path.join(output_dir, '{}.png'.format(str(counter))), image_2) # build output image # r1 0, 0.2, 0.4 # r2 0.6, 0.8, 1 row_1 = np.concatenate((images[0], images[1], images[2]), axis=1) row_2 = np.concatenate((images[3], images[4], images[5]), axis=1) output = np.concatenate((row_1, row_2), axis=0) cv2.imwrite(os.path.join(output_dir, 'ps4-5-1-b-2.png'), output)
def part_5b(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ mc01 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc01.png'), 0) / 255. mc02 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc02.png'), 0) / 255. mc03 = cv2.imread(os.path.join(input_dir, 'MiniCooper', 'mc03.png'), 0) / 255. mc01 = cv2.GaussianBlur(mc01, (45, 45), 0.05) mc02 = cv2.GaussianBlur(mc02, (45, 45), 0.05) mc03 = cv2.GaussianBlur(mc03, (45, 45), 0.05) k_size = 15 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 0.5 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values levels = 4 u, v = ps4.hierarchical_lk(mc01, mc02, levels, k_size, k_type, sigma, interpolation, border_mode) u_v = quiver(u, v, scale=3, stride=10) cv2.imwrite(os.path.join(output_dir, "ps4-5-1-b-1-quiver.png"), u_v) mc_001 = ps4.normalize_and_scale(mc01) mc_002 = ps4.warp(mc01, -0.2 * u, -0.2 * v, interpolation, border_mode) mc_004 = ps4.warp(mc01, -0.4 * u, -0.4 * v, interpolation, border_mode) mc_006 = ps4.warp(mc01, -0.6 * u, -0.6 * v, interpolation, border_mode) mc_008 = ps4.warp(mc01, -0.8 * u, -0.8 * v, interpolation, border_mode) mc_200 = ps4.normalize_and_scale(mc02) mc_002 = ps4.normalize_and_scale(mc_002) mc_004 = ps4.normalize_and_scale(mc_004) mc_006 = ps4.normalize_and_scale(mc_006) mc_008 = ps4.normalize_and_scale(mc_008) k_size = 10 # TODO: Select a kernel size k_type = "uniform" # TODO: Select a kernel type sigma = 2 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values levels = 6 u, v = ps4.hierarchical_lk(mc02, mc03, levels, k_size, k_type, sigma, interpolation, border_mode) #print "U shape:{}".format(u.shape) #print "V shape:{}".format(v.shape) #print "img shape: {}".format(mc01.shape) u_v = quiver(u, v, scale=3, stride=10) cv2.imwrite(os.path.join(output_dir, "ps4-5-1-b-2-quiver.png"), u_v) mc_201 = ps4.normalize_and_scale(mc02) mc_202 = ps4.warp(mc02, -0.2 * u, -0.2 * v, interpolation, border_mode) mc_204 = ps4.warp(mc02, -0.4 * u, -0.4 * v, interpolation, border_mode) mc_206 = ps4.warp(mc02, -0.6 * u, -0.6 * v, interpolation, border_mode) mc_208 = ps4.warp(mc02, -0.8 * u, -0.8 * v, interpolation, border_mode) mc_300 = ps4.normalize_and_scale(mc03) mc_202 = ps4.normalize_and_scale(mc_202) mc_204 = ps4.normalize_and_scale(mc_204) mc_206 = ps4.normalize_and_scale(mc_206) mc_208 = ps4.normalize_and_scale(mc_208) mc01_02_row1 = np.concatenate((mc_001, mc_002, mc_004), axis=1) mc01_02_row2 = np.concatenate((mc_006, mc_008, mc_200), axis=1) mc01_02_all = np.concatenate((mc01_02_row1, mc01_02_row2), axis=0) images_01_02 = [mc_001, mc_002, mc_004, mc_006, mc_008, mc_200] cv2.imwrite(os.path.join(output_dir, "ps4-5-1-b-1.png"), mc01_02_all) imageio.mimsave(os.path.join(output_dir, "ps4-5-1-b-1.gif"), images_01_02) mc02_03_row1 = np.concatenate((mc_201, mc_202, mc_204), axis=1) mc02_03_row2 = np.concatenate((mc_206, mc_208, mc_300), axis=1) mc02_03_all = np.concatenate((mc02_03_row1, mc02_03_row2), axis=0) images_02_03 = [mc_201, mc_202, mc_204, mc_206, mc_208, mc_300] cv2.imwrite(os.path.join(output_dir, "ps4-5-1-b-2.png"), mc02_03_all) imageio.mimsave(os.path.join(output_dir, "ps4-5-1-b-2.gif"), images_02_03)
def part_5b(): """Frame interpolation Follow the instructions in the problem set instructions. Place all your work in this file and this section. """ I_0 = cv2.imread('input_images/MiniCooper/mc01.png', 0) / 1. I_1 = cv2.imread('input_images/MiniCooper/mc02.png', 0) / 1. # I_0 = cv2.blur(I_0, (15, 15)) # I_1 = cv2.blur(I_1, (15, 15)) k_size = 33 # TODO: Select a kernel size k_type = "" # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values im_array = [] t_values = np.arange(0, 1.2, .2) # for i in range(15, 85, 2): # # U, V = ps4.hierarchical_lk(I_0, I_1, levels=4, k_size=i, k_type=k_type, sigma=sigma, # interpolation=interpolation, border_mode=border_mode) # # # print i # u_v = quiver(U, V, scale=3, stride=10) # cv2.imwrite(str(i)+'.png', u_v) # # return None I_temp = I_0 for val in t_values: U, V = ps4.hierarchical_lk(I_temp, I_1, levels=4, k_size=k_size, k_type=k_type, sigma=sigma, interpolation=interpolation, border_mode=border_mode) # Unew, Vnew = cv2.blur(U, (3,3)), cv2.blur(V, (3,3)) dst = ps4.warp(I_0, U=-U * val, V=-V * val, interpolation=interpolation, border_mode=border_mode) I_temp = dst # warped = warped.astype('uint8') # dst = cv2.fastNlMeansDenoising(warped, None, searchWindowSize=21, templateWindowSize=7, h=10) # warped = cv2.fastNlMeansDenoising(warped,10,10,10) # warped = cv2.blur(warped, (3,3)) im_array.append(dst) cv2.imwrite(str(val) + '.png', dst) im_array[-1] = I_1 r1 = np.concatenate((im_array[0], im_array[1], im_array[2]), axis=1) r2 = np.concatenate((im_array[3], im_array[4], im_array[5]), axis=1) complete = np.concatenate((r1, r2), axis=0) cv2.imwrite('output/ps4-5-1-b-1.png', complete) ## ##starting on mc2 --> mc3 ## I_0 = cv2.imread('input_images/MiniCooper/mc02.png', 0) / 1. I_1 = cv2.imread('input_images/MiniCooper/mc03.png', 0) / 1. # I_0 = cv2.blur(I_0, (15, 15)) # I_1 = cv2.blur(I_1, (15, 15)) k_size = 33 # TODO: Select a kernel size k_type = "" # TODO: Select a kernel type sigma = 0 # TODO: Select a sigma value if you are using a gaussian kernel interpolation = cv2.INTER_CUBIC # You may try different values border_mode = cv2.BORDER_REFLECT101 # You may try different values im_array = [] t_values = np.arange(0, 1.2, .2) I_temp = I_0 for val in t_values: U, V = ps4.hierarchical_lk(I_temp, I_1, levels=4, k_size=k_size, k_type=k_type, sigma=sigma, interpolation=interpolation, border_mode=border_mode) # Unew, Vnew = cv2.blur(U, (3,3)), cv2.blur(V, (3,3)) dst = ps4.warp(I_0, U=-U * val, V=-V * val, interpolation=interpolation, border_mode=border_mode) I_temp = dst # warped = warped.astype('uint8') # dst = cv2.fastNlMeansDenoising(warped, None, searchWindowSize=21, templateWindowSize=7, h=10) # warped = cv2.fastNlMeansDenoising(warped,10,10,10) # warped = cv2.blur(warped, (3,3)) im_array.append(dst) cv2.imwrite(str(val) + '.png', dst) im_array[-1] = I_1 r1 = np.concatenate((im_array[0], im_array[1], im_array[2]), axis=1) r2 = np.concatenate((im_array[3], im_array[4], im_array[5]), axis=1) complete = np.concatenate((r1, r2), axis=0) cv2.imwrite('output/ps4-5-1-b-2.png', complete.astype(np.int16))