如何使用“梯度矢量流”获得曲线?

时间:2019-03-04 07:38:37

标签: python numpy image-processing contour image-segmentation

我试图实现“梯度矢量流”以找出曲线,但是我对结果感到困惑,我使用公式得出了矢量场,应该如何获得曲线而不是矢量场? 我该怎么办?

def gradient_vector_flow(fx, fy, iter, mu, dx=1.0, dy=1.0, verbose=True):
    # calc gradient vector flow of input gradient field fx, fy
    b = fx ** 2.0 + fy ** 2.0
    c1, c2 = b * fx, b * fy
    r = 0.25  # (17) r < 1/4 required for convergence.
    dt = dx * dy / (r * mu)
    curr_u = fx
    curr_v = fy

    def laplacian(m):
        return (
            np.hstack([m[:, 0:1], m[:, :-1]])
            + np.hstack([m[:, 1:], m[:, -2:-1]])
            + np.vstack([m[0:1, :], m[:-1, :]])
            + np.vstack([m[1:, :], m[-2:-1, :]])
            - 4 * m
        )

    for i in range(iter):
        next_u = (1.0 - b * dt) * curr_u + r * laplacian(curr_u) + c1 * dt
        next_v = (1.0 - b * dt) * curr_v + r * laplacian(curr_v) + c2 * dt
        curr_u, curr_v = next_u, next_v
    return curr_u, curr_v

0 个答案:

没有答案