def scale_u_and_v(u, v, level, pyr): """Scales up U and V arrays to match the image dimensions assigned to the first pyramid level: pyr[0]. You will use this method in part 3. In this section you are asked to select a level in the gaussian pyramid which contains images that are smaller than the one located in pyr[0]. This function should take the U and V arrays computed from this lower level and expand them to match a the size of pyr[0]. This function consists of a sequence of ps4.expand_image operations based on the pyramid level used to obtain both U and V. Multiply the result of expand_image by 2 to scale the vector values. After each expand_image operation you should adjust the resulting arrays to match the current level shape i.e. U.shape == pyr[current_level].shape and V.shape == pyr[current_level].shape. In case they don't, adjust the U and V arrays by removing the extra rows and columns. Hint: create a for loop from level-1 to 0 inclusive. Both resulting arrays' shapes should match pyr[0].shape. Args: u: U array obtained from ps4.optic_flow_lk v: V array obtained from ps4.optic_flow_lk level: level value used in the gaussian pyramid to obtain U and V (see part_3) pyr: gaussian pyramid used to verify the shapes of U and V at each iteration until the level 0 has been met. Returns: tuple: two-element tuple containing: u (numpy.array): scaled U array of shape equal to pyr[0].shape v (numpy.array): scaled V array of shape equal to pyr[0].shape """ i = level while i != 0: h, w = pyr[i - 1].shape u = 2 * ps4.expand_image(u) v = 2 * ps4.expand_image(v) h_, w_ = u.shape if h != h_: u = u[:-1, :] v = v[:-1, :] if w % 2 != 0: u = u[:, :-1] v = v[:, :-1] i = i - 1 # TODO: Your code here return (u, v) raise NotImplementedError
def scale_u_and_v(u, v, level, pyr): """Scales up U and V arrays to match the image dimensions assigned to the first pyramid level: pyr[0]. You will use this method in part 3. In this section you are asked to select a level in the gaussian pyramid which contains images that are smaller than the one located in pyr[0]. This function should take the U and V arrays computed from this lower level and expand them to match a the size of pyr[0]. This function consists of a sequence of ps4.expand_image operations based on the pyramid level used to obtain both U and V. Multiply the result of expand_image by 2 to scale the vector values. After each expand_image operation you should adjust the resulting arrays to match the current level shape i.e. U.shape == pyr[current_level].shape and V.shape == pyr[current_level].shape. In case they don't, adjust the U and V arrays by removing the extra rows and columns. Hint: create a for loop from level-1 to 0 inclusive. Both resulting arrays' shapes should match pyr[0].shape. Args: u: U array obtained from ps4.optic_flow_lk v: V array obtained from ps4.optic_flow_lk level: level value used in the gaussian pyramid to obtain U and V (see part_3) pyr: gaussian pyramid used to verify the shapes of U and V at each iteration until the level 0 has been met. Returns: tuple: two-element tuple containing: u (numpy.array): scaled U array of shape equal to pyr[0].shape v (numpy.array): scaled V array of shape equal to pyr[0].shape """ U = u.copy() V = v.copy() for cur_level in range(level - 1, -1, -1): U = ps4.expand_image(U) * 2 V = ps4.expand_image(V) * 2 h, w = pyr[cur_level].shape V = V[:h, :w] U = U[:h, :w] return U, V
def test_expand(self): input_imgs = [ 'test_expand1_img.npy', 'test_expand2_img.npy', 'test_expand3_img.npy' ] ref_imgs = [ 'test_expand1_ref.npy', 'test_expand2_ref.npy', 'test_expand3_ref.npy' ] for i in range(3): f1 = input_imgs[i] f2 = ref_imgs[i] test_array = np.load(INPUT_DIR + f1) expanded = ps4.expand_image(test_array.copy()) ref_expanded = np.load(INPUT_DIR + f2) #print test_array.shape, "original" #print expanded.shape, "expanded" #print ref_expanded.shape, "reference" #cv2.imshow("original",expanded) #cv2.imshow("expanded",ref_expanded) #cv2.waitKey(0) #print expanded #print ref_expanded correct = np.allclose(expanded, ref_expanded, atol=0.05) self.assertTrue(correct, "Output does not match the reference " "solution.")
def test_expand(self): input_imgs = [ 'test_expand1_img.npy', 'test_expand2_img.npy', 'test_expand3_img.npy' ] ref_imgs = [ 'test_expand1_ref.npy', 'test_expand2_ref.npy', 'test_expand3_ref.npy' ] for i in range(3): f1 = input_imgs[i] f2 = ref_imgs[i] test_array = np.load(INPUT_DIR + f1) expanded = ps4.expand_image(test_array.copy()) ref_expanded = np.load(INPUT_DIR + f2) correct = np.allclose(expanded, ref_expanded, atol=0.05) self.assertTrue(correct, "Output does not match the reference " "solution.")
def scale_u_and_v(u, v, level, pyr): """Scales up U and V arrays to match the image dimensions assigned to the first pyramid level: pyr[0]. You will use this method in part 3. In this section you are asked to select a level in the gaussian pyramid which contains images that are smaller than the one located in pyr[0]. This function should take the U and V arrays computed from this lower level and expand them to match a the size of pyr[0]. This function consists of a sequence of ps4.expand_image operations based on the pyramid level used to obtain both U and V. Multiply the result of expand_image by 2 to scale the vector values. After each expand_image operation you should adjust the resulting arrays to match the current level shape i.e. U.shape == pyr[current_level].shape and V.shape == pyr[current_level].shape. In case they don't, adjust the U and V arrays by removing the extra rows and columns. Hint: create a for loop from level-1 to 0 inclusive. Both resulting arrays' shapes should match pyr[0].shape. Args: u: U array obtained from ps4.optic_flow_lk v: V array obtained from ps4.optic_flow_lk level: level value used in the gaussian pyramid to obtain U and V (see part_3) pyr: gaussian pyramid used to verify the shapes of U and V at each iteration until the level 0 has been met. Returns: tuple: two-element tuple containing: u (numpy.array): scaled U array of shape equal to pyr[0].shape v (numpy.array): scaled V array of shape equal to pyr[0].shape """ # TODO: Your code here """ (uh, uw) = u.shape[:2] (vh, vw) = v.shape[:2] (h, w) = pyr[level].shape[:2] if not uh == h: U = U[:h-uh, :] if not uw == w: U = U[:, :w-uw] if not vh == h: V = V[:h-vh, :] if not vw == w: V = V[:, :w-vw] """ for lev in range(level, 0, -1): img = pyr[lev] (uh, uw) = u.shape[:2] (vh, vw) = v.shape[:2] (h, w) = img.shape[:2] if not uh == h: u = u[:h - uh, :] if not uw == w: u = u[:, :w - uw] if not vh == h: v = v[:h - vh, :] if not vw == w: v = v[:, :w - vw] u, v = ps4.optic_flow_lk(ps4.expand_image(img) * 2) img = pyr[0] (uh, uw) = u.shape[:2] (vh, vw) = v.shape[:2] (h, w) = img.shape[:2] if not uh == h: u = u[:h - uh, :] if not uw == w: u = u[:, :w - uw] if not vh == h: v = v[:h - vh, :] if not vw == w: v = v[:, :w - vw] return u, v