图像处理100问中的最邻近插值法缩放范围


import cv2
import numpy as np

# Nereset Neighbor interpolation
def nn_interpolate(img, ax, ay):
    H, W, C = img.shape

    aH = round(ay * H)     ** #这里用round,缩放比最大是1.5**
    aW = round(ax * W)    ** #如果用int向下取整,它的缩放比可以达到1.9**

    y = np.arange(aH).repeat(aW).reshape(aW, -1)
    x = np.tile(np.arange(aW), (aH, 1))
    y = np.round(y / ay).astype(np.int)
    x = np.round(x / ax).astype(np.int)

    out = img[y,x]

    out = out.astype(np.uint8)

    return out


# Read image
img = cv2.imread("test1.jpg").astype(np.float)
# Nearest Neighbor
out = nn_interpolate(img, ax=1.5, ay=1.5)

# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()

问题1:该程序的缩放比不能大于2,小于2的时候是可以的,这是为什么呢? 会报错IndexError: index 128 is out of bounds for axis 1 with size 128

问题2:img[y,x]这是什么语法呢?y和x都是二维数组啊。

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。