def test_layer1(img1, img2):

    settings = SolverSettings()
    settings.alpha = 10 / 200
    init_flow = np.zeros(shape=(2, img1.shape[1], img1.shape[2]))

    width = img2.shape[2]
    height = img2.shape[1]
    start = time()
    plt.figure()
    penalty_func_1 = SquaredPenalty()
    penalty_func_2 = GeneralizedCharbonnierPenalty()

    penalty_func = MixPenalty(penalty_func_1, penalty_func_2, 0)

    gnc_steps = 3

    for iter, lambda_k in zip(range(gnc_steps),
                              [200 / 255, 200 / 255, 200 / 255]):
        print("GNC: ", iter)
        if (gnc_steps > 1):
            mix_factor = iter / (gnc_steps - 1)
        else:
            mix_factor = 0
        settings.weight_kernel = lambda_k
        penalty_func.mix_factor = mix_factor
        for iter2 in range(3):
            init_flow = solve_layer(img1, img2, init_flow, penalty_func,
                                    settings)
        show_flow_field(init_flow, width, height)

    return init_flow
Exemple #2
0
def test_flow():
    flow = read_flow_field(
        r"..\..\..\..\resources\eval-twoframes-groundtruth\Dimetrodon\flow10.flo"
    )

    #flow = scale_flow(flow,int(flow.shape[2]/80),int(flow.shape[1]/80))

    flow_ = np.array([[[1, 2], [5, 6]], [[8, 7], [4, 3]]])

    flow_ = np.full(shape=(2, 4, 4), fill_value=1)

    print(flow)
    show_flow_field(flow, flow.shape[2], flow.shape[1])
    plt.show()

    flow_blur = gaussian_blur_flow(flow, 1)
    show_flow_field(flow_blur, flow.shape[2], flow.shape[1])
    plt.show()
Exemple #3
0
def test_upscale_visual():
    flow = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],
                      [13, 14, 15, 16]],
                     [[8, 7, 6, 5], [4, 3, 2, 1], [12, 11, 10, 9],
                      [13, 14, 15, 16]]])
    factor = 1.5
    width = flow.shape[2]
    height = flow.shape[1]

    plt.title("Original Flow")
    show_flow_field(flow, width, height)

    scaled_flow = upscale_flow(flow, int(width * factor), int(height * factor))

    plt.figure()
    plt.title("Original Flow")
    show_flow_field(scaled_flow, int(width * factor), int(height * factor))
    plt.show()
Exemple #4
0
def test_bilateral_median_filter(img1,img2):
    settings = SolverSettings()
    settings.median_filter_size = -1
    img1 = downscale_image(img1, 0.1)
    img2 = downscale_image(img2, 0.1)
    init_flow = np.zeros(shape=(2, img1.shape[1], img1.shape[2]),dtype = np.double)

    flow = solve_layer(img1, img2, init_flow, settings)

    #flow = flow[:,30:35,50:55]
    #img1 = img1[:,30:35,50:55]
    #img2 = img2[:, 30: 35, 50:55]

    plt.title("Flow")
    show_flow_field(flow, flow.shape[2], flow.shape[1])
    plt.show()


    occlusion = compute_occlusion_log(img1, img2, flow)

    plt.title("Occlusion")
    plt.imshow(np.exp(occlusion))
    plt.show()

    start = time()

    flow = bilateral_median_filter(flow.astype(np.double),occlusion.astype(np.double),init_flow.astype(np.double),img1.astype(np.double),
                                   weigth_auxiliary=2, weigth_filter=6, sigma_distance=5, sigma_color=4,
                                   filter_size=3)
    c_flow = c_filter.bilateral_median_filter(flow.astype(np.double), occlusion.astype(np.double),
                                              init_flow.astype(np.double),
                                              img1.astype(np.double),
                                              weigth_auxiliary=2, weigth_filter=6, sigma_distance=5, sigma_color=4,
                                              filter_size=3)

    print("Bilateral Median Filter Time: ",time()-start)

    return flow,c_flow
