问题描述
-
我正在尝试实现最近邻居插值技术,以在Python中缩放图像。
-
当比例因子小于2 时,我的代码似乎可以运行。
-
否则,我得到以下错误:
...\NN_Zoom\NN.py",line 50,in NN_interpolation zoom[i][j] = im[near_neighbour(X,P)[0]][near_neighbour(X,P)[1]] IndexError: index 600 is out of bounds for axis 0 with size 600
例如:缩放形状为:(1500,1500)图像形状为:(600,600)
#==================================
# Import Libraries
#==================================
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
#----------------------------------
# Euclidian distance
#==================================
def euclidian_dist(a,b):
'''
Euclidian distance between 2 points a(x_a,y_a) and b(x_b,y_b)
distance = Square Root ( (x_a - x_b)^2 + (y_a - y_b)^2 )
'''
return np.sqrt(((a[0]-b[0])**2)+((a[1]-b[1])**2))
#----------------------------------
# Nearest Neighbour
#==================================
def near_neighbour(X,P):
'''
The nearest neighbour of point X(x,y) to the centroid P(x_p,y_p)
The Neighbourhood is defined by the Upper-Left corner of the point X,which means 3 neighbours and the point X.
'''
i,j = X[0],X[1]
A = [[i,j],[i,j+1],[i+1,j+1]]
dist = [euclidian_dist(A[0],P),euclidian_dist(A[1],euclidian_dist(A[2],euclidian_dist(A[3],P)]
minpos = dist.index(min(dist))
return A[minpos]
#----------------------------------
# Nearest Neighbour Interpolation
#==================================
def NN_interpolation(im,scale_factor):
'''
Interpolation of the image im with scale factor scale_factor,using Nearest Neighbour.
'''
row,col = im.shape[0],im.shape[1]
n_row,n_col = int(scale_factor * row),int(scale_factor * col)
# fill in img
zoom = np.arange(n_row*n_col).reshape(n_row,n_col)
print("zoom shape is: ",zoom.shape,"image shape is: ",im.shape,'\n')
for i in range(n_row):
for j in range(n_col):
P = [float(i)/scale_factor,float(j)/scale_factor]
X = [int(i) for i in P]
zoom[i][j] = im[near_neighbour(X,P)[1]]
return zoom
#-------------------------
# Example
#=========================
im = imread('image.png')[...,0]
J = NN_interpolation(im,1.5)
plt.figure(num='NN-Interpolation')
plt.subplot(121)
imgplot = plt.imshow(im,cmap="gray") # displaying the image
plt.title('Original')
plt.subplot(122)
imgplot = plt.imshow(J,cmap="gray") # displaying the image
plt.title('Zoomed')
plt.show()
print(im.shape,J.shape)
解决方法
-
您的问题是由缩放尺寸除以比例因子时的向上舍入引起的。
-
解决方案是使用 floor 功能:
# P = [float(i)/scale_factor,float(j)/scale_factor]
from math import floor
P = [floor(float(i)/scale_factor),floor(float(j)/scale_factor)]