一、几何变换

二、插值

# 将 [left, right] 区间上的值 val 映射到 [newleft, newright] 上,返回映射结果。
# 支持数组形式的 newleft, newright。
# isclamp 提供超界截止功能(这里没有使用)。
def rmap(val, left, right, newleft, newright, isclamp=False):
    if (right == left):
        return newleft
    else:
        if (not isclamp):
            return newleft + (val - left) / (right - left) * (newright - newleft)
        else:
            if (right > left and val < left) or (left > right and val > left):
                return newleft
            elif (right > left and val > right) or (left > right and val < right):
                return newright
            else:
                return newleft + (val - left) / (right - left) * (newright - newleft)

# 双线性插值、重复模式,获取图像 img 在 x, y(可为int或float)点处的值。
# 留意图像纵轴向下为 +y,横轴向右为 +x。单通道、多通道图像均可。
def bilinear_repeat_get_img_val(img, x, y):
    ### repeat
    R = img.shape[0]
    C = img.shape[1]
    while (y >= R):
        y -= R
    while (y < 0):
        y += R
    while (x >= C):
        x -= C
    while (x < 0):
        x += C
    ### bilinear
    left = int(np.floor(x))
    right = int(np.ceil(x))
    if right >= C:
        right -= C
    up = int(np.floor(y))
    down = int(np.ceil(y))
    if down >= R:
        down -= R
    up_color = rmap(x, left, right, img[up, left], img[up, right])
    down_color = rmap(x, left, right, img[down, left], img[down, right])
    return rmap(y, up, down, up_color, down_color)