Beispiel #1
0
 def optik_flowlk_write(img0, img1, iname, k_size, k_type, sigma, gks,
                        gksd):
     #Try blurring your images or smoothing your results
     img0 = cv2.GaussianBlur(img0, (gks, gks), gksd)
     img1 = cv2.GaussianBlur(img1, (gks, gks), gksd)
     U, V = ps4.optic_flow_lk(img0, img1, k_size, k_type, sigma)
     # Flow image
     u_v = quiver(U, V, scale=3, stride=10)
     cv2.imwrite(os.path.join(output_dir, iname), 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 = 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))
Beispiel #3
0
def part_1a():

    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.
    shift_r5_u5 = cv2.imread(
        os.path.join(input_dir, 'TestSeq', 'ShiftR5U5.png'), 0) / 255.

    # Optional: smooth the images if LK doesn't work well on raw images
    k_size = 55
    k_type = ""
    sigma = 5
    u, v = ps4.optic_flow_lk(shift_0, shift_r2, k_size, k_type, sigma)

    q = 0.5
    u = u / q
    v = v / q

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-a-1.png"), u_v)

    # Now let's try with ShiftR5U5. You may want to try smoothing the
    # input images first.

    k_size = 81
    k_type = ""
    sigma = 0
    u, v = ps4.optic_flow_lk(shift_0, shift_r5_u5, k_size, k_type, sigma)

    q = .5
    u = u / q
    v = v / q

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-a-2.png"), u_v)
Beispiel #4
0
def part_1():
    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.
    shift_r5_u5 = cv2.imread(
        os.path.join(input_dir, 'TestSeq', 'ShiftR5U5.png'), 0) / 255.
    shift_r10 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR10.png'),
                           0) / 255.
    shift_r20 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR20.png'),
                           0) / 255.
    shift_r40 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR40.png'),
                           0) / 255.

    # Create a black image, a window
    u_v = np.zeros(shift_0.shape, np.uint8)
    cv2.namedWindow('image')

    # create trackbars for color change
    cv2.createTrackbar('k_size', 'image', 3, 70, nothing)
    cv2.createTrackbar('k_type', 'image', 0, 1, nothing)
    cv2.createTrackbar('sigma', 'image', 1, 50, nothing)
    cv2.createTrackbar('blur', 'image', 0, 1, nothing)
    cv2.createTrackbar('blur_size', 'image', 1, 20, 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
        k_size = cv2.getTrackbarPos('k_size', 'image')
        k_type = 'gaussian' if cv2.getTrackbarPos('k_type',
                                                  'image') == 1 else 'uniform'
        sigma = cv2.getTrackbarPos('sigma', 'image')
        blur = cv2.getTrackbarPos('blur', 'image')
        blur_size = cv2.getTrackbarPos('blur_size', 'image')

        img_1 = blur_img(shift_0, blur_size, blur)
        img_2 = blur_img(shift_r10, blur_size, blur)

        u, v = ps4.optic_flow_lk(img_1, img_2, k_size, k_type, sigma)

        # Flow image
        u_v = experiment.quiver(u, v, scale=3, stride=10)

    cv2.destroyAllWindows()
    print k_size, k_type, sigma, blur_size, blur
Beispiel #5
0
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
Beispiel #6
0
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 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 test_optic_flow_LK(self):

        for i in range(3):
            print i

            f1 = self.input_imgs_1[i]
            f2 = self.input_imgs_2[i]

            img1 = cv2.imread(INPUT_DIR + f1, 0) / 255.
            img2 = cv2.imread(INPUT_DIR + f2, 0) / 255.

            u, v = ps4.optic_flow_lk(img1.copy(), img2.copy(), self.k_size,
                                     self.k_type, 1.)

            r = self.r_val[i]
            c = self.c_val[i]

            d_c = self.delta_c[i]
            d_r = self.delta_r[i]

            center_box = self.cb[i]

            u_mean = np.mean(u[r:r + center_box[0], c:c + center_box[1]])

            check_u = abs(u_mean - d_c) <= 0.5

            error_msg = "Average of U values in the area where there is " \
                        "movement is greater than the allowed amount."

            self.assertTrue(check_u, error_msg)

            v_mean = np.mean(v[r:r + center_box[0], c:c + center_box[1]])

            check_v = abs(v_mean - d_r) <= 0.5

            error_msg = "Average of V values in the area where there is " \
                        "movement is greater than the allowed amount."

            self.assertTrue(check_v, error_msg)
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.

    k = 11
    sigma = 7
    yos_img_02g = cv2.GaussianBlur(yos_img_02,
                                   ksize=(k, k),
                                   sigmaX=sigma,
                                   sigmaY=sigma)
    yos_img_03g = cv2.GaussianBlur(yos_img_03,
                                   ksize=(k, k),
                                   sigmaX=sigma,
                                   sigmaY=sigma)

    levels = 5  # Define the number of pyramid levels
    yos_img_02_g_pyr = ps4.gaussian_pyramid(yos_img_02g, levels)
    yos_img_03_g_pyr = ps4.gaussian_pyramid(yos_img_03g, levels)

    level_id = 0  # TODO: Select the level number (or id) you wish to use
    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
    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_1b():
    """Performs the same operations applied in part_1a using the images
    ShiftR10, ShiftR20 and ShiftR40.

    You will compare the base image Shift0.png with the remaining
    images located in the directory TestSeq:
    - ShiftR10.png
    - ShiftR20.png
    - ShiftR40.png

    Make sure you explore different parameters and/or pre-process the
    input images to improve your results.

    In this part you should save the following images:
    - ps4-1-b-1.png
    - ps4-1-b-2.png
    - ps4-1-b-3.png

    Returns:
        None
    """
    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.
    shift_r20 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR20.png'),
                           0) / 255.
    shift_r40 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR40.png'),
                           0) / 255.

    # blur images
    gkernel = (25, 25)
    gsigma = 15
    shift_0_blur = cv2.GaussianBlur(shift_0, gkernel, gsigma)
    shift_r10_blur = cv2.GaussianBlur(shift_r10, gkernel, gsigma)
    Shift_r20_blur = cv2.GaussianBlur(shift_r20, gkernel, gsigma)
    Shift_r40_blur = cv2.GaussianBlur(shift_r40, gkernel, gsigma)
    k_type = "uniform"
    sigma = 1

    # shift_r10
    k_size = 71
    u, v = ps4.optic_flow_lk(shift_0_blur, shift_r10_blur, k_size, k_type,
                             sigma)
    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-1.png"), u_v)

    k_size = 71
    u, v = ps4.optic_flow_lk(shift_0_blur, Shift_r20_blur, k_size, k_type,
                             sigma)
    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-2.png"), u_v)

    # shift_r40
    k_size = 71
    u, v = ps4.optic_flow_lk(shift_0_blur, Shift_r40_blur, k_size, k_type,
                             sigma)
    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-3.png"), u_v)
