예제 #1
0
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
예제 #2
0
def part_4a():
    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.

    levels = 5
    k_type = "gaussian"
    interpolation = cv2.INTER_CUBIC  # You may try different values
    border_mode = cv2.BORDER_REFLECT101  # You may try different values

    u10, v10 = ps4.hierarchical_lk(shift_0, shift_r10, levels, 25, k_type, 1,
                                   interpolation, border_mode)
    u_v = quiver(u10, v10, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-1.png"), u_v)

    # You may want to try different parameters for the remaining function
    # calls.
    u20, v20 = ps4.hierarchical_lk(shift_0, shift_r20, levels, 25, k_type, 2,
                                   interpolation, border_mode)
    u_v = quiver(u20, v20, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-2.png"), u_v)

    u40, v40 = ps4.hierarchical_lk(shift_0, shift_r40, levels, 45, k_type, 3,
                                   interpolation, border_mode)
    u_v = quiver(u40, v40, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-3.png"), u_v)
예제 #3
0
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))
예제 #4
0
def part_4a():
    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.

    levels = 3  # TODO: Define the number of levels
    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
    interpolation = cv2.INTER_CUBIC  # You may try different values
    border_mode = cv2.BORDER_REFLECT101  # You may try different values

    u10, v10 = ps4.hierarchical_lk(shift_0, shift_r10, levels, k_size, k_type,
                                   sigma, interpolation, border_mode)

    u_v = quiver(u10, v10, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-1.png"), u_v)

    # You may want to try different parameters for the remaining function
    # calls.
    u20, v20 = ps4.hierarchical_lk(shift_0, shift_r20, levels, k_size, k_type,
                                   sigma, interpolation, border_mode)

    u_v = quiver(u20, v20, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-2.png"), u_v)

    u40, v40 = ps4.hierarchical_lk(shift_0, shift_r40, levels, k_size, k_type,
                                   sigma, interpolation, border_mode)
    u_v = quiver(u40, v40, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-3.png"), u_v)
예제 #5
0
def part_4a():
    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.

    levels = 2  # TODO: Define the number of levels
    k_size = 0  # TODO: Select a kernel size
    k_type = "gaussian"  # TODO: Select a kernel type
    sigma = 30  # 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

    shift_0 = cv2.GaussianBlur(shift_0, (35, 35), 10)
    shift_r10 = cv2.GaussianBlur(shift_r10, (35, 35), 10)

    u, v = ps4.hierarchical_lk(shift_0, shift_r10, levels, k_size, k_type,
                               sigma, interpolation, border_mode)
    q = 5.0
    u = u / q
    v = v / q
    #
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-1.png"), u_v)
    #
    # # You may want to try different parameters for the remaining function
    # # calls.
    levels = 3
    shift_r20 = cv2.GaussianBlur(shift_r20, (35, 35), 10)
    u, v = ps4.hierarchical_lk(shift_0, shift_r20, levels, k_size, k_type,
                               sigma, interpolation, border_mode)

    q = 10.0
    u = u / q
    v = v / q

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

    #

    levels = 3
    shift_r40 = cv2.GaussianBlur(shift_r40, (35, 35), 10)
    u, v = ps4.hierarchical_lk(shift_0, shift_r40, levels, k_size, k_type,
                               sigma, interpolation, border_mode)
    q = 15.0
    u = u / q
    v = v / q
    u_v = quiver(u, v, scale=3, stride=10)
    cv2.imwrite(os.path.join(output_dir, "ps4-4-a-3.png"), u_v)
예제 #6
0
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))
예제 #7
0
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))
예제 #8
0
def part_6():
    """Challenge Problem

    Follow the instructions in the problem set instructions.

    Place all your work in this file and this section.
    """
    skip = 2
    video1 = 'input_videos/racecar.mp4'
    print video1

    image_gen1 = video_frame_generator(video1)
    image1 = image_gen1.next()
    for _ in range(skip):
        image2 = image_gen1.next()

    h1, w1 = image1.shape[:2]

    frame_num = 1
    out_path = "output/optic_flow_racecar.mp4"
    video_out = mp4_video_writer(out_path, (w1, h1), fps=20)

    frame_ct = 0
    while (image2 is not None) and (frame_ct < 300):
        frame_ct += 1
        print "Processing frame {}".format(frame_num)

        levels = 5  # TODO: Define the number of levels
        k_size = 50  # TODO: Select a kernel size
        k_type = "gaussian"  # TODO: Select a kernel type
        sigma = 20  # 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

        image1bw = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
        image2bw = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
        image1bw = ps4.normalize_and_scale(image1bw * 1., scale_range=(0, 1))
        image2bw = ps4.normalize_and_scale(image2bw * 1., scale_range=(0, 1))

        u20, v20 = ps4.hierarchical_lk(image1bw, image2bw, levels, k_size,
                                       k_type, sigma, interpolation,
                                       border_mode)
        u_v = quiver(u20, v20, scale=3, stride=20)

        image_out = image1.copy()
        image_out[np.where(u_v > 10)] = 255
        if frame_ct == 30:
            cv2.imwrite(os.path.join(output_dir, "ps4-6-a-1.png"), image_out)
        if frame_ct == 200:
            cv2.imwrite(os.path.join(output_dir, "ps4-6-a-2.png"), image_out)

        video_out.write(image_out)
        image1 = image2.copy()
        for _ in range(skip):
            image2 = image_gen1.next()

        frame_num += 1

    video_out.release()