Exemple #5
0
    plt.show()

    start = time()

    flow = bilateral_median_filter(flow.astype(np.double),occlusion.astype(np.double),init_flow.astype(np.double),img1.astype(np.double),
                                   weigth_auxiliary=2, weigth_filter=6, sigma_distance=5, sigma_color=4,
                                   filter_size=3)
    c_flow = c_filter.bilateral_median_filter(flow.astype(np.double), occlusion.astype(np.double),
                                              init_flow.astype(np.double),
                                              img1.astype(np.double),
                                              weigth_auxiliary=2, weigth_filter=6, sigma_distance=5, sigma_color=4,
                                              filter_size=3)

    print("Bilateral Median Filter Time: ",time()-start)

    return flow,c_flow

if __name__ == '__main__':
    img1,img2 = grove3()
    flow = test_bilateral_median_filter_bug2()

    #flow[0][flow[0]>7]=0
    #flow[1][flow[1] > 7] = 0
    plt.title("Final")
    print(flow.flatten())
    show_flow_field(flow,flow.shape[2],flow.shape[1],mode="RGB")
    show_flow_field(flow, flow.shape[2], flow.shape[1], mode="Split")


    plt.show()
Exemple #6
0
def solve_layer(first_frame,second_frame, initial_flow_field, solver_settings):
    """

    :param first_frame: np.array(float) shape = (ColorChannel,Height,Width)
    :param second_frame: np.array(float) shape = (ColorChannel,Height,Width)
    :param initial_flow_field: np.array(float) (Flow_Direction, Height,Width)
    :param solver_settings: SolverSettings
    :return: np.array(float) (Flow_Direction, Height,Width)
    """
    #wrap second image
    second_frame_warped = warp_image(second_frame,initial_flow_field)

    A,b = setup_linear_system(first_frame,second_frame_warped,solver_settings)


    start = time()

    solver = solver_settings.solver
    if(solver=="lsmr"):

        x,info = splinalg.lsmr(A,b)[:2]
    elif(solver=="cg"):
        # A = sparse.csr_matrix(A)
        # multilevel = smoothed_aggregation_solver(A)

        # M = multilevel.aspreconditioner(cycle='AMLI')

        x, info = splinalg.cg(A, b, atol=0.001, maxiter=100)
        print("Diff Ax-b: ", np.mean((A.dot(x) - b) ** 2))
    elif(solver=="cg_own"):
        x, info = cg(A, b, tol = 0.001,maxiter=100)
    elif(solver=="bicgstab"):
        x,info =splinalg.bicgstab(A,b)
    elif(solver=="minres"):
        x,info = splinalg.minres(A,b,maxiter=100)[:2]
    elif(solver=="spsolve"):
        A = sparse.csr_matrix(A)
        x = splinalg.spsolve(A,b)

    print("Lg with", solver_settings.solver,": ",time()-start)
    print("Number of iterations: ", info, "\nMean error: ", (b - A.dot(x)).mean())



    width = first_frame.shape[2]
    height = first_frame.shape[1]
    x.shape = (2,height,width)


    flow = x+initial_flow_field

    plt.title("Before Filter")
    show_flow_field(flow, width, height)
    plt.show()
    if(solver_settings.median_filter_size > 0 ):
        #flow[0] = medfilt2d(flow[0],solver_settings.median_filter_size)
        #flow[1] = medfilt2d(flow[1],solver_settings.median_filter_size)

        log_occlusion = compute_occlusion_log(first_frame, first_frame, flow)
        init_flow = np.zeros(shape=(2, first_frame.shape[1], first_frame.shape[2]), dtype=np.double)


        flow = bilateral_median_filter(flow.astype(np.double), log_occlusion.astype(np.double),
                                       flow.astype(np.double), first_frame.astype(np.double),
                                       weigth_auxiliary=0.1, weigth_filter=10, sigma_distance=7, sigma_color=4/255,filter_size = solver_settings.median_filter_size )
        plt.title("After Filter")
        show_flow_field(flow,flow.shape[2],flow.shape[1])
        plt.show()

        #flow[0] = medfilt2d(flow[0], 3)
        #flow[1] = medfilt2d(flow[1],3)

    return flow
