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都是二维数组啊。