def undistort_halcon_polynomial(u_tilde, v_tilde, k1, k2, k3, p1, p2):
    """
    From the HALCON Docs:
    The polynomial model uses three parameters () to model
    the radial distortions and two parameters () to model
    the decentering distortions.

    The following equations transform the distorted image
    plane coordinates into undistorted image plane coordinates
    if the polynomial model is used:

    These equations cannot be inverted analytically. Therefore,
    distorted image plane coordinates must be calculated from
    undistorted image plane coordinates numerically.

    k1=k2=k3=p1=p2=0 means no distortion.
    """
    u_tilde_to_2 = u_tilde**2
    v_tilde_to_2 = v_tilde**2
    r_to_2 = u_tilde_to_2 + v_tilde_to_2
    r_to_4 = r_to_2**2
    r_to_6 = r_to_4 * r_to_2
    temp1 =  k1 * r_to_2
    temp1 += k2 * r_to_4
    temp1 += k3 * r_to_6
    u = u_tilde + u_tilde * temp1  # the radial part
    v = v_tilde + v_tilde * temp1
    uv_tilde = u_tilde * v_tilde
    u += p1 * (r_to_2 + 2 * u_tilde_to_2) + 2 * p2 * uv_tilde # The tilt part
    v += 2 * p1 * uv_tilde + p2 * (r_to_2 + 2 * v_tilde_to_2)
    return u,v