Exemple #7
0
def compare_flow(computed_flow, real_flow,current_frame,next_frame, plot=True,arrows=True):
    """

    :param computed_flow: np.array(float) (YX, height_1,width_1)
    :param real_flow: np.array(float) (YX, height_2,width_2)
    :param plot: bool
    :param current_frame: (ChannelCount, height,width)
    :param next_frame: (ChannelCount, height,width)
    :return: (min(height_1,height_2),min(width_1,width_2))
    """
    height_1 = computed_flow.shape[1]
    width_1 = computed_flow.shape[2]
    height_2 = real_flow.shape[1]
    width_2 = real_flow.shape[2]

    width = -1
    height = -1

    if(height_1>height_2 and width_1>width_2):
        computed_flow = down_scale_flow(computed_flow, width_2, height_2)
        width = width_2
        height = height_2
    elif (height_2>height_1 and width_2>width_1):
        real_flow = down_scale_flow(real_flow, width_1, height_1)
        width = width_1
        height = height_1

    elif(height_2==height_1 and width_2==width_1):
        width = width_1
        height=height_1
    else:
        raise Exception("False dimensions: flow_1: (", height_1, ", ", width_1, ") flow_2: (", height_2, ", ", width_2, ")" )

    ang_error = angular_error(computed_flow, real_flow)
    abs_error = absolute_endpoint_error(computed_flow, real_flow)

    print("Average angular error:")
    print(np.mean(ang_error[~np.isnan(ang_error)]))
    print("Median angular error:")
    print(np.mean(ang_error[~np.isnan(ang_error)]))

    print("Absolute endpoint error:")
    print(np.mean(abs_error[~np.isnan(abs_error)]))
    print("Median endpoint error:")
    print(np.median(abs_error[~np.isnan(abs_error)]))


    if(plot):
        #rescale image
        current_frame = scale_image(current_frame,width,height)
        next_frame = scale_image(next_frame, width, height)

        next_frame_warped = warp_image(next_frame,computed_flow)


        plt.figure()
        show_image(current_frame)
        plt.title("Current frame")

        plt.figure()
        show_image(next_frame_warped)
        plt.title("Next frame warped to current frame")

        fig, axs = plt.subplots(1,2)
        axs[0].set_title("Current frame - Next frame squared")
        show_image((current_frame-next_frame)**2,axes=axs[0])

        axs[1].set_title("Current frame - Next frame warped squared")
        show_image((current_frame - next_frame_warped) ** 2,axes=axs[1])

        if (arrows):
            plt.figure()
            show_image(current_frame, axes=plt)
            show_flow_field_arrow(computed_flow, width, height, axes=plt)
            plt.title("Arrows")

        fig, axs = plt.subplots(1,2)

        show_flow_field(computed_flow,width,height,axes=axs[0])
        axs[0].set_title("Computed_flow")
        show_flow_field(real_flow, width, height, axes=axs[1])
        axs[1].set_title("Ground Truth")




        fig, axs = plt.subplots(1,2)
        axs[0].imshow(ang_error)
        axs[0].set_title("Angular error")
        axs[1].imshow(abs_error)
        axs[1].set_title("Absolute endpoint error")

        plt.show()
Exemple #8
0
def test_downscale_visual(flow):
    flow = down_scale_flow(flow, flow.shape[2] / 6, flow.shape[1] / 6)
    width = flow.shape[2]
    height = flow.shape[1]
    show_flow_field(flow, width, height)
    plt.show()