def part_1b():
    """Performs the same operations applied in part_1a using the images
    ShiftR10, ShiftR20 and ShiftR40.

    You will compare the base image Shift0.png with the remaining
    images located in the directory TestSeq:
    - ShiftR10.png
    - ShiftR20.png
    - ShiftR40.png

    Make sure you explore different parameters and/or pre-process the
    input images to improve your results.

    In this part you should save the following images:
    - ps4-1-b-1.png
    - ps4-1-b-2.png
    - ps4-1-b-3.png

    Returns:
        None
    """
    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.
    shift_r20 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR20.png'),
                           0) / 255.
    shift_r40 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR40.png'),
                           0) / 255.

    # Optional: smooth the images if LK doesn't work well on raw images
    k = 31
    sigma = 9
    shift_0g = cv2.GaussianBlur(shift_0,
                                ksize=(k, k),
                                sigmaX=sigma,
                                sigmaY=sigma)
    shift_r10g = cv2.GaussianBlur(shift_r10,
                                  ksize=(k, k),
                                  sigmaX=sigma,
                                  sigmaY=sigma)

    k_size = 65  # 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(shift_0g, shift_r10g, k_size, k_type, sigma)

    # Flow image
    u_v = quiver(u, v, scale=0.6, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-1.png"), u_v)

    # Now let's try with ShiftR5U5. You may want to try smoothing the
    k = 31
    sigma = 15
    shift_0g = cv2.GaussianBlur(shift_0,
                                ksize=(k, k),
                                sigmaX=sigma,
                                sigmaY=sigma)
    shift_r20g = cv2.GaussianBlur(shift_r20,
                                  ksize=(k, k),
                                  sigmaX=sigma,
                                  sigmaY=sigma)

    k_size = 90  # 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(shift_0g, shift_r20g, k_size, k_type, sigma)

    # Flow image
    u_v = quiver(u, v, scale=0.5, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-2.png"), u_v)

    # Now let's try with ShiftR5U5. You may want to try smoothing the
    k = 61
    sigma = 15
    shift_0g = cv2.GaussianBlur(shift_0,
                                ksize=(k, k),
                                sigmaX=sigma,
                                sigmaY=sigma)
    shift_r40g = cv2.GaussianBlur(shift_r40,
                                  ksize=(k, k),
                                  sigmaX=sigma,
                                  sigmaY=sigma)

    k_size = 120  # 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(shift_0g, shift_r40g, k_size, k_type, sigma)

    # Flow image
    u_v = quiver(u, v, scale=0.25, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-3.png"), u_v)
Beispiel #12
0
def part_1b():
    """Performs the same operations applied in part_1a using the images
    ShiftR10, ShiftR20 and ShiftR40.

    You will compare the base image Shift0.png with the remaining
    images located in the directory TestSeq:
    - ShiftR10.png
    - ShiftR20.png
    - ShiftR40.png

    Make sure you explore different parameters and/or pre-process the
    input images to improve your results.

    In this part you should save the following images:
    - ps4-1-b-1.png
    - ps4-1-b-2.png
    - ps4-1-b-3.png

    Returns:
        None
    """
    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.
    shift_r20 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR20.png'),
                           0) / 255.
    shift_r40 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR40.png'),
                           0) / 255.

    # r10

    img1 = blur_img(shift_0, blur_size=6)
    img2 = blur_img(shift_r10, blur_size=6)

    k_size = 56
    k_type = "uniform"
    sigma = 0
    u, v = ps4.optic_flow_lk(img1, img2, k_size, k_type, sigma)

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-1.png"), u_v)

    # r20

    img1 = blur_img(shift_0, blur_size=10)
    img2 = blur_img(shift_r20, blur_size=10)

    k_size = 38
    k_type = "uniform"
    sigma = 0
    u, v = ps4.optic_flow_lk(img1, img2, k_size, k_type, sigma)

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-2.png"), u_v)

    # r40

    img1 = blur_img(shift_0, blur_size=12)
    img2 = blur_img(shift_r40, blur_size=12)

    k_size = 47
    k_type = "uniform"
    sigma = 0
    u, v = ps4.optic_flow_lk(img1, img2, k_size, k_type, sigma)

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-3.png"), u_v)
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
Beispiel #14
0
def part_6():
    """Challenge Problem

    Follow the instructions in the problem set instructions.

    Place all your work in this file and this section.
    """

    video = os.path.join("input_videos", "ps4-my-video.mp4")
    image_gen = ps4.video_frame_generator(video)

    current = image_gen.__next__()
    current_resized = cv2.resize(current, (380, 760))
    next = image_gen.__next__()

    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    video_out = cv2.VideoWriter("output.mp4", fourcc, 20, (380, 760))

    frame_num = 1

    while next is not None:
        # print("Processing fame {}".format(frame_num))

        next_resized = cv2.resize(next, (380, 760))

        k_size = 25
        k_type = ""

        gray_current = cv2.cvtColor(current_resized, cv2.COLOR_BGR2GRAY) / 255
        gray_next = cv2.cvtColor(next_resized, cv2.COLOR_BGR2GRAY) / 255

        if frame_num > 20:
            cv2.imwrite("out/current.png", current_resized)
            cv2.imwrite("out/next.png", next_resized)

        gray_current = cv2.GaussianBlur(gray_current, (35, 35), 10)
        gray_next = cv2.GaussianBlur(gray_next, (35, 35), 10)
        u, v = ps4.optic_flow_lk(gray_current, gray_next, k_size, k_type, 0)

        u_abs = abs(u)
        v_abs = abs(v)

        # m = np.max([u_abs.max(), v_abs.max()])
        m = np.max([np.average(u_abs), np.average(v_abs)])
        print("Processing fame {}".format(frame_num), np.average(u), u.max(),
              np.average(v), v.max())

        u = u / (m)
        v = v / (m)

        u_v = quiver(u, v, scale=3, stride=10)
        cv2.imwrite("out/video/quiver_" + str(frame_num) + ".png", u_v)

        red = current_resized[:, :, 2]
        red[u_v[:, :, 1] > 254] = 255
        current_resized[:, :, 2] = red
        cv2.imwrite("out/frame/frame" + str(frame_num) + ".png",
                    current_resized)
        video_out.write(current_resized)

        current_resized = next_resized
        next = image_gen.__next__()
        if next is not None:
            next_resized = cv2.resize(next, (380, 760))

        frame_num += 1

    video_out.release()
Beispiel #15
0
def part_1b():
    """Performs the same operations applied in part_1a using the images
    ShiftR10, ShiftR20 and ShiftR40.

    You will compare the base image Shift0.png with the remaining
    images located in the directory TestSeq:
    - ShiftR10.png
    - ShiftR20.png
    - ShiftR40.png

    Make sure you explore different parameters and/or pre-process the
    input images to improve your results.

    In this part you should save the following images:
    - ps4-1-b-1.png
    - ps4-1-b-2.png
    - ps4-1-b-3.png

    Returns:
        None
    """
    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.
    shift_r20 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR20.png'),
                           0) / 255.
    shift_r40 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR40.png'),
                           0) / 255.

    k_size = 171
    k_type = "gaussian"
    sigma = 60
    shift_0 = cv2.GaussianBlur(shift_0, (35, 35), 10)
    shift_r10 = cv2.GaussianBlur(shift_r10, (35, 35), 10)
    u, v = ps4.optic_flow_lk(shift_0, shift_r10, k_size, k_type, sigma)

    q = 4.0
    u = u / q
    v = v / q

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-1.png"), u_v)

    k_size = 181
    k_type = "gaussian"
    sigma = 60
    shift_r20 = cv2.GaussianBlur(shift_r20, (35, 35), 10)
    u, v = ps4.optic_flow_lk(shift_0, shift_r20, k_size, k_type, sigma)

    q = 3.5
    u = u / q
    v = v / q

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-2.png"), u_v)

    k_size = 181
    k_type = "ave"
    sigma = 60
    shift_0 = cv2.GaussianBlur(shift_0, (25, 25), 15)
    shift_r40 = cv2.GaussianBlur(shift_r40, (25, 25), 15)
    shift_r40 = cv2.GaussianBlur(shift_r40, (25, 25), 15)
    u, v = ps4.optic_flow_lk(shift_0, shift_r40, k_size, k_type, sigma)

    q = 2.0
    u = u / q
    v = v / q

    # Flow image
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-3.png"), u_v)
Beispiel #16
0
def part_1b():
    """Performs the same operations applied in part_1a using the images
    ShiftR10, ShiftR20 and ShiftR40.

    You will compare the base image Shift0.png with the remaining
    images located in the directory TestSeq:
    - ShiftR10.png
    - ShiftR20.png
    - ShiftR40.png

    Make sure you explore different parameters and/or pre-process the
    input images to improve your results.

    In this part you should save the following images:
    - ps4-1-b-1.png
    - ps4-1-b-2.png
    - ps4-1-b-3.png

    Returns:
        None
    """
    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.
    shift_r20 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR20.png'),
                           0) / 255.
    shift_r40 = cv2.imread(os.path.join(input_dir, 'TestSeq', 'ShiftR40.png'),
                           0) / 255.

    k_type = 'uniform'
    sigma = 0.5

    # 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
    #
    #     k_size = cv2.getTrackbarPos('kSize', 'Params')
    #     u, v = ps4.optic_flow_lk(shift_0, shift_r20, k_size, k_type, sigma)
    #     u_v = quiver(u, v, scale=3, stride=10)
    #     cv2.imshow("Params", u_v)

    # change k size per thing to see impact.
    k_size = 41
    u, v = ps4.optic_flow_lk(shift_0, shift_r10, k_size, k_type, sigma)
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-1.png"), u_v)

    k_size = 63
    u, v = ps4.optic_flow_lk(shift_0, shift_r20, k_size, k_type, sigma)
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-2.png"), u_v)

    k_size = 63
    u, v = ps4.optic_flow_lk(shift_0, shift_r40, k_size, k_type, sigma)
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-1-b-3.png"), u_v)