import numpy as np
import cv2
import math
Image = range(1, 17, 1)
Image = np.reshape(Image, (4,4))
Image = Image.astype("uint8")
print(Image)
'''
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
'''
output1 = cv2.resize(Image, (11,11), None, 0, 0, cv2.INTER_NEAREST)
print(output1)
'''
[[ 1 1 1 2 2 2 3 3 3 4 4]
[ 1 1 1 2 2 2 3 3 3 4 4]
[ 1 1 1 2 2 2 3 3 3 4 4]
[ 5 5 5 6 6 6 7 7 7 8 8]
[ 5 5 5 6 6 6 7 7 7 8 8]
[ 5 5 5 6 6 6 7 7 7 8 8]
[ 9 9 9 10 10 10 11 11 11 12 12]
[ 9 9 9 10 10 10 11 11 11 12 12]
[ 9 9 9 10 10 10 11 11 11 12 12]
[13 13 13 14 14 14 15 15 15 16 16]
[13 13 13 14 14 14 15 15 15 16 16]]
'''
output2 = cv2.resize(Image, (11,11), None, 0, 0, cv2.INTER_LINEAR)
print(output2)
'''
[[ 1 1 1 2 2 2 3 3 3 4 4]
[ 1 1 1 2 2 3 3 3 4 4 4]
[ 3 3 3 3 4 4 4 5 5 6 6]
[ 4 4 4 5 5 6 6 6 7 7 7]
[ 5 5 6 6 7 7 7 8 8 8 8]
[ 7 7 7 8 8 9 9 9 10 10 10]
[ 8 8 9 9 10 10 10 10 11 11 11]
[10 10 10 11 11 11 12 12 12 13 13]
[11 11 12 12 12 13 13 13 14 14 14]
[13 13 13 13 14 14 14 15 15 16 16]
[13 13 13 14 14 14 15 15 15 16 16]]
'''
#最近邻居
temp = np.zeros((11, 11), dtype=np.uint8)
rows, cols = temp.shape[:2]
for row in range(0, rows, 1):
for col in range(0, cols, 1):
#结果图中的每一个像素点值由原图得到
#srcX / dstX = srcWidth / dstWidth
#srcY / dstY = srcWidth / dstWidth
#==
#srcX = dstX * (srcWidth / dstWidth)
#srcY = dstY * (srcHeight / dstHeight)
ind_x = (col*(4/11))
ind_y = (row*(4/11))
#print("ind_x, ind_y: ", ind_x, ind_y)
true_ind_x = math.floor(ind_x)
true_ind_y = math.floor(ind_y)
#print("true_ind_x, true_ind_y: ", true_ind_x, true_ind_y)
temp[row, col] = Image[ true_ind_y, true_ind_x ]
print(temp)
'''
[[ 1 1 1 2 2 2 3 3 3 4 4]
[ 1 1 1 2 2 2 3 3 3 4 4]
[ 1 1 1 2 2 2 3 3 3 4 4]
[ 5 5 5 6 6 6 7 7 7 8 8]
[ 5 5 5 6 6 6 7 7 7 8 8]
[ 5 5 5 6 6 6 7 7 7 8 8]
[ 9 9 9 10 10 10 11 11 11 12 12]
[ 9 9 9 10 10 10 11 11 11 12 12]
[ 9 9 9 10 10 10 11 11 11 12 12]
[13 13 13 14 14 14 15 15 15 16 16]
[13 13 13 14 14 14 15 15 15 16 16]]
'''
#双线性
temp2 = np.zeros((11, 11), dtype=np.uint8)
rows, cols = temp2.shape[:2]
for row in range(0, rows, 1):
for col in range(0, cols, 1):
#结果图中的每一个像素点值由原图得到
#srcX / dstX = srcWidth / dstWidth
#srcY / dstY = srcWidth / dstWidth
#==
#srcX = dstX * (srcWidth / dstWidth)
#srcY = dstY * (srcHeight / dstHeight)
ind_x = (col*(4/11))
ind_y = (row*(4/11))
LeftUp = [math.floor(ind_x), math.floor(ind_y)]
RightDown = [math.floor(ind_x)+1, math.floor(ind_y)+1]
RightUp = [math.floor(ind_x)+1, math.floor(ind_y)]
LeftDown = [math.floor(ind_x), math.floor(ind_y)+1]
#越界检测
LeftUp[0] = LeftUp[0]-1 if LeftUp[0] >= 4 else LeftUp[0]
LeftUp[1] = LeftUp[1]-1 if LeftUp[1] >= 4 else LeftUp[1]
RightDown[0] = RightDown[0]-1 if RightDown[0] >= 4 else RightDown[0]
RightDown[1] = RightDown[1]-1 if RightDown[1] >= 4 else RightDown[1]
RightUp[0] = RightUp[0]-1 if RightUp[0] >= 4 else RightUp[0]
RightUp[1] = RightUp[1]-1 if RightUp[1] >= 4 else RightUp[1]
LeftDown[0] = LeftDown[0]-1 if LeftDown[0] >= 4 else LeftDown[0]
LeftDown[1] = LeftDown[1]-1 if LeftDown[1] >= 4 else LeftDown[1]
value_LeftUp = Image[LeftUp[1], LeftUp[0]]
value_RightDown = Image[RightDown[1], RightDown[0]]
value_RightUp = Image[RightUp[1], RightUp[0]]
value_LeftDown = Image[LeftDown[1], LeftDown[0]]
#Decimal_point
u = ind_x-LeftUp[0]
v = ind_y-LeftUp[1]
temp2[row, col] = (1-u)* (1-v) * value_LeftUp + \
(1-u)* v * value_LeftDown + \
u * (1-v) * value_RightUp + \
u * v * value_RightDown
print(temp2)
'''
[[ 1 1 1 2 2 2 3 3 3 4 4]
[ 2 2 3 3 3 4 4 5 5 5 5]
[ 3 4 4 5 5 5 6 6 6 6 6]
[ 5 5 6 6 6 7 7 7 8 8 8]
[ 6 7 7 7 8 8 9 9 9 9 9]
[ 8 8 9 9 9 10 10 10 11 11 11]
[ 9 10 10 10 11 11 11 12 12 12 12]
[11 11 11 12 12 13 13 13 14 14 14]
[12 13 13 13 14 14 14 15 15 15 15]
[13 13 13 14 14 14 15 15 15 16 16]
[13 13 13 14 14 14 15 15 15 16 16]]
'''
'''opencv
[[ 1 1 1 2 2 2 3 3 3 4 4]
[ 1 1 1 2 2 3 3 3 4 4 4]
[ 3 3 3 3 4 4 4 5 5 6 6]
[ 4 4 4 5 5 6 6 6 7 7 7]
[ 5 5 6 6 7 7 7 8 8 8 8]
[ 7 7 7 8 8 9 9 9 10 10 10]
[ 8 8 9 9 10 10 10 10 11 11 11]
[10 10 10 11 11 11 12 12 12 13 13]
[11 11 12 12 12 13 13 13 14 14 14]
[13 13 13 13 14 14 14 15 15 16 16]
[13 13 13 14 14 14 15 15 15 16 16]]
'''