예제 #9
0
def part_6():
    """Challenge Problem

    Follow the instructions in the problem set instructions.

    Place all your work in this file and this section.
    """
    k_size = 25
    levels = 3
    interpolation = cv2.INTER_CUBIC  # You may try different values
    border_mode = cv2.BORDER_REFLECT101  # You may try different values

    input_video_path = os.path.join('input_videos', 'ps4-my-video.mp4')
    output_video_path = os.path.join(output_dir, 'ps4-6.mp4')

    video_in = video_frame_generator(input_video_path)

    prev_frame = video_in.next()

    h, w, d = prev_frame.shape

    fourcc = cv2.cv.CV_FOURCC(*'mp4v')
    video_out = cv2.VideoWriter(output_video_path, fourcc, 20, (w, h))

    export_frames = [50, 100]
    n_export = 1

    i = 0

    while 1:
        print "processing: " + str(i)

        next_frame = video_in.next()
        if next_frame is None:
            break

        u, v = ps4.hierarchical_lk(to_gray_float(prev_frame),
                                   to_gray_float(next_frame), levels, k_size,
                                   'uniform', 0, interpolation, border_mode)

        u_v = quiver(u, v, scale=3, stride=20)

        out_frame = prev_frame.copy()
        out_frame[u_v[:, :, 1] != 0] = (0, 255, 0)

        if i in export_frames:
            cv2.imwrite(
                os.path.join(output_dir, "ps4-6-a-{}.png".format(n_export)),
                out_frame)
            n_export += 1

        video_out.write(out_frame)

        prev_frame = next_frame
        i += 1

    video_out.release()
