使用 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 (将#修改为@)