微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 RectBivariateSpline 进行图像平滑

如何解决使用 RectBivariateSpline 进行图像平滑

我正在尝试使用 RectBivariateSpline 创建图像插值。该代码似乎对某些图像可以正常工作,但不是我提供的每个图像。我有什么地方做错了,你可以帮我吗?

正确工作的插值是这个

The code worked correctly with this image

对于其他图像,例如此图像,它不进行任何插值。

The code did not work on this one

代码在这里

import numpy as np
from scipy import signal,misc
import matplotlib.pyplot as plt
from PIL import Image
from itertools import product
import pandas as pd
from scipy.interpolate import griddata
from scipy import interpolate
import matplotlib.pyplot as plt


im = Image.open('32_27.PNG')
im = np.array(im.convert('L'))
image = im.astype(np.float32)
derfilt = np.array([1.0,-1,1.0],dtype=np.float32)
ck = signal.cspline2d(image,8.0)
deriv = (signal.sepfir2d(ck,derfilt,[1]) +
         signal.sepfir2d(ck,[1],derfilt))

M,N = im.shape
nx,ny = im.shape[1],im.shape[0]
x = np.arange(im.shape[1])
y = np.arange(im.shape[0])

z = im[:,:]

x = np.ravel(x)
y = np.ravel(y)

pixels = im.reshape(-1)

indices = np.array(list(product(x,y)))

index = pd.Series(pixels,name="pixels")
df = pd.DataFrame({
    "Y" : indices[:,0],"X" : indices[:,1],},index=index)
print(df.index.values)

x_range=((x.max()-x.min()))
y_range=((y.max()-y.min()))
grid_x,grid_y = np.mgrid[x.min():x.max():(M*1j),y.min():y.max():(N*1j)]
points = df[['X','Y']].values
values = df.index.values

grid_z0 = griddata(points,values,(grid_x,grid_y),method='linear').astype(np.float32)


interp = interpolate.RectBivariateSpline(y,x,grid_z0.T)
#result = interpolate.RectBivariateSpline.__call__(interp,y,grid_z0.T )

xnew = np.arange(im.shape[1])
ynew = np.arange(im.shape[0])
xnew = np.ravel(xnew)
ynew= np.ravel(ynew)
x2_range=((xnew.max()-xnew.min()))
y2_range=((ynew.max()-ynew.min()))
grid_xNew,grid_yNew = np.mgrid[xnew.min():xnew.max():(M*50j),ynew.min():ynew.max():(N*50j)]

plt.figure()
plt.imshow(im)
plt.gray()
plt.title('Original Image')
plt.show()

z2 = interp.ev(grid_xNew,grid_yNew)
plt.figure()
plt.imshow(z2)
plt.gray()
plt.title('Interpolated Image')
plt.show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。