예제 #10
0
def helper_for_part_6(video_name, fps, frame_ids, output_prefix, counter_init):

    video = os.path.join(video_dir, video_name)
    image_gen = video_frame_generator(video)

    image_a = image_gen.next()  # frame 1
    image_b = image_gen.next()

    h, w, d = image_a.shape

    levels = 8
    k_size = 41
    k_type = 'uniform'
    interpolation = cv2.INTER_CUBIC  # You may try different values
    border_mode = cv2.BORDER_REFLECT101  # You may try different values

    out_path = "output/ar_{}-{}".format(output_prefix[4:], video_name)

    video_out = mp4_video_writer(out_path, (w, h), fps)
    output_counter = counter_init

    frame_num = 1
    output_counter = 0
    output_frame = frame_ids[output_counter]

    while image_b is not None:
        a = cv2.cvtColor(image_a.copy(), cv2.COLOR_BGR2GRAY)
        b = cv2.cvtColor(image_b.copy(), cv2.COLOR_BGR2GRAY)

        u, v = ps4.hierarchical_lk(a, b, levels, k_size, k_type, 0,
                                   interpolation, border_mode)

        image = quiver(u, v, scale=3, stride=10, base_image=image_a)

        if output_counter < len(frame_ids) and frame_num == output_frame:
            print 'Saving image of {}'.format(frame_num)
            out_str = "frame-{}.png".format(frame_num)
            save_image(out_str, image)

            output_counter += 1
            if output_counter < len(frame_ids):
                output_frame = frame_ids[output_counter]

            print 'Saving Next {} : Data of {} and {}'.format(
                output_frame, output_counter, len(frame_ids))

        # video_out.write(image)
        image_a = image_b.copy()
        image_b = image_gen.next()
        # save_image('b.png', image_b)
        # image_b = cv2.imread('output/b.png', 0) / 1.
        frame_num += 1

        print 'Frame: {}'.format(frame_num)

    video_out.release()
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(vid_dir, "ps4-my-video.mp4")
    frame_gen = video_frame_generator(video)
    #for i in range(150):
    frame1 = frame_gen.next()

    frame2 = frame_gen.next()

    h, w = frame1.shape[:2]

    out_path = os.path.join(output_dir, "video_out.mp4")
    video_out = mp4_video_writer(out_path, (w, h), fps=40)

    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 = 5
    print "img shape: {}".format(frame1.shape)
    cv2.imwrite(os.path.join(output_dir, "frame1.png"), frame1)
    frame_num = 1
    while frame2 is not None and frame1 is not None and frame_num <= 1000:
        print "Processing Frame {}".format(frame_num)
        img1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) / 255.0
        img2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) / 255.0
        img1 = cv2.GaussianBlur(img1, (15, 15), 0.05)
        img2 = cv2.GaussianBlur(img2, (15, 15), 0.05)
        #img1 = ps4.normalize_and_scale(img1)
        #img2 = ps4.normalize_and_scale(img2)

        u, v = ps4.hierarchical_lk(img1, img2, levels, k_size, k_type, sigma,
                                   interpolation, border_mode)
        quiver_image = quiver_img(frame1, u, v, scale=3, stride=10)
        #print "u shape: {}".format(u.shape)
        #print "v shape: {}".format(v.shape)
        if frame_num == 50:
            u_v = quiver_img(frame1, u, v, scale=3, stride=10)
            cv2.imwrite(os.path.join(output_dir, "ps4-6-a-1.png"), u_v)
        elif frame_num == 100:
            u_v = quiver_img(frame1, u, v, scale=3, stride=10)
            cv2.imwrite(os.path.join(output_dir, "ps4-6-a-2.png"), u_v)

        frame1 = frame2
        frame2 = frame_gen.next()

        video_out.write(quiver_image)
        frame_num += 1
예제 #12
0
def helper_for_part_6(video_name, fps, frame_ids, output_prefix, counter_init):

    video = os.path.join(VID_DIR, video_name)
    image_gen = video_frame_generator(video)

    image = image_gen.__next__()
    image_pre = image
    h, w, d = image.shape

    out_path = "output/ar_{}-{}".format(output_prefix[4:], video_name)
    video_out = mp4_video_writer(out_path, (w, h), fps)

    output_counter = counter_init

    frame_num = 1

    while image is not None:

        print("Processing fame {}".format(frame_num))

        image_pre_grey = cv2.cvtColor(image_pre, cv2.COLOR_BGR2GRAY)
        image_grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        levels = 4
        k_size = 75
        k_type = "uniform" 
        sigma = 0.1
        interpolation = cv2.INTER_CUBIC 
        border_mode = cv2.BORDER_REFLECT101
        u, v = ps4.hierarchical_lk(image_pre_grey, image_grey, levels, k_size, k_type,
                                   sigma, interpolation, border_mode)
        u_v = quiver(u, v, scale=0.1, stride=20, color=(255, 255, 255))

        res = np.copy(image)

        res = cv2.addWeighted(res,0.7, u_v, 1, 0.7)

        frame_id = frame_ids[(output_counter - 1) % 2]

        if frame_num == frame_id:
            out_str = output_prefix + "-{}.png".format(output_counter)
            save_image(out_str, res)
            output_counter += 1

        video_out.write(res)

        image_pre = image
        image = image_gen.__next__()

        frame_num += 1

    video_out.release()
