BasesHomo icon indicating copy to clipboard operation
BasesHomo copied to clipboard

geometricDistance mistake?

Open Zekhire opened this issue 3 years ago • 1 comments

Hello. I would like to ask if there is no error in the geometricDistance function. The function is:

def geometricDistance(correspondence, flow):
    flow = flow.permute(1, 2, 0).cpu().detach().numpy()

    p1 = correspondence[0] # 0
    p2 = correspondence[1] # 1

    if isinstance(correspondence[1][0], float):
        result = p2 - (p1 - flow[int(p1[1]), int(p1[0])])
        error = np.linalg.norm(result)
    else:
        result = [p2 - (p1 - flow[p1[1], p1[0]]), p1 - (p2 - flow[p2[1], p2[0]])]
        error = min(np.linalg.norm(result[0]), np.linalg.norm(result[1]))

    return error

And it is called from :

def compute_eval_results(data_batch, output_batch, manager):

    imgs_full = data_batch["imgs_ori"]
    points = data_batch["points"]
    batch_size, _, grid_h, grid_w = imgs_full.shape

    H_flow_f, H_flow_b = output_batch['H_flow']

    H_flow_f = net.upsample2d_flow_as(H_flow_f, imgs_full, mode="bilinear", if_rate=True)  # scale
    H_flow_b = net.upsample2d_flow_as(H_flow_b, imgs_full, mode="bilinear", if_rate=True)
    img1_full_warp = net.get_warp_flow(imgs_full[:, :3, ...], H_flow_b, start=0)
    img2_full_warp = net.get_warp_flow(imgs_full[:, 3:, ... ], H_flow_f, start=0)

    errs = []
    errs_p = []

    for i in range(len(points)):  # len(points)
        point = eval(points[i])
        err = 0
        tmp = []
        for j in range(6):  # len(point['matche_pts'])
            points_value = point['matche_pts'][j]
            err_p = geometricDistance(points_value, H_flow_f[i])
            err += err_p
            tmp.append(err_p)

        errs.append(err / (j + 1))
        errs_p.append(tmp)
    # ==================================================================== return ======================================================================

    eval_results = {}
    eval_results["img1_full_warp"] = img1_full_warp
    eval_results["errs"] = errs
    eval_results["errs_p"] = errs_p

    return eval_results

For what I am understand right now is that the H_flow_f is flow that transform img2 to img1 and thus p2 to p1, so for each pixel x2 in img2 its location in img1 is:

x1 = x2 + H_flow_f[x2[1], x2[0]]

and thus in the reverse operation:

x2 = x1 - H_flow_f[x2[1], x2[0]]

so I think it should be:

...
    if isinstance(correspondence[1][0], float):
        result = p2 - (p1 - flow[int(p2[1]), int(p2[0])])
        error = np.linalg.norm(result)
    else:
        result = [p2 - (p1 - flow[p2[1], p2[0]]), p1 - (p2 - flow[p1[1], p1[0]])]
        error = min(np.linalg.norm(result[0]), np.linalg.norm(result[1]))
...

Zekhire avatar Jan 13 '22 09:01 Zekhire

hi The sign of flow is determined by the definition of flow Your opinion is correct and it is better to remember to modify the sign of flow in here

thanks ~

CallMeFrozenBanana avatar Aug 11 '22 10:08 CallMeFrozenBanana