# python 图像插值 最近邻、双线性、双三次

5 篇文章 0 订阅

import cv2
import numpy as np
def function(img):
height,width,channels =img.shape
emptyImage=np.zeros((2048,2048,channels),np.uint8)
sh=2048/height
sw=2048/width
for i in range(2048):
for j in range(2048):
x=int(i/sh)
y=int(j/sw)
emptyImage[i,j]=img[x,y]
return emptyImage

zoom=function(img)
cv2.imshow("nearest neighbor",zoom)
cv2.imshow("image",img)
cv2.waitKey(0)

import cv2
import numpy as np
import math
def function(img,m,n):
height,width,channels =img.shape
emptyImage=np.zeros((m,n,channels),np.uint8)
value=[0,0,0]
sh=m/height
sw=n/width
for i in range(m):
for j in range(n):
x = i/sh
y = j/sw
p=(i+0.0)/sh-x
q=(j+0.0)/sw-y
x=int(x)-1
y=int(y)-1
for k in range(3):
if x+1<m and y+1<n:
value[k]=int(img[x,y][k]*(1-p)*(1-q)+img[x,y+1][k]*q*(1-p)+img[x+1,y][k]*(1-q)*p+img[x+1,y+1][k]*p*q)
emptyImage[i, j] = (value[0], value[1], value[2])
return emptyImage

zoom=function(img,2048,2048)
cv2.imshow("Bilinear Interpolation",zoom)
cv2.imshow("image",img)
cv2.waitKey(0)

import cv2
import numpy as np
import math

def S(x):
x = np.abs(x)
if 0 <= x < 1:
return 1 - 2 * x * x + x * x * x
if 1 <= x < 2:
return 4 - 8 * x + 5 * x * x - x * x * x
else:
return 0
def function(img,m,n):
height,width,channels =img.shape
emptyImage=np.zeros((m,n,channels),np.uint8)
sh=m/height
sw=n/width
for i in range(m):
for j in range(n):
x = i/sh
y = j/sw
p=(i+0.0)/sh-x
q=(j+0.0)/sw-y
x=int(x)-2
y=int(y)-2
A = np.array([
[S(1 + p), S(p), S(1 - p), S(2 - p)]
])
if x>=m-3:
m-1
if y>=n-3:
n-1
if x>=1 and x<=(m-3) and y>=1 and y<=(n-3):
B = np.array([
[img[x-1, y-1], img[x-1, y],
img[x-1, y+1],
img[x-1, y+1]],
[img[x, y-1], img[x, y],
img[x, y+1], img[x, y+2]],
[img[x+1, y-1], img[x+1, y],
img[x+1, y+1], img[x+1, y+2]],
[img[x+2, y-1], img[x+2, y],
img[x+2, y+1], img[x+2, y+1]],

])
C = np.array([
[S(1 + q)],
[S(q)],
[S(1 - q)],
[S(2 - q)]
])
blue = np.dot(np.dot(A, B[:, :, 0]), C)[0, 0]
green = np.dot(np.dot(A, B[:, :, 1]), C)[0, 0]
red = np.dot(np.dot(A, B[:, :, 2]), C)[0, 0]

# ajust the value to be in [0,255]
if value > 255:
value = 255
elif value < 0:
value = 0
return value

emptyImage[i, j] = np.array([blue, green, red], dtype=np.uint8)

return emptyImage

zoom=function(img,1024,1024)
cv2.imshow("cubic",zoom)
cv2.imshow("image",img)
cv2.waitKey(0)

• 6
点赞
• 37
收藏
觉得还不错? 一键收藏
• 打赏
• 4
评论
05-21 156
03-16 193
03-22 4660
03-01 106
12-28 5460
03-05 7665
05-25 4362

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

cc0587

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。