예제 #13
0
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
예제 #14
0
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))
예제 #15
0
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
    k_size = 91  # 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
    levels = 4
    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_r10, k_size, k_type, sigma)

    u10, v10 = ps4.hierarchical_lk(shift_0, shift_r10, levels, k_size, k_type,
                                   sigma, interpolation, border_mode)

    u_v = quiver(u10, v10, scale=3, stride=10)
    # cv2.imwrite(os.path.join(output_dir, "out/ps4-5-a-1.png"), u_v)

    # cv2.imwrite("out/I_0.png", shift_0*255)

    I_t_02 = interpolate_frames(shift_0, shift_r10, 0.2, u10, v10)
    # cv2.imwrite("out/I_t_02.png", ps4.normalize_and_scale(I_t_02.astype(np.float)))
    I_t_04 = interpolate_frames(shift_0, shift_r10, 0.4, u10, v10)
    # cv2.imwrite("out/I_t_04.png", ps4.normalize_and_scale(I_t_04.astype(np.float)))
    I_t_06 = interpolate_frames(shift_0, shift_r10, 0.6, u10, v10)
    # cv2.imwrite("out/I_t_06.png", I_t_06.astype(np.float) * 255)
    I_t_08 = interpolate_frames(shift_0, shift_r10, 0.8, u10, v10)
    # cv2.imwrite("out/I_t_08.png", I_t_08.astype(np.float) * 255)

    # cv2.imwrite("out/I_1.png", shift_r10*255)

    result = create_2_by_3_image(shift_0, I_t_02, I_t_04, I_t_06, I_t_08,
                                 shift_r10)

    cv2.imwrite("ps4-5-a-1.png", result * 255)
예제 #16
0
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
예제 #17
0
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))
예제 #18
0
def part_4a():
    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.

    # Create a window
    cv2.namedWindow('image')
    u_v = np.zeros(shift_0.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(shift_0, shift_r20, levels, k_size, k_type,
                                   sigma, interpolation, border_mode)

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

    cv2.destroyAllWindows()
    print levels, k_size, k_type, sigma
예제 #19
0
    def test_optic_flow_HLK(self):

        for i in range(3):

            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.hierarchical_lk(img1.copy(), img2.copy(), 3,
                                       self.k_size, self.k_type, 1.,
                                       cv2.INTER_CUBIC, cv2.BORDER_REFLECT101)

            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]])

            max_diff = abs(d_c) * .1 + .2
            check_u = abs(u_mean - d_c) < max_diff

            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]])

            max_diff = abs(d_r) * .1 + .2
            check_v = abs(v_mean - d_r) < max_diff

            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)
예제 #20
0
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))
예제 #21
0
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))
예제 #22
0
def part_6():
    """Challenge Problem

    Follow the instructions in the problem set instructions.

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

    IMG_DIR = "input_images"
    VID_DIR = "input_videos"
    OUT_DIR = "output"
    if not os.path.isdir(OUT_DIR):
        os.makedirs(OUT_DIR)

    video_file = "ps4-my-video.mp4"
    fps = 40

    counter_init = 1
    output_prefix = 'part_6'

    # Todo: Complete this part on your own.'
    video = os.path.join(VID_DIR, video_file)
    image_gen = video_frame_generator(video)
    image1 = image_gen.__next__()
    image2 = image_gen.__next__()
    h, w, d = image1.shape

    out_path = "output/ar_{}-{}".format('part_6', 'quiver.mp4')
    video_out = mp4_video_writer(out_path, (w, h), fps)

    output_counter = counter_init

    frame_num = 1
    interpolation = cv2.INTER_CUBIC  # You may try different values
    border_mode = cv2.BORDER_REFLECT101  # You may try different values

    while image2 is not None:

        print("Processing fame {}".format(frame_num))
        k = 15
        sigma = 7
        image1g = cv2.GaussianBlur(image1,
                                   ksize=(k, k),
                                   sigmaX=sigma,
                                   sigmaY=sigma)
        image2g = cv2.GaussianBlur(image2,
                                   ksize=(k, k),
                                   sigmaX=sigma,
                                   sigmaY=sigma)

        levels = 4
        k_size = 30
        u20, v20 = ps4.hierarchical_lk(image1g[:, :, 0] / 255.,
                                       image2g[:, :, 0] / 255., 4, k_size,
                                       k_type, sigma, interpolation,
                                       border_mode)

        u_v = quiver_(u20, v20, image1, scale=3, stride=10)

        frame_id = frame_ids[(output_counter - 1) % 3]

        if frame_num == frame_id:
            out_str = output_prefix + "-{}.png".format(output_counter)
            save_image(out_str, u_v)
            output_counter += 1

        video_out.write(u_v)

        image1 = image2.copy()
        image2 = image_gen.__next__()

        frame_num += 1

    video_out.release()
예제 #23
0
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)
예제 #24
0
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))
예제 #25
0
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)
예제 #26
0
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))
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)