简单线性插值采样

388 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

简单线性插值采样

def LinearSampling(A, B):
    x0 = A[0]
    x1 = B[0]
    y0 = A[1]
    y1 = B[1]
    if x1 - x0 == 0: #在同一条竖直线上
        pos = np.array(list(zip([x0] * 256, np.linspace(y0, y1, 256) + 0.5)))
        pos = pos.astype(np.int32) # 采样为整数
        return np.unique(pos, axis=0)
    else: #其他任意情况
        k = (y1 - y0) / (x1 - x0)

        def func(x):
            return k * (x - x0) + y0

        x_v = np.linspace(x0, x1, 256)
        pos = np.array(list(zip(x_v, func(x_v) + 0.5)))
        pos = pos.astype(np.int32)
        return np.unique(pos, axis=0)

256个采样点,最后将重复的点合并,并返回采样后的的点数组 函数的两个参数分别是两个仅有两个元素的数组[x, y] 返回值是

[[x0,y0],[x1,y1],[x3,y3]...][ [x_0, y_0], [x_1, y_1], [x_3, y_3] ...]